spring源码学习之:项目公共配置项解决方案
2017-03-06 16:44
429 查看
一:项目中有一些key,value的简单配置
org.apache.commons.configuration.DatabaseConfiguration可以轻松解决
二:配置项目的xml中bean
View Code
六:spring占位符
Spring 利用PropertyPlaceholderConfigurer占位符
1. PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现。PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改。
2.在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码,如:
当然也可以引入多个属性文件,如:
3.譬如,jdbc.properties的内容为:
4.那么在spring配置文件中,我们就可以这样写:
5.这样,一个简单的数据源就设置完毕了。可以看出:PropertyPlaceholderConfigurer起的作用就是将占位符指向的数据库配置信息放在bean中定义的工具。
6.查看源代码,可以发现,locations属性定义在 PropertyPlaceholderConfigurer的祖父类 PropertiesLoaderSupport中,而location只有 setter方法。类似于这样的配置,在spring的源程序中很常见的。
PropertyPlaceholderConfigurer如果在指定的Properties文件中找不到你想使用的属性,它还会在Java的System类属性中查找。
我们可以通过System.setProperty(key, value)或者java中通过-Dnamevalue来给Spring配置文件传递参数。
org.apache.commons.configuration.DatabaseConfiguration可以轻松解决
二:配置项目的xml中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); } }
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&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的aop目标对象中进行自我调用,且需要实施相应的事务定义的解决方案
- MongoDB的使用学习之(七)MongoDB的聚合查询(spring-data-mongodb两种方式)附项目源码
- Spring源码学习一_下载Spring项目源码并编译为Eclipse
- MyBatis学习总结(二)——SpringMVC+Spring4+Mybatis3集成,开发简单Web项目+源码下载
- Spring源码学习一_下载Spring项目源码并编译为Eclipse项目
- ThinkPHP学习之项目配置与前后台公共配置项
- Spring Framework Reference 5.0.2 源码学习记录 使用p-namespace和c-namespace
- spring源码学习笔记-初始化(二)-BeanFactory
- Maven项目配置spring时 出现Could not open ServletContext resource [/WEB-INF/applicationContext.xml]解决方案
- Spring MVC 学习笔记(二) 基于spring2.5注解实现的spring MVC项目
- Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码)
- 【系统学习SpringBoot】初见Thymeleaf之SpringBoot访问项目中的 jsp/html
- 【spring源码学习】spring的IOC容器在初始化bean过程
- spring IOC源码学习(二):BeanDefinition资源加载
- JavaEE进阶知识学习-----SpringBoot基础知识-1-项目初始化知识
- 专访许鹏:谈C程序员修养及大型项目源码阅读与学习
- Java之美[从菜鸟到高手演变]之Spring源码学习 - 环境搭建
- spring源码学习之路---IOC实现原理(三)
- dubbo源码学习笔记----结合Spring
- 【持久化框架】SpringMVC+Spring4+Mybatis3集成,开发简单Web项目+源码下载