spring原始碼學習之:專案公共配置項解決方案

Love Lenka發表於2017-03-06

一:專案中有一些key,value的簡單配置

org.apache.commons.configuration.DatabaseConfiguration可以輕鬆解決

 

二:配置專案的xml中bean

 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>
View Code

 

三:專案中就可以使用Configuration作為注入應用,就可以獲取配置表中對應key的value值

1 @Autowired
2 private Configuration configuration;
View Code

 

四:spring的FactoryBean的介面

 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>
View Code

==>在xml中配置實現該介面的類的bean,返回給ioc容器中的物件,是實現該介面的類的getObject()返回的物件。

==& gt;上述配置中 org.springmodules.commons.configuration.CommonsConfigurationFactoryBean就 是實現FactoryBean的實現類。返回給ioc容器的物件為

 

 

五:org.springframework.beans.factory.config.PropertyPlaceholderConfigurer佔位符號的應用

==>佔位符號應用的配置資訊

 1 <!-- 這樣,就可以在xml配置檔案裡用展位符號,業務bean裡的屬性也可以用佔位符號 -->
 2     <bean id="commonsConfigurationPropertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
 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>
View Code

==>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>
View Code

 

==>業務類的應用案例

 
 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);
     }
 }
View Code

 

六:spring佔位符

Spring 利用PropertyPlaceholderConfigurer佔位符

 

1. PropertyPlaceholderConfigurer是個bean工廠後置處理器的實現,也就是 BeanFactoryPostProcessor介面的一個實現。PropertyPlaceholderConfigurer可以將上下文(配置文 件)中的屬性值放在另一個單獨的標準java Properties檔案中去。在XML檔案中用${key}替換指定的properties檔案中的值。這樣的話,只需要對properties檔案進 行修改,而不用對xml配置檔案進行修改。

2.在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置檔案中加入外部屬性檔案,當然也可以指定外部檔案的編碼,如:

複製程式碼
<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

   <property name="location">

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

   </property>

    <property name="fileEncoding">

      <value>UTF-8</value>

    </property>

</bean>
複製程式碼

 

當然也可以引入多個屬性檔案,如:

複製程式碼
<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

  <property name="locations">

   <list>

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

    <value>classpath: conf/sqlmap/jdbc.properties</value>//注意這兩種value值的寫法

   </list>

  </property>

</bean>
複製程式碼

 

3.譬如,jdbc.properties的內容為:

複製程式碼
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
複製程式碼

 

4.那麼在spring配置檔案中,我們就可以這樣寫:

複製程式碼
<bean id="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

  <property name="locations">

   <list>

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

   </list>

  </property>

</bean>

<bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">

  <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>
複製程式碼

 

5.這樣,一個簡單的資料來源就設定完畢了。可以看出:PropertyPlaceholderConfigurer起的作用就是將佔位符指向的資料庫配置資訊放在bean中定義的工具。

6.檢視原始碼,可以發現,locations屬性定義在 PropertyPlaceholderConfigurer的祖父類 PropertiesLoaderSupport中,而location只有 setter方法。類似於這樣的配置,在spring的源程式中很常見的。

PropertyPlaceholderConfigurer如果在指定的Properties檔案中找不到你想使用的屬性,它還會在Java的System類屬性中查詢。

我們可以通過System.setProperty(key, value)或者java中通過-Dnamevalue來給Spring配置檔案傳遞引數。

相關文章