Spring基础12——使用外部属性文件

1.使用外部属性文件

        在配置文件里配置Bean时,有时需要在Bean的配置文件里引入系统部署的细节信息(例如:文件的路径、数据源配置信息等),而这些部署细节实际上需要和bean配置相分离,因为我们修改一次properties文件的代价要远远低于修改spring.xml。

       为了满足在bean配置时引入配置文件中的信息,Spring提供一个PropertyPlaceholderConfigurer的BeanFactory后置处理器,这个处理器允许用户将从外部加载的配置文件信息放到bean的属性当中,可以在Bean配置文件里使用形式为${var}的变量,PropertyPlaceholderConfigurer从属性文件里加载属性,并使用这些属性来替换这些带$的变量。

 

首先我们在classpath下创建一个db.properties的数据库配置文件:

1 user=root
2 password=123456
3 driverclass=com.mysql.jdbc.Driver
4 jdbcurl=jdbc:mysql:///test

重新引入spring的命名空间:

1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4        xmlns:context="http://www.springframework.org/schema/context"
5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
6         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
7         http://www.springframework.org/schema/context
8         http://www.springframework.org/schema/context/spring-context-4.2.xsd">

之后我们通过<context>标签将配置文件引进来:

1 <!--导入属性配置文件-->
2 <context:property-placeholder location="classpath:db.properties"/>

接下来建立一个与properties文件属性的应对DataSource类,并生成属性对应的setter方法和toString方法。

 1 /**
 2  * @author wzy
 3  * @version 1.0
 4  * @date 2019/5/8 17:18
 5  */
 6 public class DataSource {
 7     private String user;
 8     private String password;
 9     private String driverClass;
10     private String jdbcUrl;
11 
12     public void setUser(String user) {
13         this.user = user;
14     }
15 
16     public void setPassword(String password) {
17         this.password = password;
18     }
19 
20     public void setDriverClass(String driverClass) {
21         this.driverClass = driverClass;
22     }
23 
24     public void setJdbcUrl(String jdbcUrl) {
25         this.jdbcUrl = jdbcUrl;
26     }
27     @Override
28     public String toString() {
29         return "DataSource{" +
30                 "user='" + user + '\'' +
31                 ", password='" + password + '\'' +
32                 ", driverClass='" + driverClass + '\'' +
33                 ", jdbcUrl='" + jdbcUrl + '\'' +
34                 '}';
35     }
36 }

继续编写spring.xml配置文件,声明一个DataSource类的Bean,并且使用${属性名}的方式从配置文件中取属性。

1 <bean id="dataSource" class="com.wzy.properties.DataSource">
2         <!--使用外部化属性文件的属性-->
3         <property name="user" value="${user}"/>
4         <property name="password" value="${password}"/>
5         <property name="driverClass" value="${driverclass}"/>
6         <property name="jdbcUrl" value="${jdbcurl}"/>
7     </bean>

编写测试类Main:

 1 import org.springframework.context.ApplicationContext;
 2 import org.springframework.context.support.ClassPathXmlApplicationContext;
 3 import java.sql.SQLException;
 4 
 5 /**
 6  * @author wzy
 7  * @version 1.0
 8  * @date 2019/5/7 15:58
 9  */
10 public class Main {
11     public static void main(String[] args) throws SQLException {
12         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-properties.xml");
13 
14         DataSource dataSource = (DataSource) ctx.getBean("dataSource");
15 
16         System.out.println(dataSource);
17 
18     }
19 }

执行结果:

我们可以看到配置文件中的属性成功的注入到了dataSource这个bean当中,我们可以总结出spring引用properties文件中的属性时,需要两步:

1.使用<context:property-placeholder location="classpath:db.properties"/>标签引入配置文件位置。

2.使用${属性名}的方式对配置文件的属性引用到bean当中。

posted @ 2019-05-08 17:33  风云独舞  阅读(155)  评论(0编辑  收藏  举报