Spring源码窥探之:AOP注解
AOP也就是我们日常说的@面向切面编程,看概念比较晦涩难懂,难懂的是设计理念,以及这样设计的好处是什么。在Spring的AOP中,常用的几个注解如下:@Aspect,@Before,@After,@AfterReturning,@AfterThrowing,@Around,@PointCut以及@EnableAspectJAutoProxy
1. 模拟业务类CalculateService
/**
* description:模拟业务类
*
* @author 70KG
* @date 2018/12/17
*/
public class CalculateService { public int calculate(int i, int j) {
int result = i / j;
System.out.println("---->CalculateService-业务方法执行。。。。。。");
return result;
} }
2. 编写切面类,切面类必须标注它为切面类(@Aspect),关于切入点表达式,可以参考官方文档,写法很多种,还有关于JoinPoint这个参数必须放在第一位,否则报错。
/**
* description:日志切面类,JoinPoint必须在参数的第一位
*
* @author 70KG
* @date 2018/12/17
*/
@Aspect // ---> 声明此类是一个切面类
public class LogAdvice { @Pointcut("execution(* com.nmys.story.springCore.springaop.sample01.CalculateService.*(..))")
public void pointCut() {
} /**
* 目标方法执行前执行
*/
@Before("pointCut()")
public void logBefore(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
Object[] args = joinPoint.getArgs();
System.out.println("---->" + signature + "方法执行前,传入的参数是:" + Arrays.asList(args));
} /**
* 目标方法执行后执行
*/
@After("pointCut()")
public void logAfter(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
Object[] args = joinPoint.getArgs();
System.out.println("---->" + signature + "方法执行后,传入的参数是:" + Arrays.asList(args)); } /**
* 方法发生异常时执行
*/
@AfterThrowing(value = "pointCut()", throwing = "ex")
public void logException(JoinPoint joinPoint, Exception ex) {
Signature signature = joinPoint.getSignature();
System.out.println("---->" + signature + "方法执行后,抛出了异常信息:" + ex.getMessage());
} /**
* 正常返回时执行
*/
@AfterReturning(value = "pointCut()", returning = "result")
public void logReturn(JoinPoint joinPoint, Object result) {
Signature signature = joinPoint.getSignature();
System.out.println("---->" + signature + "方法执行后,返回的结果是:" + result);
} }
3. 配置类
/**
* description
*
* @author 70KG
* @date 2018/12/17
*/
@Configuration
@EnableAspectJAutoProxy // ---> 开启切面的自动代理
public class MyConfig { @Bean
public CalculateService calculateService() {
return new CalculateService();
} // 将切面类也交给容器管理
@Bean
public LogAdvice logAdvice() {
return new LogAdvice();
} }
4. 测试类
/**
* description
*
* @author 70KG
* @date 2018/12/17
*/
public class Test01 { public static void main(String[] args) { // 加载配置文件
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MyConfig.class);
CalculateService calc = (CalculateService) ac.getBean("calculateService");
calc.calculate(4,2);
} }
5. 结果
---->int com.nmys.story.springCore.springaop.sample01.CalculateService.calculate(int,int)方法执行前,传入的参数是:[4, 2]
---->CalculateService-业务方法执行。。。。。。
---->int com.nmys.story.springCore.springaop.sample01.CalculateService.calculate(int,int)方法执行后,传入的参数是:[4, 2]
---->int com.nmys.story.springCore.springaop.sample01.CalculateService.calculate(int,int)方法执行后,返回的结果是:2
Spring源码窥探之:AOP注解的更多相关文章
- Spring源码窥探之:注解方式的AOP原理
AOP入口代码分析 通过注解的方式来实现AOP1. @EnableAspectJAutoProxy通过@Import注解向容器中注入了AspectJAutoProxyRegistrar这个类,而它在容 ...
- Spring源码分析之AOP从解析到调用
正文: 在上一篇,我们对IOC核心部分流程已经分析完毕,相信小伙伴们有所收获,从这一篇开始,我们将会踏上新的旅程,即Spring的另一核心:AOP! 首先,为了让大家能更有效的理解AOP,先带大家过一 ...
- Spring源码情操陶冶-AOP之Advice通知类解析与使用
阅读本文请先稍微浏览下上篇文章Spring源码情操陶冶-AOP之ConfigBeanDefinitionParser解析器,本文则对aop模式的通知类作简单的分析 入口 根据前文讲解,我们知道通知类的 ...
- spring源码分析(二)Aop
创建日期:2016.08.19 修改日期:2016.08.20-2016.08.21 交流QQ:992591601 参考资料:<spring源码深度解析>.<spring技术内幕&g ...
- Spring源码情操陶冶-AOP之ConfigBeanDefinitionParser解析器
aop-Aspect Oriented Programming,面向切面编程.根据百度百科的解释,其通过预编译方式和运行期动态代理实现程序功能的一种技术.主要目的是为了程序间的解耦,常用于日志记录.事 ...
- spring源码学习之AOP(一)
继续源码学习,看了spring中基础的容器和AOP感觉自己也没有什么长进,哈哈,我也不知道到底有用没有,这可能是培养自己的一种精神吧,不管那么多,继续学习!AOP中 AOP中几个重要的概念:(1)Ad ...
- Spring源码情操陶冶-AnnotationConfigBeanDefinitionParser注解配置解析器
本文承接前文Spring源码情操陶冶-自定义节点的解析,分析spring中的context:annotation-config节点如何被解析 源码概览 对BeanDefinitionParser接口的 ...
- spring源码学习之AOP(二)
接着上一篇中的内容! 3.创建代理 在获取了所有的bean对应的增强器之后,便可以进行代理的创建了org.springframework.aop.framework.autoproxy包下的Abstr ...
- Spring源码分析-从@ComponentScan注解配置包扫描路径到IoC容器中的BeanDefinition,经历了什么(一)?
阅前提醒 全文较长,建议沉下心来慢慢阅读,最好是打开Idea,点开Spring源码,跟着下文一步一步阅读,更加便于理解.由于笔者水平优先,编写时间仓促,文中难免会出现一些错误或者不准确的地方,恳请各位 ...
- Spring源码情操陶冶-PropertyPlaceholderBeanDefinitionParser注解配置解析器
本文针对spring配置的context:property-placeholder作下简单的分析,承接前文Spring源码情操陶冶-自定义节点的解析 spring配置文件应用 <context: ...
随机推荐
- Git config文件
查看该文件: git config --global --list Ref: https://blog.csdn.net/themagickeyjianan/article/details/79683 ...
- Graph machine learning 工具
OGB: Open Graph Benchmark https://ogb.stanford.edu/ https://github.com/snap-stanford/ogb OGB is a co ...
- Java基础知识点总结(三)
figure:first-child { margin-top: -20px; } #write ol, #write ul { position: relative; } img { max-wid ...
- setdefault函数的用法及理解
setdefault函数的用法及理解 dict.setdefault(key, default=None) 功能:如果键不存在于字典中,将会添加该键并将default的值设为该键的默认值,如果键存在于 ...
- Ideaui和WebStrom2019最新版自动生成破解码
http://idea.medeming.com/jet/ 直接点击下载 6ZUMD7WWWU-eyJsaWNlbnNlSWQiOiI2WlVNRDdXV1dVIiwibGljZW5zZWVOYW1l ...
- POI2015 WYC
也许更好的阅读体验 \(\mathcal{Description}\) 给定一张n个点m条边的带权有向图,每条边的边权只可能是1,2,3中的一种.将所有可能的路径按路径长度排序,请输出第k小的路径的长 ...
- Java中Date、String、Calendar类型之间的转化
1.Calendar 转化 String //获取当前时间的具体情况,如年,月,日,week,date,分,秒等 Calendar calendat = Calendar.getInstanc ...
- js调用浏览器下载
$scope.Download = function (url) { var save_link = document.createElementNS("http://www.w3.org/ ...
- Lipo移除ORC架构
Lipo移除ORC架构 打包前检查链接 https://cloud.baidu.com/doc/OCR/OCR-iOS-SDK.html#FAQ cd /Users/guojun/JG-iOS/Pro ...
- JUC - ThreadPoolExecutor
JUC - ThreadPoolExecutor 创建一个ThreadPoolExecutor ThreadPoolExecutor( int corePoolSize, // 保留在池中的线程数,即 ...