BeanPostProcessor是spring框架提供的一个扩展点。
通过实现这个接口,程序员可插手bean实例化的过程。且这个接口可以设置多个,会形成一个列表,依次执行。
AOP正是通过BeanPostProcessor和IOC容器建立起了联系。
public interface BeanPostProcessor { default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { return bean; } default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } }
它有两个方法postProcessBeforeInitialization
,postProcessAfterInitialization
。
分别在bean初始化之前和之后执行。
执行顺序如下:
1,首先执行bean的构造方法,
2,BeanPostProcessor的postProcessBeforeInitialization
方法
3,InitializingBean的afterPropertiesSet方法
4,initMethod方法
5,BeanPostProcessor的postProcessAfterInitialization
方法
示例:
package com.test2; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan("com.test2") public class AppConfig { }
package com.test2; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @Component public class Bean1 { public Bean1(){ System.out.println("Bean1 构造"); } @PostConstruct public void init(){ System.out.println("Bean1 init"); } }
package com.test2; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.context.annotation.ComponentScan; import org.springframework.core.PriorityOrdered; import org.springframework.stereotype.Component; @Component public class MyBeanPostProcessor implements BeanPostProcessor,PriorityOrdered { public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println(beanName + " postProcessAfterInitialization"); return bean; } public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println(beanName + " postProcessBeforeInitialization"); return bean; } public int getOrder() { return 11; //值越小越先执行 } }
package com.test2; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.core.PriorityOrdered; import org.springframework.stereotype.Component; @Component public class MyBeanPostProcessor1 implements BeanPostProcessor,PriorityOrdered { public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println(beanName + " postProcessAfterInitialization1"); return bean; } public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println(beanName + " postProcessBeforeInitialization1"); return bean; } public int getOrder() { return 10; } }
package com.test2; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class MyTest { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); System.out.println(context.getBean("bean1")); } }
执行结果:
appConfig postProcessBeforeInitialization1
appConfig postProcessBeforeInitialization
appConfig postProcessAfterInitialization1
appConfig postProcessAfterInitialization
Bean1 构造
bean1 postProcessBeforeInitialization1
bean1 postProcessBeforeInitialization
Bean1 init
bean1 postProcessAfterInitialization1
bean1 postProcessAfterInitialization
com.test2.Bean1@6e38921c