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

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

二:配置项目的xml中bean

 <bean name="databaseConfiguration"  class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg type="javax.sql.DataSource">
<bean class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/PROFILEDS</value>
</property>
</bean>
</constructor-arg>
<constructor-arg index="1" value="配置表的表名字"/>
<constructor-arg index="2" value="配置表的key的列名"/>
<constructor-arg index="3" value="配置表的value的列名"/>
</bean>

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

 @Autowired
private Configuration configuration;

四:spring的FactoryBean的接口

 <!--该bean注入到ioc容器是一个Properties对象-->
<bean name="commonsConfigurationFactoryBean" class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
<constructor-arg ref="databaseConfiguration"/>
</bean> <!--查询配置表的bean,项目中业务类可以直接注入Configuration使用,实时查询配置信息-->
<bean name="databaseConfiguration" class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg type="javax.sql.DataSource">
<bean class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/PROFILEDS</value>
</property>
</bean>
</constructor-arg>
<constructor-arg index="1" value="配置表的表名字"/>
<constructor-arg index="2" value="配置表的key的列名"/>
<constructor-arg index="3" value="配置表的value的列名"/>
</bean>

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

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

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

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

 <!-- 这样,就可以在xml配置文件里用展位符号,业务bean里的属性也可以用占位符号 -->
<bean id="commonsConfigurationPropertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="properties" ref="commonsConfigurationFactoryBean"/>
</bean> <!--该bean注入到ioc容器是一个Properties对象-->
<bean name="commonsConfigurationFactoryBean" class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
<constructor-arg ref="databaseConfiguration"/>
</bean> <!--查询配置表的bean,项目中业务类可以直接注入Configuration使用,实时查询配置信息-->
<bean name="databaseConfiguration" class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg type="javax.sql.DataSource">
<bean class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/PROFILEDS</value>
</property>
</bean>
</constructor-arg>
<constructor-arg index="1" value="配置表的表名字"/>
<constructor-arg index="2" value="配置表的key的列名"/>
<constructor-arg index="3" value="配置表的value的列名"/>
</bean>

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

==>业务类的应用案例

 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 利用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配置文件传递参数。

spring源码学习之:项目公共配置项解决方案的更多相关文章

  1. spring源码学习之路---IOC初探(二)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章当中我没有提及具体的搭 ...

  2. Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件

    写在前面 从大四实习至今已一年有余,作为一个程序员,一直没有用心去记录自己工作中遇到的问题,甚是惭愧,打算从今日起开始养成写博客的习惯.作为一名java开发人员,Spring是永远绕不过的话题,它的设 ...

  3. spring源码学习之路---深入AOP(终)

    作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章和各位一起看了一下sp ...

  4. Spring源码学习

    Spring源码学习--ClassPathXmlApplicationContext(一) spring源码学习--FileSystemXmlApplicationContext(二) spring源 ...

  5. Spring源码学习-容器BeanFactory(四) BeanDefinition的创建-自定义标签的解析.md

    写在前面 上文Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签对Spring默认标签的解析做了详解,在xml元素的解析中,Spri ...

  6. Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签

    写在前面 上文Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作中Spring对XML解析后创建了对应的Docum ...

  7. Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作

    写在前面 上文 Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件主要讲Spring容器创建时通过XmlBeanDefinitionReader读 ...

  8. 【目录】Spring 源码学习

    [目录]Spring 源码学习 jwfy 关注 2018.01.31 19:57* 字数 896 阅读 152评论 0喜欢 9 用来记录自己学习spring源码的一些心得和体会以及相关功能的实现原理, ...

  9. Spring 源码学习——Aop

    Spring 源码学习--Aop 什么是 AOP 以下是百度百科的解释:AOP 为 Aspect Oriented Programming 的缩写,意为:面向切面编程通过预编译的方式和运行期动态代理实 ...

  10. Spring 源码学习 04:初始化容器与 DefaultListableBeanFactory

    前言 在前一篇文章:创建 IoC 容器的几种方式中,介绍了四种方式,这里以 AnnotationConfigApplicationContext 为例,跟进代码,看看 IoC 的启动流程. 入口 从 ...

随机推荐

  1. ASP.NET MVC 处理404与500错误页面的方法

    第一步创建ErrorPageController 第二步添加Oops页面 @{ ViewBag.Title = "Oops"; Layout = "~/Areas/Adm ...

  2. 团体程序设计天梯赛 L2-018. 多项式A除以B(模拟)

    题意:给你A,B两个多项式,问你A/B的值:注意多项式给你的是每个式子的指数与系数:保留到一位小数,如果出现系数为0(保留后也是)的情况,请不要输出它,如果没有非系数为0的情况就输出特殊 题解:多项式 ...

  3. struts2——第一个案例

    步骤如下 编程工具等百度云分享 1.创建一个web项目 2.引入struts2的基本jar包 struts2的基本jar包百度云 链接:https://pan.baidu.com/s/1LBnPJhF ...

  4. const作用

    const有以下几个作用: 1. 定义const常量,具有不可变性.eg. const int MAX = 100;  int Array[MAX]; 2. 进行类型检查,使编译器对处理内容有更多的了 ...

  5. Cacti的基本安装配置

    ////////////////////cacti///////////////////////////常用的监控软件有:cacti.nagios.zabbix等 cacti 重图形.有数据历史.需要 ...

  6. 初探UiAutomator2.0中使用Xpath定位元素

    J 今天的主题是讲一下在使用过程中遇到的一个问题,如何在UiAutomator2.0中使用Xpath定位元素? 背景 现在的app在打包成apk的时候都是有加固处理的,各种混淆加固,所以已经破坏了或扰 ...

  7. window cmd

    切换目录盘  直接 d:  (e:  f:) 在目录下切换文件用cd   文件名(可以加绝对路径 绝对路径可以到复制   也可以加相对路径) javac     XXX.java  编译成字节码 Ja ...

  8. 遍历jsonArray和jsonObject

    遍历jsonArray String str = "[{name:'a',value:'aa'},{name:'b',value:'bb'},{name:'c',value:'cc'}]&q ...

  9. C#的静态构造函数.cctor

    静态构造函数操作的是类(而非其实例的)成员.静态构造函数(.cctor)的一些特点:1. 声明和定义形式上,只能有static一个修饰符,不能有任何修饰符和返回值(也不能有void).2. 不能被显示 ...

  10. Pro C/C++环境搭建

    一. 安装oracle软件或者oracle客户端. 二. 载入orasql10.lib (1)在连接器->常规->附件库目录中,加入orasql10.lib库所在目录. (2)在连接器-& ...