一般的Bean对象都是被定义在*.xml中的,xml中的各个bean的属性注入非常方便。
但是有些时候在阅读xml的时候就非常的费劲儿,尤其是在文件中定义了很多内容。
有的定义好了,基本不会改变,有的就需要改变。这个时候可以使用BeanFactoryPostProcessor接口来解决。
*.beans.factory.config.BeanFactoryPostProcessor;这个名称的意思说当依赖注入到BeanFactory之后可以进行处理,具体怎么处理看情况而定。
*.beans.factory.config.PropertyPlaceholderConfigurer实现了BeanFactoryPostProcessor接口。
就可以用它来完成。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>hello.properties</value>//资源文件名称 </property> </bean> <bean id="helloBean" class="com.baidu.HelloBean"> <property name="helloWord"> <value>${helloWord}</value>//资源文件属性名 </property> .... </bean> ...... </beans> PropertyPlaceholderConfigurer类会读取hello.properties文件到本地location,并将读取到的资源文件属性值赋值给helloWord。从而完成依赖注入
另一种情况是完成高权限的统一。spring提供了一个BeanFactoryPostProcessor实现类。
*.beans.factory.config.PropertyOverrideConfigurer;
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="configBean" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer"> <property name="location"> <value>hello.properties</value> </property> </bean> <bean id="helloBean" class="com.baidu.HelloBean"> <property name="helloWord"> <value>Hello!caterpillar!</value> </property> .... </bean> .... </beans> helloBean.helloWord=Hello!Justin!