zoukankan      html  css  js  c++  java
  • spring源码学习之:项目公共配置项解决方案

    一:项目中有一些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配置文件传递参数。

  • 相关阅读:
    Educational Codeforces Round 6
    Educational Codeforces Round 5
    [Educational Round 5][Codeforces 616F. Expensive Strings]
    [Codeforces Round #508 (Div. 2)][Codeforces 1038E. Maximum Matching]
    Codeforces Round #509 (Div. 2)
    从零开始编写一个vue插件
    Web前端错题模糊题记录
    vue中响应式props办法
    vue中router-link的click事件失效的解决办法
    ubuntu修改顶栏颜色
  • 原文地址:https://www.cnblogs.com/shangxiaofei/p/6510674.html
Copyright © 2011-2022 走看看