spring源码学习之:项目公共配置项解决方案

 

一:项目中有一些key,value的简单配置

org.apache.commons.configuration.DatabaseConfiguration可以轻松解决

二:配置项目的xml中bean

spring源码学习之:项目公共配置项解决方案
 1 <bean name="databaseConfiguration"  class="org.apache.commons.configuration.DatabaseConfiguration">
 2         <constructor-arg type="javax.sql.DataSource">
 3             <bean class="org.springframework.jndi.JndiObjectFactoryBean">
 4                 <property name="jndiName">
 5                     <value>java:comp/env/jdbc/PROFILEDS</value>
 6                 </property>
 7             </bean>
 8         </constructor-arg>
 9         <constructor-arg index="1" value="配置表的表名字"/>
10         <constructor-arg index="2" value="配置表的key的列名"/>
11         <constructor-arg index="3" value="配置表的value的列名"/>
12 </bean>
spring源码学习之:项目公共配置项解决方案

三:项目中就可以使用Configuration作为注入应用,就可以获取配置表中对应key的value值

1 @Autowired
2 private Configuration configuration;

四:spring的FactoryBean的接口

spring源码学习之:项目公共配置项解决方案
 1 <!--该bean注入到ioc容器是一个Properties对象-->
 2 <bean name="commonsConfigurationFactoryBean" class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
 3        <constructor-arg ref="databaseConfiguration"/>
 4     </bean>
 5     
 6 
 7 
 8 <!--查询配置表的bean,项目中业务类可以直接注入Configuration使用,实时查询配置信息-->
 9 <bean name="databaseConfiguration"  class="org.apache.commons.configuration.DatabaseConfiguration">
10         <constructor-arg type="javax.sql.DataSource">
11             <bean class="org.springframework.jndi.JndiObjectFactoryBean">
12                 <property name="jndiName">
13                     <value>java:comp/env/jdbc/PROFILEDS</value>
14                 </property>
15             </bean>
16         </constructor-arg>
17         <constructor-arg index="1" value="配置表的表名字"/>
18         <constructor-arg index="2" value="配置表的key的列名"/>
19         <constructor-arg index="3" value="配置表的value的列名"/>
20 </bean>
spring源码学习之:项目公共配置项解决方案

==>在xml中配置实现该接口的类的bean,返回给ioc容器中的对象,是实现该接口的类的getObject()返回的对象。

==& gt;上述配置中 org.springmodules.commons.configuration.CommonsConfigurationFactoryBean就 是实现FactoryBean的实现类。返回给ioc容器的对象为

五:org.springframework.beans.factory.config.PropertyPlaceholderConfigurer占位符号的应用

==>占位符号应用的配置信息

spring源码学习之:项目公共配置项解决方案
 1 <!-- 这样,就可以在xml配置文件里用展位符号,业务bean里的属性也可以用占位符号 -->
 2     <bean >  
 3         <property name="order" value="1" />  
 4         <property name="ignoreUnresolvablePlaceholders" value="true" />
 5         <property name="properties" ref="commonsConfigurationFactoryBean"/>
 6     </bean>
 7 
 8 
 9 
10 <!--该bean注入到ioc容器是一个Properties对象-->
11 <bean name="commonsConfigurationFactoryBean" class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
12        <constructor-arg ref="databaseConfiguration"/>
13     </bean>
14     
15 
16 
17 <!--查询配置表的bean,项目中业务类可以直接注入Configuration使用,实时查询配置信息-->
18 <bean name="databaseConfiguration"  class="org.apache.commons.configuration.DatabaseConfiguration">
19         <constructor-arg type="javax.sql.DataSource">
20             <bean class="org.springframework.jndi.JndiObjectFactoryBean">
21                 <property name="jndiName">
22                     <value>java:comp/env/jdbc/PROFILEDS</value>
23                 </property>
24             </bean>
25         </constructor-arg>
26         <constructor-arg index="1" value="配置表的表名字"/>
27         <constructor-arg index="2" value="配置表的key的列名"/>
28         <constructor-arg index="3" value="配置表的value的列名"/>
29 </bean>
spring源码学习之:项目公共配置项解决方案

==>xml的应用案例

<!--${}-->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="staticMethod" value="com.configuration.BusinessSourceHelper.setBusinessSource"/>         <property name="arguments" value="${commons.configuration.businessSource}" />
</bean>

==>业务类的应用案例

spring源码学习之:项目公共配置项解决方案
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 

 @Producer
 public class AddRuleProducer {
     
    @Value("${billing.add.rule}")
     private String billingTopic;
     
     @Autowired
     private MessageSender messageSender;
     
     public void sendTopic(String message) {
         messageSender.send(billingTopic, message);
     }
 }
spring源码学习之:项目公共配置项解决方案

六:spring占位符

Spring 利用PropertyPlaceholderConfigurer占位符

1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现。PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改。

