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注解的更多相关文章

  1. Spring源码窥探之:注解方式的AOP原理

    AOP入口代码分析 通过注解的方式来实现AOP1. @EnableAspectJAutoProxy通过@Import注解向容器中注入了AspectJAutoProxyRegistrar这个类,而它在容 ...

  2. Spring源码分析之AOP从解析到调用

    正文: 在上一篇,我们对IOC核心部分流程已经分析完毕,相信小伙伴们有所收获,从这一篇开始,我们将会踏上新的旅程,即Spring的另一核心:AOP! 首先,为了让大家能更有效的理解AOP,先带大家过一 ...

  3. Spring源码情操陶冶-AOP之Advice通知类解析与使用

    阅读本文请先稍微浏览下上篇文章Spring源码情操陶冶-AOP之ConfigBeanDefinitionParser解析器,本文则对aop模式的通知类作简单的分析 入口 根据前文讲解,我们知道通知类的 ...

  4. spring源码分析(二)Aop

    创建日期:2016.08.19 修改日期:2016.08.20-2016.08.21 交流QQ:992591601 参考资料:<spring源码深度解析>.<spring技术内幕&g ...

  5. Spring源码情操陶冶-AOP之ConfigBeanDefinitionParser解析器

    aop-Aspect Oriented Programming,面向切面编程.根据百度百科的解释,其通过预编译方式和运行期动态代理实现程序功能的一种技术.主要目的是为了程序间的解耦,常用于日志记录.事 ...

  6. spring源码学习之AOP(一)

    继续源码学习,看了spring中基础的容器和AOP感觉自己也没有什么长进,哈哈,我也不知道到底有用没有,这可能是培养自己的一种精神吧,不管那么多,继续学习!AOP中 AOP中几个重要的概念:(1)Ad ...

  7. Spring源码情操陶冶-AnnotationConfigBeanDefinitionParser注解配置解析器

    本文承接前文Spring源码情操陶冶-自定义节点的解析,分析spring中的context:annotation-config节点如何被解析 源码概览 对BeanDefinitionParser接口的 ...

  8. spring源码学习之AOP(二)

    接着上一篇中的内容! 3.创建代理 在获取了所有的bean对应的增强器之后,便可以进行代理的创建了org.springframework.aop.framework.autoproxy包下的Abstr ...

  9. Spring源码分析-从@ComponentScan注解配置包扫描路径到IoC容器中的BeanDefinition,经历了什么(一)?

    阅前提醒 全文较长,建议沉下心来慢慢阅读,最好是打开Idea,点开Spring源码,跟着下文一步一步阅读,更加便于理解.由于笔者水平优先,编写时间仓促,文中难免会出现一些错误或者不准确的地方,恳请各位 ...

  10. Spring源码情操陶冶-PropertyPlaceholderBeanDefinitionParser注解配置解析器

    本文针对spring配置的context:property-placeholder作下简单的分析,承接前文Spring源码情操陶冶-自定义节点的解析 spring配置文件应用 <context: ...

随机推荐

  1. 测试代码的练习2——python编程从入门到实践

    11-3 雇员:编写一个名为Employee的类,其方法__init__() 接受名.姓和年薪,并将它们都存储在属性中.编写一个名为give_raise()的方法,它默认将年薪增加5000美元,但也能 ...

  2. DS AVL树详解

    先说说二叉搜索树: 是有序的二叉树,根值>左节点值,右节点值>根值. 如果要查找某个值,二叉搜索树和二分查找一样,每进行一次值比较,就会减少一半的遍历区间. 但是,如果树插入的值一直递增/ ...

  3. 法那科 三菱 CNC虚拟机

    有虚拟机,就不用去线上 接线调机了,影响生产,还怕搞坏机子,很方便.

  4. pytest_pytest-html生成html报告

    前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...

  5. Oracle各种连接函数总结

    1.前言 Oracle可用连接函数会介绍以下几个 Oracle列转行函数 Listagg() strcat() wmsys.wm_concat() 2.Oracle列转行函数 Listagg() 2. ...

  6. Android为TV端助力之QQ空间热更新技术

    直接上代码 package com.enjoy.patch; import android.content.Context;import android.os.Build;import android ...

  7. Android为TV端助力之无法依赖constraint-layout:1.1.3(转发)

    原文地址 http://fanjiajia.cn/2018/09/25/Android%20Studio%20Could%20not%20resolve%20com.android.support.c ...

  8. 图说jdk1.8新特性(3)--- 注解与类型推测优化

    获取同一类型多个注解 ​ jdk1.8的java.lang.Class类新增了方法getAnnotationsByType方法,该方法可以获取某一个类型的注解列表,具体代码示例如下: public c ...

  9. 使用EwoMail搭建属于自己的个人邮件服务器——超详细图文教程

    版权声明:本文为CSDN博主「C_成」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/qq_41692307 ...

  10. Hibernate配置文件模版

    hibernate.cfg.xml 配置文件模版: <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-config ...