spring源码学习之:项目公共配置项解决方案
一:项目中有一些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&characterEncoding=UTF-8&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源码学习之:项目公共配置项解决方案的更多相关文章
- spring源码学习之路---IOC初探(二)
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章当中我没有提及具体的搭 ...
- Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件
写在前面 从大四实习至今已一年有余,作为一个程序员,一直没有用心去记录自己工作中遇到的问题,甚是惭愧,打算从今日起开始养成写博客的习惯.作为一名java开发人员,Spring是永远绕不过的话题,它的设 ...
- spring源码学习之路---深入AOP(终)
作者:zuoxiaolong8810(左潇龙),转载请注明出处,特别说明:本博文来自博主原博客,为保证新博客中博文的完整性,特复制到此留存,如需转载请注明新博客地址即可. 上一章和各位一起看了一下sp ...
- Spring源码学习
Spring源码学习--ClassPathXmlApplicationContext(一) spring源码学习--FileSystemXmlApplicationContext(二) spring源 ...
- Spring源码学习-容器BeanFactory(四) BeanDefinition的创建-自定义标签的解析.md
写在前面 上文Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签对Spring默认标签的解析做了详解,在xml元素的解析中,Spri ...
- Spring源码学习-容器BeanFactory(三) BeanDefinition的创建-解析Spring的默认标签
写在前面 上文Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作中Spring对XML解析后创建了对应的Docum ...
- Spring源码学习-容器BeanFactory(二) BeanDefinition的创建-解析前BeanDefinition的前置操作
写在前面 上文 Spring源码学习-容器BeanFactory(一) BeanDefinition的创建-解析资源文件主要讲Spring容器创建时通过XmlBeanDefinitionReader读 ...
- 【目录】Spring 源码学习
[目录]Spring 源码学习 jwfy 关注 2018.01.31 19:57* 字数 896 阅读 152评论 0喜欢 9 用来记录自己学习spring源码的一些心得和体会以及相关功能的实现原理, ...
- Spring 源码学习——Aop
Spring 源码学习--Aop 什么是 AOP 以下是百度百科的解释:AOP 为 Aspect Oriented Programming 的缩写,意为:面向切面编程通过预编译的方式和运行期动态代理实 ...
- Spring 源码学习 04:初始化容器与 DefaultListableBeanFactory
前言 在前一篇文章:创建 IoC 容器的几种方式中,介绍了四种方式,这里以 AnnotationConfigApplicationContext 为例,跟进代码,看看 IoC 的启动流程. 入口 从 ...
随机推荐
- SVN使用—工作模式及运行原理以及优缺点对比
一.SVN原理 (1)运行方式 svn服务器有2种运行方式:独立服务器和借助apache运行. 1.独立服务器访问 访问地址如:svn://svn.test.com/test 2.借助Apache等h ...
- Spring混合配置
Spring混合配置 一.在JavaConfig中引入其他配置 在JavaConfig中引入JavaConfig配置 使用@Import({OtherConfig1.class,OtherConfig ...
- DelayQueue与ProirityBlockingQueue
DelayQueue是一个无界队列,只有在延迟期满的时候,才可以取出元素.该队列的头部存储的延期期满了后保存时间最长的元素. DelayQueue阻塞队列在我们系统开发中也常常会用到,例如:缓存系统的 ...
- MYSQL 多实例运行
1.创建数据文件 mkdir /var/lib/mysql_3307 mysql_install_db --datadir=/var/lib/mysql_3307 --user=mysql 2.给数据 ...
- Codeforces Beta Round #17 D.Notepad 指数循环节
D. Notepad time limit per test 2 seconds memory limit per test 64 megabytes input standard input out ...
- 子tab里面新增tab(top.jQuery)
top.jQuery应用. var jq = top.jQuery; if (jq("#centre").tabs("exists", "预览页面&q ...
- Pandas日期功能
日期功能扩展了时间序列,在财务数据分析中起主要作用.在处理日期数据的同时,我们经常会遇到以下情况 - 生成日期序列 将日期序列转换为不同的频率 创建一个日期范围 通过指定周期和频率,使用date.ra ...
- 图片加载之Picasso使用
简介 Picasso是Square公司开源的一个Android图形缓存库,可以实现图片下载和缓存功能. 主要有以下一些特性: 在Adapter中回收和取消已经不在视野范围图片资源的加载,防止可能出现的 ...
- LeetCode第[48]题(Java):Rotate Image
题目:矩阵旋转 难度:Medium 题目内容: You are given an n x n 2D matrix representing an image. Rotate the image by ...
- mysql中删除完全重复数据的准确SQL语句
删除数据库中重复的记录,只保留一条 DELETE FROM tb_gps_records WHERE id NOT IN (SELECT bid FROM (SELECT min(id) as bid ...