2.在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码,如:

spring源码学习之:项目公共配置项解决方案
spring源码学习之:项目公共配置项解决方案
<bean >

   <property name="location">

     <value>conf/sqlmap/jdbc.properties</value>

   </property>

    <property name="fileEncoding">

      <value>UTF-8</value>

    </property>

</bean>
spring源码学习之:项目公共配置项解决方案
spring源码学习之:项目公共配置项解决方案

当然也可以引入多个属性文件,如:

spring源码学习之:项目公共配置项解决方案
spring源码学习之:项目公共配置项解决方案
<bean >

  <property name="locations">

   <list>

    <value>/WEB-INF/mail.properties</value>  

    <value>classpath: conf/sqlmap/jdbc.properties</value>//注意这两种value值的写法

   </list>

  </property>

</bean>
spring源码学习之:项目公共配置项解决方案
spring源码学习之:项目公共配置项解决方案

3.譬如,jdbc.properties的内容为:

spring源码学习之:项目公共配置项解决方案
spring源码学习之:项目公共配置项解决方案
jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost/mysqldb?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=round;

jdbc.username=root

jdbc.password=123456
spring源码学习之:项目公共配置项解决方案
spring源码学习之:项目公共配置项解决方案

4.那么在spring配置文件中,我们就可以这样写:

spring源码学习之:项目公共配置项解决方案
spring源码学习之:项目公共配置项解决方案
<bean >

  <property name="locations">

   <list>

    <value>classpath: conf/sqlmap/jdbc.properties </value>

   </list>

  </property>

</bean>

<bean >

  <property name="driverClassName"value="${jdbc.driverClassName}" />

  <property name="url" value="${jdbc.url}" />

  <property name="username" value="${jdbc.username}"/>

  <property name="password"value="${jdbc.password}" />

</bean>
spring源码学习之:项目公共配置项解决方案
spring源码学习之:项目公共配置项解决方案

5.这样,一个简单的数据源就设置完毕了。可以看出:PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具。

6.查看源代码,可以发现,locations属性定义在 PropertyPlaceholderConfigurer的祖父类 PropertiesLoaderSupport中,而location只有 setter方法。类似于这样的配置,在spring的源程序中很常见的。

PropertyPlaceholderConfigurer如果在指定的Properties文件中找不到你想使用的属性,它还会在Java的System类属性中查找。

我们可以通过System.setProperty(key, value)或者java中通过-Dnamevalue来给Spring配置文件传递参数。

一:项目中有一些key,value的简单配置

org.apache.commons.configuration.DatabaseConfiguration可以轻松解决

二:配置项目的xml中bean

spring源码学习之:项目公共配置项解决方案
 1 <bean name="databaseConfiguration"  class="org.apache.commons.configuration.DatabaseConfiguration">
 2         <constructor-arg type="javax.sql.DataSource">
 3             <bean class="org.springframework.jndi.JndiObjectFactoryBean">
 4                 <property name="jndiName">
 5                     <value>java:comp/env/jdbc/PROFILEDS</value>
 6                 </property>
 7             </bean>
 8         </constructor-arg>
 9         <constructor-arg index="1" value="配置表的表名字"/>
10         <constructor-arg index="2" value="配置表的key的列名"/>
11         <constructor-arg index="3" value="配置表的value的列名"/>
12 </bean>
spring源码学习之:项目公共配置项解决方案

三:项目中就可以使用Configuration作为注入应用,就可以获取配置表中对应key的value值

1 @Autowired
2 private Configuration configuration;

四:spring的FactoryBean的接口

spring源码学习之:项目公共配置项解决方案
 1 <!--该bean注入到ioc容器是一个Properties对象-->
 2 <bean name="commonsConfigurationFactoryBean" class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
 3        <constructor-arg ref="databaseConfiguration"/>
 4     </bean>
 5     
 6 
 7 
 8 <!--查询配置表的bean,项目中业务类可以直接注入Configuration使用,实时查询配置信息-->
 9 <bean name="databaseConfiguration"  class="org.apache.commons.configuration.DatabaseConfiguration">
10         <constructor-arg type="javax.sql.DataSource">
11             <bean class="org.springframework.jndi.JndiObjectFactoryBean">
12                 <property name="jndiName">
13                     <value>java:comp/env/jdbc/PROFILEDS</value>
14                 </property>
15             </bean>
16         </constructor-arg>
17         <constructor-arg index="1" value="配置表的表名字"/>
18         <constructor-arg index="2" value="配置表的key的列名"/>
19         <constructor-arg index="3" value="配置表的value的列名"/>
20 </bean>
spring源码学习之:项目公共配置项解决方案

==>在xml中配置实现该接口的类的bean,返回给ioc容器中的对象,是实现该接口的类的getObject()返回的对象。

==& gt;上述配置中 org.springmodules.commons.configuration.CommonsConfigurationFactoryBean就 是实现FactoryBean的实现类。返回给ioc容器的对象为

