/** * 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. * @parambeanFactory the bean factory used by the application context * @throwsorg.springframework.beans.BeansException in case of errors*/voidpostProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
/**
* 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
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
* @see org.springframework.beans.factory.FactoryBean
*/
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("1. 调用BeanFactoryPostProcessor的postProcessBeanFactory");
BeanDefinition bd = beanFactory.getBeanDefinition("myJavaBean");
MutablePropertyValues pv = bd.getPropertyValues();
pv.addPropertyValue("remark", "BeanFactoryPostProcessor中修改之后的remark");
}
}
public class MyBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if ("myJavaBean".equals(beanName)){
System.out.println("3. 调用BeanPostProcessor.postProcessBeforeInitialization");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if ("myJavaBean".equals(beanName)){
System.out.println("7. 调用BeanPostProcessor.postProcessAfterInitialization");
}
return bean;
}
}
@Configuration
public class MyConfig {
@Bean(initMethod = "initMethod", destroyMethod = "destroyMethod")
public MyJavaBean myJavaBean() {
MyJavaBean bean = new MyJavaBean();
bean.setRemark("原始remark");
return bean;
}
@Bean
public MyBeanFactoryPostProcessor myBeanFactoryPostProcessor(){
return new MyBeanFactoryPostProcessor();
}
@Bean
public MyBeanPostProcessor myBeanPostProcessor(){
return new MyBeanPostProcessor();
}
}
public class MyJavaBean implements InitializingBean, DisposableBean {
private String desc;
private String remark;
public MyJavaBean() {
System.out.println("2 Bean的无参构造函数");
}
public MyJavaBean(String desc, String remark) {
this.desc = desc;
this.remark = remark;
System.out.println("2 Bean的全参构造函数");
}
@PostConstruct
public void postConstruct() {
System.out.println("4. 调用PostConstruct方法");
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
System.out.println("调用setDesc方法");
this.desc = desc;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
System.out.println("调用setRemark方法");
this.remark = remark;
System.out.println("remark = " + this.remark);
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("5. 调用InitializingBean.afterPropertiesSet方法");
this.desc = "afterPropertiesSet中修改之后的desc";
}
public void initMethod() {
System.out.println("6. 调用Bean的init方法");
}
@Override
public void destroy() throws Exception {
System.out.println("8. 调用DisposableBean.destroy");
}
public void destroyMethod() {
System.out.println("9. 调用Bean的destroy方法");
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("[desc:").append(desc);
builder.append(", remark:").append(remark).append("]");
return builder.toString();
}
}