文章目录
1.是什么
PropertyPlaceholderConfigurer是个bean工厂后置处理器的实现,也就是 BeanFactoryPostProcessor接口的一个实现。
2.作用
在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码。PropertyPlaceholderConfigurer可以将上下文(配置文 件)中的属性值放在另一个单独的标准java Properties文件中去。在XML文件中用${key}替换指定的properties文件中的值。这样的话,只需要对properties文件进 行修改,而不用对xml配置文件进行修改
3.使用方法
方法一
3.1编写.properties文件
/jdbc.properties 文件
jdbc.url=jdbc:mysql://66.59.208.106:3306/ds?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
jdbc.username=root
jdbc.password=root
3.2.在.xml中引入外部文件,即.properties文件
如果引单个文件:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>jdbc.properties</value>
</property>
<property name="fileEncoding">
<value>UTF-8</value>
</property>
</bean>
如果引多个文件(添加):
classpath可以理解:当前项目src-
classpath路径指向哪里
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:inter.properties</value>
<value>classpath:email.properties</value>
</list>
</property>
</bean>
3.3.引入外部文件后,就可以在xml中用${key}替换指定的properties文件中的值,通常项目中都会将jdbc的配置放在properties文件中
<!-- 配置dbcp数据源 -->
<bean id="dataSourceDefault" class="org.apache.commons.dbcp.BasicDataSource">
<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>
在启动容器时,初始化bean时,${key}就会替换成properties文件中的值
方法二
为简化PropertyPlaceholderConfigurer的使用,Spring提供了context:property-placeholder/元素,启用它后,开发者便不用配置PropertyPlaceholderConfigurer对象了
3.1编写.properties文件
/jdbc.properties 文件
jdbc.url=jdbc:mysql://66.59.208.106:3306/ds?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
jdbc.username=root
jdbc.password=root
3.2.配置属性文件的位置
<!-- 数据库配置文件位置 -->
<context:property-placeholder location="classpath:jdbc.properties" />
PropertyPlaceholderConfigurer内置的功能非常丰富,如果它未找到${xxx}中定义的xxx键,它还会去JVM系统属性(System.getProperty())和环境变量(System.getenv())中寻找。通过启用systemPropertiesMode和searchSystemEnvironment属性,开发者能够控制这一行为。context:property-placeholder大大的方便了我们数据库的配置。这样就可以为spring配置的bean的属性设置值了
3.3.配置bean的属性
<!-- 配置dbcp数据源 -->
<bean id="dataSourceDefault" class="org.apache.commons.dbcp.BasicDataSource">
<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>
注:spring容器中最多只能定义一个context:property-placeholder,否则会报错:Could not resolve placeholder XXX,但如果想引入多个属性文件怎么办那,可以使用通配符:<context:property-placeholder location=“classpath*:conf*.properties”/>
4.自定义 PropertyPlaceholderConfigurer
继承PropertyPlaceholderConfigurer 类,重写processProperties()方法,之所以自定义,很有可能是因为java代码中使用了属性值,那些经常变得值,但又是常量,但是这样配置的话,如果属性文件中的属性值改变了,必须重启容器,因为容器启动时,已经将属性值初始化进了代码中就不变了
public class PropertyPlaceholder extends PropertyPlaceholderConfigurer {
private static Map<String,String> propertyMap;
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
propertyMap = new HashMap<String, String>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
propertyMap.put(keyStr, value);
}
}
//自定义一个方法,即根据key拿属性值,方便java代码中取属性值
public static String getProperty(String name) {
return propertyMap.get(name);
}
}
在创建bean时就会调用processProperties()方法,属性文件中设置的键值对都放在了Properties中
自定义了PropertyPlaceholderConfigurer类之后,在xml中配置时,配置bean中的class就必须设置为自定义的类(包路径+类名),不能再写为PropertyPlaceholderConfigurer类,否则自定义的方法不能使用,因为根本没有创建自定义类的bean