public interface BeanFactoryPostProcessor { /** * Modify the application context's internal bean factory after its standard * initialization. All bean definitions will have been loaded, but no beans * will have been instantiated yet. This allows for overriding or adding * properties even to eager-initializing beans. * @param beanFactory the bean factory used by the application context * @throws org.springframework.beans.BeansException in case of errors */ void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException; }
package com.test.spring; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; /** * bean后置处理器 * @author zss * */ public class PostProcessor implements BeanPostProcessor{ @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("后置处理器处理bean=【"+beanName+"】开始"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("后置处理器处理bean=【"+beanName+"】完毕!"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return bean; } }
这个大概看一下即可,主要关注它继承了谁,并且关注他的日志,BeanFactoryPostProcessor.java
这个就是一个普通的测试类,关注一下他的属性。BeanFactoryPostProcessorTest.java
Test case:
package com.test.spring; import org.junit.Before; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class T { ApplicationContext applicationcontext=null; @Before public void before() { System.out.println("》》》Spring ApplicationContext容器开始初始化了......"); applicationcontext= new ClassPathXmlApplicationContext(new String[]{"spring.xml"}); System.out.println("》》》Spring ApplicationContext容器初始化完毕了......"); } @Test public void test() { //BeanLifecycle beanLifecycle =applicationcontext.getBean("beanLifecycle",BeanLifecycle.class); BeanFactoryPostProcessorTest beanFactoryPostProcessorTest=applicationcontext.getBean(BeanFactoryPostProcessorTest.class); System.out.println(beanFactoryPostProcessorTest.toString()); } }
测试结果:
2017-03-20 14:36:10 INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@17ad352e: startup date [Mon Mar 20 14:36:10 CST 2017]; root of context hierarchy
2017-03-20 14:36:10 INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [spring-service.xml]
******调用了BeanFactoryPostProcessor
》》》修改了name属性初始值了
》》》调用了BeanNameAware的setBeanName方法了
》》》调用了BeanFactoryAware的setBeanFactory方法了
后置处理器处理bean=【beanFactoryPostProcessorTest】开始
后置处理器开始调用了
》》》调用了Initailization的afterPropertiesSet方法了
后置处理器处理bean=【beanFactoryPostProcessorTest】完毕!
后置处理器调用结束了
》》》Spring ApplicationContext容器初始化完毕了......
BeanFactoryPostProcessorTest [name=赵四, sex=男]
---------------------------------------------------------------------------------------------------------
从测试结果中可以看到beanFactoryPostProcessorTest定义的name值由" 张三"变为"赵四",同时发现postProcessorBeanFactory方法执行顺序先于BeanPostProcessor接口中方法。
<property name="name" value="张三"/>//这个是元数据
<property name="sex" value="男"/>//这个是元数据
</bean>
在Spring中内置了一些BeanFactoryPostProcessor实现类:
- org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
- org.springframework.beans.factory.config.PropertyOverrideConfigurer
- org.springframework.beans.factory.config.CustomEditorConfigurer:用来注册自定义的属性编辑器
db.password=tiger
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.
PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>userinfo.properties</value>
</list>
</property>
</bean>
<bean name="userInfo" class="test.UserInfo">
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
org.springframework.beans.factory.config.PropertyPlaceholderCVonfigurer
的Bean就会停止对剩余PropertyPlaceholderConfigurer
的扫描,即只能存在一个实例!
<context:property-placeholder location="" file-encoding="" ignore-resource-not-found="" ignore-unresolvable="" properties-ref="" local-override="" system-properties-mode="" order="" />
(1)location:表示属性文件位置,多个之间通过如逗号/分号等分隔;
(2)file-encoding:文件编码;
(3)ignore-resource-not-found:如果属性文件找不到,是否忽略,默认false,即不忽略,找不到将抛出异常
(4)ignore-unresolvable:是否忽略解析不到的属性,如果不忽略,找不到将抛出异常
(5)properties-ref:本地java.util.Properties配置
(6)local-override:是否本地覆盖模式,即如果true,那么properties-ref的属性将覆盖location加载的属性
(7)system-properties-mode:系统属性模式,ENVIRONMENT(默认),NEVER,OVERRIDE
(8)ENVIRONMENT:将使用Spring 3.1提供的PropertySourcesPlaceholderConfigurer,其他情况使用Spring 3.1之前的PropertyPlaceholderConfigurer
(9)OVERRIDE: PropertyPlaceholderConfigurer使用,因为在spring 3.1之前版本是没有Enviroment的,所以OVERRIDE是spring 3.1之前版本的Environment
(10)NEVER:只查找properties-ref、location;
(11)order:当配置多个<context:property-placeholder/>
时的查找顺序
使用注意:
1、location中的加载文件的顺序
如果location中有多个文件:
classpath:db.properties,classpath:default.properties,classpath:default3.properties,classpath:default2.properties
将依次加载,值得注意的是如果后一个文件中有和前面某一个文件中属性名是相同的,最终取的值是后加载的值
举例来说:
default.properties文件中有个属性名userId,其对应的值为-1
default2.properties文件中也有一个属性名userId,其对应的值为-2
default3.properties文件中特有一个属性名userId,其对于那个的值为-3
default.properties文件先加载,此时userId的值为-1,当default3.properties文件加载时将更新原来的值,此时userId的值为-3,同理,最后加载default2.properties文件,所以userId最终值为-2
所以需要避免不同属性文件中的属性名称重名
2.ignore-resource-not-found和ignore-unresolvable两个属性是类似的作用,推荐配对使用
如果location中的文件指向了一个不存在的文件,那么也极有可能意味着有属性无法解析(虽然存在其他属性文件中存在重名,但是这个是应该避免的)
所以当ignore-resource-not-found设为true时,ignore-unresolvable也必须设为true,其实当ignore-unresolvable设为true时,ignore-resource-not-found的值true或false,并不影响异常的抛出
如果设置为ture,后属性值无法解析成功,将赋值为${属性名}
不推荐将ignore-resource-not-found和ignore-unresolvable的值设置为ture,默认为false,可以有效避免程序运行异常
3.properties-ref属性
引入其他方式引入的属性文件
<bean id="refProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:default2.properties</value>
</list>
</property>
</bean>
该属性需要local-override配合使用,只有当local-override属性值为true时,properties-ref属性文件中属性值将覆盖location属性文件属性值(同名属性)
4.local-override属性
当local-override属性值为true时,properties-ref属性文件中属性值将覆盖location属性文件属性值(同名属性)
5.sytem-properties-mode属性
不同的sytem-properties-mode属性定义了不同的查找顺序
Environment环境:包括JDK环境,系统环境变量,Sevlet环境,Spring环境等,是Spring在3.1之后抽象的一个表示环境配置
在local-override属性值为false,sytem-properties-mode属性值为ENVIRONMENT或OVRRIDE时,查找顺序是location,然后是environment或者System.getProperty(),System.getenv()(Spring 3.1 之前)
即现加载location指向的属性文件,再加之environment指向的环境,当environment环境中存在和location指向的属性文件中同名的属性,则该属性的值将被修改,取决于environment环境中的值
如果sytem-properties-mode属性值为NEVER,则只查询location指向的属性文件
当local-override属性值为true时,最后将加载properties-ref指向的文件,如遇到同名的,该同名属性值将取决于properties-ref指向的文件中的值
所以,最终程序中获取的值将是一个综合作用后的值,一般情况下建议sytem-properties-mode属性值为NEVER避免ENVIRONMENT环境中的不可控