美文网首页
Spring(四)—— 使用外部属性文件

Spring(四)—— 使用外部属性文件

作者: 年少懵懂丶流年梦 | 来源:发表于2017-04-19 18:04 被阅读49次

在配置文件里配置Bean时,有时需要在Bean的配置里混入系统部署的细节信息(例如:文件路径,数据源配置信息等)。
而这些部署细节实际上需要和Bean配置相分离。如果我们使用外部的一个文件来存放这些信息,显然是更好的选择。

代码演示

这里使用mysql做演示,其中的用户名和密码根据自己情况来写。jdbcUrl中的test表示我将访问test这个数据库.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

  <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="root"></property>
        <property name="password" value="111111"></property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///test"></property>
    </bean>

</beans>
package test;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) throws SQLException {
        // 导入IOC容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        // 获取数据源对象
        DataSource dataSource = (DataSource) ctx.getBean("dataSource");

        // 输出驱动
        System.out.println(dataSource.getConnection());
    }

}

OK,成功。运行结果:


接下来将这些配置信息放到外部文件中

首先在src文件下新建一个 db.properties 文件

db.properties

myuser=root
password=123456
driverclass=com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql:///test

applicationContext.xml

这里首先需要添加Context的命名空间:xml下面选中 Namespaces 选相框。钩上Context即可。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <!-- 导入属性wen -->
    <context:property-placeholder location="classpath:db.properties"/>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${myuser}"></property>
        <property name="password" value="${password}"></property>
        <property name="driverClass" value="${driverclass}"></property>
        <property name="jdbcUrl" value="${jdbcurl}"></property>
    </bean>

</beans>

将上述代码运行,结果跟之前的是一样的

Spring提供了一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将Bean配置的部分内容外移到属性文件中,可以在Bean配置文件里使用形式为<mark>${var}</mark>的变量,PropertyPlaceholderConfigurer从属性文件(db.properties)里加载属性,并使用这些属性来替换变量。

而我们首先在db.properties上写好了Jdbc的基本信息,然后在xml中调取该文件。最后在dataSource的Bean中通过${var}的方式来调取db.properties中的值。这样一来,当我们需要修改数据库的值时就可以直接修改db.properties的内容了。

小结

Spring提供一种方式让我们将配置信息放在外部文件中。具体做法如下:

  1. 将信息写到xxx.properties文件中,采用属性=值的格式。

  2. 在xml文件中首先开启Context的命名空间,利用PropertyPlaceholderConfigurer来locate到这个db.properties文件。

  3. 在数据源dataSource中通过${var}的格式获取外部文件的值。

相关文章

网友评论

      本文标题:Spring(四)—— 使用外部属性文件

      本文链接:https://www.haomeiwen.com/subject/hbgrwttx.html