Spring AOP在Bean生命周期中的调用时机
来源:互联网 发布:变革管理 知乎 编辑:程序博客网 时间:2024/06/07 03:16
之前有写了一个生命周期的例子,直接拿来用,在每个生命周期方法中调用print方法。见上一篇
加上AOP的代码
package com.aspect;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.springframework.stereotype.Component;@Aspect@Componentpublic class AroundAspect {@Around("execution(* com.bean.MyMode.*(..))")public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {String methodName = pjp.getSignature().getName();System.out.println("----Aspect before " + methodName + " called ");Object retVal = pjp.proceed();System.out.println("----Aspect after " + methodName + " called ");return retVal;}}
运行得到结果:
InstantiationBeanPostProcessor postProcessBeforeInstantiation called
MyMode constructor call
1
MyMode constructor finish
InstantiationBeanPostProcessor postProcessAfterInstantiation called
InstantiationBeanPostProcessor postProcessPropertyValues called
BeanPostProcessor postProcessBeforeInitialization called
1
BeanPostProcessor postProcessBeforeInitialization finish
Mode @PostConstruct anno init called
1
MyMode @PostConstruct anno init finish
Mode afterPropertiesSet called
1
MyMode afterPropertiesSet finish
Mode @Bean anno Init called
1
MyMode @Bean anno Initfinish
BeanPostProcessor postProcessAfterInitialization called
----Aspect before print called
1
----Aspect after print called
BeanPostProcessor postProcessAfterInitialization finish
----Aspect before print called
1
----Aspect after print called
Mode @PreDestroy anno destory called
1
MyMode @PostConstruct anno destory finish
Mode destroy called
1
MyMode destroy finish
Mode @Bean anno destory called
1
MyMode @Bean anno destory finish
结论是:在Bean的整个生命周期中,只有在初始化方法调用完之后销毁之前,AOP是有效的。
- Spring AOP在Bean生命周期中的调用时机
- Bean在Spring容器中的生命周期
- Bean在Spring容器中的生命周期
- Bean 在 Spring 容器中的生命周期
- Bean在Spring容器中的生命周期
- Bean在Spring容器中的生命周期
- Bean在Spring容器中的生命周期
- Bean(应用组件)在Spring容器中的生命周期
- spring中的bean的生命周期
- Spring Bean在BeanFactory生命周期
- spring bean初始化时机
- Spring之Bean生命周期方法调用
- Spring的IOC、Spring对象初始化bean时机、Spring容器生命周期
- Spring的IOC、Spring对象初始化bean时机、Spring容器生命周期
- Java EE - AOP 和 spring 的bean 生命周期
- Jsp调用Spring中的Bean
- Spring bean中的高级属性和生命周期
- Spring之Bean在BeanFactory的生命周期
- 项目1--图基本算法库
- c#中异步调用asyn
- Selenium快速入门
- SQL入门语句
- 巧妙地利用STL map set pair 贪心+排序 Codeforces Round #331 (Div. 2)C. Wilbur and Points
- Spring AOP在Bean生命周期中的调用时机
- IO端口与IO内存
- java中的排序方法
- 第12周、项目3 - 图遍历算法实现
- 线性表4 – 数据结构和算法09
- 浅谈基于Selenium的Web自动化测试框架
- HDU 1022 栈
- 第十二周项目1——图基本算法库
- 第十二周上机实践—项目3—图遍历算法实现