Spring AOP基于@AspectJ注解的切面
转载自 浅然的专栏 https://blog.csdn.net/w_linux/article/details/80230222
该篇博客讲述基于AspectJ的注解方式实现AOP切面,关于AOP的一些概念性问题可以转战https://blog.csdn.net/w_linux/article/details/80194768
该篇博客主要阐述
1、编写切点(@Pointcut)
2、基于AspectJ的注解方式实现日志打印
3、环绕通知用法
4、JoinPoint用法
一、编写切点(@Pointcut)
@Pointcut需要在切面中使用,如下
Pointcut定义时,还可以使用&&、||、! 这三个运算
编写切点表达式
AspectJ指示器
当我们查看这些Spring支持的指示器时,注意只有execution指示器是唯一的执行匹配,而其他的指示器都是用于限制匹配的。这说明execution指示器是我们在编写切点定义时最主要使用的指示器
二、基于AspectJ的注解方式实现日志打印
1、场景分析
在一个经典的业务逻辑(加减乘除)中需要每次执行某个方法时,打印一些增强该业务逻辑的功能,比如某个方法执行完毕后提示用户执行完毕等日志功能
2、解决方案
使用基于@AspectJ的注解方式来实现日志打印,既不用在核心业务方法中添加一些非核心代码,又能实现自己想要实现的功能,利用前置通知、后置通知、返回通知、异常通知实现
前提:使用AspectJ注解需要依赖于以下几个类库
- aspectjweaver-1.6.10.jar
- spring-aop-4.3.10.RELEASE.jar
- spring-aspects-4.3.10.RELEASE.jar
当然最省力的办法就是将所有Spring的jar都加到classpath中
3、代码
Arithmetic.java(核心业务代码)
package com.linjie.aop; import org.springframework.stereotype.Component; /** * @author LinJie E-mail:ash-ali@163.com * @version 创建时间:2018年5月6日 下午7:53:56 * @核心业务:基本运算 */ @Component("arithmetic") public class Arithmetic { //add public int add(int d,int e) { System.out.println("add method END!"); System.out.println(); return d+e; } //subtraction public int sub(int a,int b) { System.out.println("sub method END!"); System.out.println(); return a-b; } //multiplicative public int mul(int a,int b) { System.out.println("mul method END!"); System.out.println(); return a*b; } //division public int div(int a,int b) { System.out.println("div method END!"); System.out.println(); return a/b; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
LogginAspectJ.java(基于AspectJ的注解方式的切面——日志)
package com.linjie.aop; import java.util.Arrays; import org.aopalliance.intercept.Joinpoint; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; /** * @author LinJie * log功能,不影响核心业务 */ @Component("logginAspectJ") @Aspect public class LogginAspectJ { /* *定义一个方法,用于声明切点表达式,该方法一般没有方法体 *@Pointcut用来声明切点表达式 *通知直接使用定义的方法名即可引入当前的切点表达式 */ @Pointcut("execution(* com.linjie.aop.Arithmetic.*(..))") public void PointcutDeclaration() {} //前置通知,方法执行之前执行 @Before("PointcutDeclaration()") public void BeforeMethod(JoinPoint jp) { String methodName = jp.getSignature().getName(); Object[] args = jp.getArgs(); System.out.println("BeforeMethod The method "+ methodName +" parameter is "+ Arrays.asList(args)); System.out.println("add before"); System.out.println(); } //后置通知,方法执行之后执行(不管是否发生异常) @After("PointcutDeclaration()") public void AfterMethod(JoinPoint jp) { String methodName = jp.getSignature().getName(); Object[] args = jp.getArgs(); System.out.println("AfterMethod The method "+ methodName +" parameter is "+Arrays.asList(args)); System.out.println(); } //返回通知,方法正常执行完毕之后执行 @AfterReturning(value="PointcutDeclaration()",returning="result") public void AfterReturningMethod(JoinPoint jp,Object result) { String methodName = jp.getSignature().getName(); Object[] args = jp.getArgs(); System.out.println("AfterReturningMethod The method "+ methodName +" parameter is "+Arrays.asList(args)+" "+result); System.out.println(); } //异常通知,在方法抛出异常之后执行 @AfterThrowing(value="PointcutDeclaration()",throwing="e") public void AfterThrowingMethod(JoinPoint jp,Exception e) { String methodName = jp.getSignature().getName(); System.out.println("AfterThrowingMethod The method "+ methodName +"exception :"+e); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
applicationContext.xml(用于配置Bean和aop切面)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 扫描 --> <context:component-scan base-package="com.linjie.aop"></context:component-scan> <!-- 使AspectJ注解自动为匹配的类生成代理对象 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
在Spring IoC容器中启动AspectJ注解支持,只要在Bean的配置文件中定义一个空的XML元素
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
- 1
测试类
package com.linjie.aop; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author LinJie * 测试 */ public class SpringTest { @Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Arithmetic arithmetic = (Arithmetic) context.getBean("arithmetic"); int result = arithmetic.add(1, 2); System.out.println("result: "+result); System.out.println("--------------"); result = arithmetic.div(8, 1); System.out.println("result: "+result); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
运行结果
三、环绕通知用法
环绕通知是最为强大的通知,这里与以上的切面不同以外其他都相同
四、JoinPoint用法
可以在上面的切面代码中发现参数JoinPoint,下面就阐述下该参数的用法
JoinPoint对象封装了SpringAop中切面方法的信息,在切面方法中添加JoinPoint参数,就可以获取到封装了该方法信息的JoinPoint对象.
常用方法
ProceedingJoinPoint对象
ProceedingJoinPoint对象是JoinPoint的子接口,该对象用在@Around的切面方法中,常用有以下两个方法
参考
《Spring IN ACTION》
- 点赞
- 收藏
- 分享
- 文章举报
- 面向切面编程AOP:基于注解的配置
- aop注解 自定义切面的注解写法
- spring切面:注解:抛出异常增强
- 使用自定义注解和切面记录系统日志
- Spring-用 AspectJ 注解声明切面
- Aspect注解实现简单的切面
- Spring整理11 -- 面对切面(AOP)1 -- 注解配置的方式实现AOP
- Spring配置文件使用@Value注解注入值的方法,以及AOP切面在Spring+SpringMVC中的不生效解决
- Spring4深入理解AOP02----AOP简介,AspectJ,AOP基于注解和XML配置(5种通知,切面优先级)
- Spring笔记 #02# 利用切面和注解校验方法参数
- spring切点切面aop——xml非注解
- springboot+自定义注解实现灵活的切面配置
- 基于注解的切面编程
- spring切面:注解:最终增强
- spring aop使用注解从切面中获取参数
- Spring 通过注解方式实现AOP切面编程
- Spring实战-注解切面(五)
- Spring 实战-第四章-4.3使用注解创建切面
- spring切点切面aop——xml非注解