Spring生命周期-BeanFactoryPostProcessor和BeanPostProcessor
Posted OkidoGreen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring生命周期-BeanFactoryPostProcessor和BeanPostProcessor相关的知识,希望对你有一定的参考价值。
BeanFactoryPostProcessor和BeanPostProcessor,这两个接口,都是Spring初始化bean时对外暴露的扩展点。两个接口名称看起来很相似,但作用及使用场景却不同,分析如下:
1、BeanFactoryPostProcessor接口
该接口的定义如下:
[java] view plain copy
- 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;
注意:BeanFactoryPostProcessor是在spring容器加载了bean的定义文件之后,在bean实例化之前执行的。接口方法的入参是ConfigurrableListableBeanFactory,使用该参数,可以获取到相关bean的定义信息,例子:
1)spring bean的定义:
[html] view plain copy
- <?xml version="1.0" encoding="UTF-8" ?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
- xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
- default-autowire="byName">
- <bean id="myJavaBean" class="com.ali.caihj.postprocessor.MyJavaBean">
- <property name="desc" value="测试一下啦" />
- <property name="remark" value="这是备注信息啦啦啦" />
- </bean>
- <bean id="myBeanFactoryPostProcessor" class="com.ali.caihj.postprocessor.MyBeanFactoryPostProcessor" />
- </beans>
2)自定义的BeanFactoryPostProcessor:
[java] view plain copy
- public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor
- public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
- System.out.println("调用MyBeanFactoryPostProcessor的postProcessBeanFactory");
- BeanDefinition bd = beanFactory.getBeanDefinition("myJavaBean");
- System.out.println("属性值============" + bd.getPropertyValues().toString());
- MutablePropertyValues pv = bd.getPropertyValues();
- if (pv.contains("remark"))
- pv.addPropertyValue("remark", "把备注信息修改一下");
- bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
- org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
- org.springframework.beans.factory.config.PropertyOverrideConfigurer
- org.springframework.beans.factory.config.CustomEditorConfigurer:用来注册自定义的属性编辑器
2、BeanPostProcessor接口
该接口的定义如下:
[java] view plain copy
- public interface BeanPostProcessor
- /**
- * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
- * initialization callbacks (like InitializingBean's <code>afterPropertiesSet</code>
- * or a custom init-method). The bean will already be populated with property values.
- * The returned bean instance may be a wrapper around the original.
- * @param bean the new bean instance
- * @param beanName the name of the bean
- * @return the bean instance to use, either the original or a wrapped one
- * @throws org.springframework.beans.BeansException in case of errors
- * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
- */
- Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
- /**
- * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean
- * initialization callbacks (like InitializingBean's <code>afterPropertiesSet</code>
- * or a custom init-method). The bean will already be populated with property values.
- * The returned bean instance may be a wrapper around the original.
- * <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
- * instance and the objects created by the FactoryBean (as of Spring 2.0). The
- * post-processor can decide whether to apply to either the FactoryBean or created
- * objects or both through corresponding <code>bean instanceof FactoryBean</code> checks.
- * <p>This callback will also be invoked after a short-circuiting triggered by a
- * @link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation method,
- * in contrast to all other BeanPostProcessor callbacks.
- * @param bean the new bean instance
- * @param beanName the name of the bean
- * @return the bean instance to use, either the original or a wrapped one
- * @throws org.springframework.beans.BeansException in case of errors
- spring bean的生命周期是怎样的,代码示例