Spring BeanPostProcessor执行时机和Bean的初始化时机总结
来源:互联网 发布:网络教育学生档案 编辑:程序博客网 时间:2024/06/06 09:22
Spring BeanPostProcessor:针对所有Spring上下文中所有的bean,
可以在配置文档applicationContext.xml中配置一个BeanPostProcessor,然后对所有的bean进行一个初始化之前和之后的代理
Bean初始化执行的顺序:先执行BeanPostProcessor实现InitializingBean 后执行afterPropertiesSet方法
然后执行init-method方法
测试用例:
xml文件配置:
<bean id="testBean" class="com.processor.TestBean" init-method="init" lazy-init="false"/><bean id="testProcessor" class="com.processor.TestBeanProcessor"/><bean id="MyBeanFactory" class="com.processor.MyBeanFactory"/><aop:aspectj-autoproxy/><bean id="testAsBean" class="com.aspectJ.ExampleBean"/><bean id="testAdvice" class="com.aspectJ.TestAdvice"/><bean id="testAdvice2" class="com.aspectJ.TestAdvice2"/>
测试的Bean实例
public class TestBean implements InitializingBean { String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void init() { System.out.println("intMethod is called"); } @PostConstruct //j2EE的注解。现在不生效 public void initPost() { System.out.println("PostConstruct is called"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("afterPropertiesSet is called"); }}BeanPostProcessor的编写
public class TestBeanProcessor implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessBeforeInitialization" + beanName + " is called"); if (bean.getClass().isAnnotationPresent(TypeAnnotation.class)) { return bean; } return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessAfterInitialization" + beanName + " is called"); return bean; }}
Junit测试用例
@Testpublic void testDubbo() { ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml"); People p = (People) ctx.getBean("cutesource"); System.out.println(p.getId()); System.out.println(p.getName()); System.out.println(p.getAge());}
postProcessBeforeInitialization cutesource is called
postProcessAfterInitialization cutesource is called
postProcessBeforeInitialization testBean is called
afterPropertiesSet is called
intMethod is called
postProcessAfterInitialization testBean is called
postProcessBeforeInitialization MyBeanFactory is called
postProcessAfterInitialization MyBeanFactory is called
postProcessBeforeInitialization testAsBean is called
postProcessAfterInitialization testAsBean is called
postProcessBeforeInitialization testAdvice is called
postProcessAfterInitialization testAdvice is called
postProcessBeforeInitialization testAdvice2 is called
postProcessAfterInitialization testAdvice2 is called
cutesource
aaa
27
postProcessBeforeInitialization testBean is called
afterPropertiesSet is called
intMethod is called
postProcessAfterInitialization testBean is called
postProcessBeforeInitialization MyBeanFactory is called
postProcessAfterInitialization MyBeanFactory is called
postProcessBeforeInitialization testAsBean is called
postProcessAfterInitialization testAsBean is called
postProcessBeforeInitialization testAdvice is called
postProcessAfterInitialization testAdvice is called
postProcessBeforeInitialization testAdvice2 is called
postProcessAfterInitialization testAdvice2 is called
cutesource
aaa
27
对每一个单例Bean的初始化都会 执行 BeanPostProcessor 的两个方法。调用时机通过查看源码
在:
AbstractAutowireCapableBeanFactory 中的 initializeBean()中,这个方法里会调用
applyBeanPostProcessorsBeforeInitialization()和applyBeanPostProcessorsAfterInitialization()方法
同时有invokeInitMethods()方法。这个方法里会调用afterPropertiesSet()
和invoke反射执行init-method方法.
阅读全文
0 0
- Spring BeanPostProcessor执行时机和Bean的初始化时机总结
- spring bean初始化时机
- Spring中bean初始化时机初始化方法和销毁方法
- Spring 创建bean的时机
- Spring BeanFactoryPostProcessor Bean后处理器 执行时机
- spring 实例初始化时机
- Spring的IOC、Spring对象初始化bean时机、Spring容器生命周期
- Spring的IOC、Spring对象初始化bean时机、Spring容器生命周期
- spring框架bean注入的时机问题
- Spring创建对象初始化bean的时机分为两种形式:
- 类的初始化时机
- 类的初始化时机
- 类初始化的时机
- 类初始化的时机
- viewDidUnload和dealloc的执行时机
- viewDidUnload和dealloc的执行时机
- java类的加载 初始化时机,已经静态代码块的执行时机
- spring实例化bean的时机以及struts2实例化action的时机
- jni 函数签名规则
- Android Studio连接夜神模拟器
- How Much Do You Know About 100G DWDM Transceiver?
- linux下获取记录文件的前N行或后N行
- ims Registered and Volte enable on Android N
- Spring BeanPostProcessor执行时机和Bean的初始化时机总结
- HIVE MapJoin异常问题处理总结
- Windows Server 2012 解决无法连接无线网络
- maven 配置多模块项目 pom modules
- Android 最完美的取色盘ColorPickView
- Git实践总结
- 不是中医粉,我为什么做了一个程序员中医调理的APP?
- 谈谈Linux下动态库查找路径的问题
- Python整理