目录html
PropertyPlaceholderConfigurer
位于org.springframework.beans.factory.config 包下,它的继承体系以下java
PropertyPlaceholderConfigurer 直接继承于PlaceholderConfigurerSupport
,它的已知实现类只有一个mysql
PreferencesPlaceholderConfigurer
spring
源自JavaDoc: PropertyPlaceholderConfigurer 是 PlaceholderConfigurerSupport 的一个子类,用来解析${…}
占位符的,可使用setLocation
和setProperties
设置系统属性和环境变量。从Spring3.1 开始,PropertySourcesPlaceholderConfigurer应优先与此实现,经过使用Spring3.1 中的 Environment和 PropertySource机制, 使它的灵活性更强。sql
可是PropertyPlaceholderConfigurer却适用以下状况:当 spring-context
模块不可用的时候,使用BeanFactory的API 而不是ApplicationContext的API。现有配置使用setSystemPropertiesMode 和 setSystemPropertiesModeName属性,建议用户不要使用这些设置, 而是使用容器的Environment属性;数据库
在Spring3.1 以前,<context:property-placeholder/>
命名空间保存了PropertyPlaceholderConfigurer的实例,若是使用spring-context-3.0 xsd的定义的话,仍然会这样作。也就是说,即便使用Spring 3.1,您也能够经过命名空间保留PropertyPlaceholderConfigurer; 只是不更新schemaLocation 并继续使用3.0 XSD。api
BeanFactoryPostProcessor
接口的一个实现。PropertyPlaceholderConfigurer能够将上下文(配置文 件)中的属性值放在另外一个单独的标准java Properties文件中去。在XML文件中用${...}替换指定的properties文件中的值。这样的话,只须要对properties文件进 行修改,而不用对xml配置文件进行修改。jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/sys jdbc.username=root jdbc.password=123456
这是一个最基本的配置数据库链接的设置,前缀统一使用jdbc来命名ide
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>database.properties</value> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <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> </beans>
经过给PropertyPlaceholderConfigurer 设置一个bean,指定
的名称为location,指定value值就可以引入外部配置文件,而后就可以经过${jdbc.key} 来获取properties 中的值 工具
file.encoding=utf-8 file.name=encoding
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>database.properties</value> <value>encoding.properties</value> </list> </property> </bean>
正如PropertyPlaceholderConfigurer基本概念中提到的,Spring可使用<context:property-placeholder/>
做为PropertyPlaceholderConfigurer 的替代方案,代码以下url
<!-- 指定单个properties --> <!--<context:property-placeholder location="database.properties" />--> <!-- 指定多个properties--> <!--<context:property-placeholder location="classpath:*.properties"/>--> <!--<context:property-placeholder location="classpath:database.properties, classpath:encoding.properties"/>--> <!-- 指定配置文件加载顺序--> <context:property-placeholder order="0" location="database.properties" /> <context:property-placeholder order="1" location="encoding.properties" />
public class SubPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private static Map<String, String> ctxPropertiesMap; @Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { // 调用父类PropertyPlaceholderConfigurer 的构造器 super.processProperties(beanFactoryToProcess, props); // 遍历配置文件的key,Properties 对象就是导入的配置文件 Enumeration<?> enumeration = props.propertyNames(); while (enumeration.hasMoreElements()) { System.out.println(enumeration.nextElement()); } ctxPropertiesMap = new HashMap<String, String>(); for (Object key : props.keySet()) { String keyStr = key.toString(); String value = props.getProperty(keyStr); ctxPropertiesMap.put(keyStr, value); } } public static String getProperty(String name){ return ctxPropertiesMap.get(name); } }
<bean id="propertyPlaceholderConfigurer" class="com.cxuan.spring.common.SubPropertyPlaceholderConfigurer"> <property name="location"> <value>database.properties</value> </property> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <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>
如何启动呢?其实引入的SubPropertyPlaceholderConfigurer 就可以随着Spring加载配置文件而被加载。
直接定义main方法,用ClassPathXmlApplicayionContext引入任意的配置文件便可。
文章参考:
Spring里PropertyPlaceholderConfigurer类的使用](https://www.cnblogs.com/huqianliang/p/5673701.html)
PropertyPlaceholderConfigurer读取配置文件
https://blog.csdn.net/y_index/article/details/79893765
https://blog.csdn.net/eson_15/article/details/51365707
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html#processProperties-org.springframework.beans.factory.config.ConfigurableListableBeanFactory-java.util.Properties-
https://blog.csdn.net/wrs120/article/details/84554366