Spring PropertyPlaceholderConfigurer
2017-03-17 09:30
169 查看
Spring利用PropertyPlaceholderConfigurer占位符。
1.PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是BeanFactoryPostProcessor接口一个实现。在XML文件中${key}可以用properties文件中值替换,这样只需要对properties文件进行修改,而不用对xml配置文件进行修改。
2.在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码。
也可以引入多个属性文件
譬如,jdbc.properties的内容为:
那么在spring配置文件中,我们就可以这样写:
我们还可以通过System.setProperty(key,value)或者java中通过-D name value来给Spring配置文件传递参数。
1.PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是BeanFactoryPostProcessor接口一个实现。在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>
譬如,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
那么在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>
我们还可以通过System.setProperty(key,value)或者java中通过-D name value来给Spring配置文件传递参数。
相关文章推荐
- spring中PropertyPlaceholderConfigurer的用法
- spring中使用外部属性文件(关于PropertyPlaceholderConfigurer)
- Spring PropertyPlaceholderConfigurer 自定义扩展
- Spring环境搭建之:通过PropertyPlaceholderConfigurer加载属性配置文件:
- Spring--PropertyPlaceholderConfigurer
- Spring 后置处理器 PropertyPlaceholderConfigurer 类(引用外部文件)
- spring 配置文件 获取变量(PropertyPlaceholderConfigurer)
- Spring 中 PropertyPlaceholderConfigurer 的使用
- spring的PropertyPlaceholderConfigurer不生效的问题
- spring中的PropertyPlaceholderConfigurer作用,用法
- spring中PropertyPlaceholderConfigurer的使用
- Spring容器扩展--PropertyPlaceholderConfigurer
- Spring PropertyPlaceholderConfigurer example
- 使用Spring PropertyPlaceholderConfigurer 配置中文出现乱码的解决方法
- spring使用PropertyPlaceholderConfigurer扩展来满足不同环境的参数配置
- Spring PropertySourcesPlaceholderConfigurer工作原理
- Spring 属性占位符配置器 PropertyPlaceholderConfigurer
- 在 Spring.NET 中使用 PropertyPlaceholderConfigurer 设置配置参数
- Spring PropertyPlaceholderConfigurer Usage
- Spring中PropertyPlaceholderConfigurer的使用