五:org.springframework.beans.factory.config.PropertyPlaceholderConfigurer占位符号的应用

==>占位符号应用的配置信息

spring源码学习之:项目公共配置项解决方案
 1 <!-- 这样,就可以在xml配置文件里用展位符号,业务bean里的属性也可以用占位符号 -->
 2     <bean >  
 3         <property name="order" value="1" />  
 4         <property name="ignoreUnresolvablePlaceholders" value="true" />
 5         <property name="properties" ref="commonsConfigurationFactoryBean"/>
 6     </bean>
 7 
 8 
 9 
10 <!--该bean注入到ioc容器是一个Properties对象-->
11 <bean name="commonsConfigurationFactoryBean" class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
12        <constructor-arg ref="databaseConfiguration"/>
13     </bean>
14     
15 
16 
17 <!--查询配置表的bean,项目中业务类可以直接注入Configuration使用,实时查询配置信息-->
18 <bean name="databaseConfiguration"  class="org.apache.commons.configuration.DatabaseConfiguration">
19         <constructor-arg type="javax.sql.DataSource">
20             <bean class="org.springframework.jndi.JndiObjectFactoryBean">
21                 <property name="jndiName">
22                     <value>java:comp/env/jdbc/PROFILEDS</value>
23                 </property>
24             </bean>
25         </constructor-arg>
26         <constructor-arg index="1" value="配置表的表名字"/>
27         <constructor-arg index="2" value="配置表的key的列名"/>
28         <constructor-arg index="3" value="配置表的value的列名"/>
29 </bean>
spring源码学习之:项目公共配置项解决方案

==>xml的应用案例

<!--${}-->
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="staticMethod" value="com.configuration.BusinessSourceHelper.setBusinessSource"/>         <property name="arguments" value="${commons.configuration.businessSource}" />
</bean>

==>业务类的应用案例

spring源码学习之:项目公共配置项解决方案
 
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 

 @Producer
 public class AddRuleProducer {
     
    @Value("${billing.add.rule}")
     private String billingTopic;
     
     @Autowired
     private MessageSender messageSender;
     
     public void sendTopic(String message) {
         messageSender.send(billingTopic, message);
     }
 }
spring源码学习之:项目公共配置项解决方案

六:spring占位符

Spring 利用PropertyPlaceholderConfigurer占位符

1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现。PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改。

2.在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码,如:

spring源码学习之:项目公共配置项解决方案
spring源码学习之:项目公共配置项解决方案
<bean >

   <property name="location">

     <value>conf/sqlmap/jdbc.properties</value>

   </property>

    <property name="fileEncoding">

      <value>UTF-8</value>

    </property>

</bean>
spring源码学习之:项目公共配置项解决方案
spring源码学习之:项目公共配置项解决方案

当然也可以引入多个属性文件,如:

spring源码学习之:项目公共配置项解决方案
spring源码学习之:项目公共配置项解决方案
<bean >

  <property name="locations">

   <list>

    <value>/WEB-INF/mail.properties</value>  

    <value>classpath: conf/sqlmap/jdbc.properties</value>//注意这两种value值的写法

   </list>

  </property>

</bean>
spring源码学习之:项目公共配置项解决方案
spring源码学习之:项目公共配置项解决方案

3.譬如,jdbc.properties的内容为:

spring源码学习之:项目公共配置项解决方案
spring源码学习之:项目公共配置项解决方案
jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost/mysqldb?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=round;

jdbc.username=root

jdbc.password=123456
spring源码学习之:项目公共配置项解决方案
spring源码学习之:项目公共配置项解决方案

4.那么在spring配置文件中,我们就可以这样写:

spring源码学习之:项目公共配置项解决方案
spring源码学习之:项目公共配置项解决方案
<bean >

  <property name="locations">

   <list>

    <value>classpath: conf/sqlmap/jdbc.properties </value>

   </list>

  </property>

</bean>

<bean >

  <property name="driverClassName"value="${jdbc.driverClassName}" />

  <property name="url" value="${jdbc.url}" />

  <property name="username" value="${jdbc.username}"/>

  <property name="password"value="${jdbc.password}" />

</bean>
spring源码学习之:项目公共配置项解决方案
spring源码学习之:项目公共配置项解决方案

5.这样,一个简单的数据源就设置完毕了。可以看出:PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具。

6.查看源代码,可以发现,locations属性定义在 PropertyPlaceholderConfigurer的祖父类 PropertiesLoaderSupport中,而location只有 setter方法。类似于这样的配置,在spring的源程序中很常见的。

PropertyPlaceholderConfigurer如果在指定的Properties文件中找不到你想使用的属性,它还会在Java的System类属性中查找。

我们可以通过System.setProperty(key, value)或者java中通过-Dnamevalue来给Spring配置文件传递参数。

相关推荐