面向切面Spring AOP
面向切面编程,就是把一个方法的执行看过一个连续的过程,然后在这个连续过程中的某几个特殊的位置切入进行其他的操作。被切入的点我们称之为切入点JoinPoint
。
常用的切入点包括:
*前置通知:@Before:在目标方法运行之前运行
*后置通知:@After:在目标方法运行结束之后运行
*返回通知:@AfterReturning:在目标方法正常返回之后运行
*异常通知:@AfterThrowing:目标方法出现异常的时候运行
*环绕通知:@Around:动态代理,手动推进目标方法运行(joinPoint.processed())
aop应用实例:
我们以一个除法运算的方法为例进行切面编程,在这个除法方法的执行前,执行后,返回后,或者发生异常的地方作为切入点进行切入。在方法的执行前,通过@Before切入获取方法的参数列表;在方法执行正常返回后,通过@AfterReturning切入,获取方法执行的结果;如果发生异常,则通过@AfterThrowing,获取异常信息。
除法方法:
public class mathCalculator {
public int div(int a, int b){
return a/b;
}
}
切入类:
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import java.util.Arrays;
@Aspect
public class logAspects {
/*
* 抽取公共的切点表达式
* @Before("public int demo02AOP.mathCalculator.div(int int)")
* 上式中为了申明一个切点表达式写的很长,而且每一个切点@Before,@After等都要写一遍,故可以抽取出来
* demo02AOP.mathCalculator.*(*))中,星号“*”表示mathCalculator类里面的所有方法
* */
@Pointcut("execution(public int demo02AOP.mathCalculator.*(..))")
public void pointCut(){}
/*
* 参数列表中的JoinPoint就是获取切入点
* 通过JoinPoint可以得到切入点的信息
* */
@Before("pointCut()")
public void logStart(JoinPoint joinPoint){
System.out.println("logStart...");
System.out.println(" name: "+joinPoint.getSignature().getName());
System.out.println(" args: "+ Arrays.asList(joinPoint.getArgs()));
}
@After("pointCut()")
public void logEnd(){
System.out.println("logEnd...");
}
/*
* returning指明了获取切入点的方法运行结果
* 引号的里面的"result",就是在方法参数列表处声明的变量,
* 表示把切入方法执行完后returning的结果存储到这个result变量中
* */
@AfterReturning(value = "pointCut()", returning = "result")
public void logReturn(Object result){
System.out.println("logReturn...");
System.out.println(" result: "+ result);
}
@AfterThrowing(value = "pointCut()", throwing = "exception")
public void logException(Exception exception){
System.out.println("logException...");
System.out.println(" exception:"+exception);
}
}
配置类:
import demo02AOP.logAspects;
import demo02AOP.mathCalculator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@EnableAspectJAutoProxy
@Configuration
public class myConfigAOP {
@Bean
public mathCalculator mathCalculator(){
return new mathCalculator();
}
@Bean
public logAspects logAspects(){
return new logAspects();
}
}
结果测试:
import DemoAnnotation.Config.myConfigAOP;
import demo02AOP.mathCalculator;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class testAutowired {
@Test
public void test02AOP(){
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(myConfigAOP.class);
mathCalculator bean = context.getBean(mathCalculator.class);
int div = bean.div(6, 3);
System.out.println(div);
}
}
正常结果输出:
logStart...
name: div
args: [6, 3]
logEnd...
logReturn...
result: 2
2
Process finished with exit code 0
将除数改为0,异常输出如下:
logStart...
name: div
args: [6, 0]
logEnd...
logException...
exception:java.lang.ArithmeticException: / by zero
java.lang.ArithmeticException: / by zero
调试遇到的问题:
主要是依赖问题,由于没有导入aspectjrt
,导致一开始的@Before
等注解无法使用,IDEA自动将Junit的测试中的before导入了;由于没有导入aspectjweaver
,编译一直报错找不到weaver类;还有没有导入spring-aop
也报错了,具体pom.xml中的依赖如下:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
</dependencies>
aop使用总结:
- 切面类编写好后,要用
@Aspect
标记这个类为切面类,不然spring不知道。 - 切面类中的具体切入的方法,要用
@Before
,@After
…等标注好 - 可以抽取公共的切点表达式作为一个单独的方法,并将该方法标注为
@Pointcut
。例如:@Pointcut("execution(public int demo02AOP.mathCalculator.*(..))")
- 不光要在配置类中往容器中添加切面类,最重要的是在配置类上方标注
@EnableAspectJAutoProxy
开启自动代理