文章目录
AOP的使用
AOP指的是程序运行期间动态的将某段代码切入到指定位置进行运行的编程。
- 导入AOP模块:Spring AOP:spring-aspects
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
- 创建一个业务逻辑类,在业务逻辑运行的时候将日志进行打印(方法运行之前、方法运行之后、方法运行出现异常等)
package com.atguigu.spring_annotation.aop;
public class MathCalculator {
public int div(int i, int j) {
System.out.println("MathCalculator...div");
return i / j;
}
}
- 创建一个切面类,负责打印业务逻辑类的日志信息,
切面类需要加上@Aspect注解
通知方法(@Before)
:前置通知:logStart:在目标方法(div)运行之前运行后置通知( @After)
:logEnd:在目标方法(div)运行结束之后运返回通知
(@AfterReturning):logReturn:在目标方法(div)正常返回之运行异常通知
(@AfterThrowing):logException:在目标方法(div)出现异常以后运行环绕通知
(@Around):动态代理,手动推进目标方法运行(JoinPoint.proceed)
package com.atguigu.spring_annotation.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import java.util.Arrays;
@Aspect
public class LogAspects {
//本类引用 "pointCut()"
//其他的切面引用 “com.atguigu.spring_annotation.aop.LogAspects.pointCut()”
@Pointcut("execution(public int com.atguigu.spring_annotation.aop.MathCalculator.*(..))")
public void pointCut() {
}
//在切入点表达式指定的目标方法前切入
@Before("execution(public int com.atguigu.spring_annotation.aop.MathCalculator.div(int, int))")
public void logStart(JoinPoint joinPoint) {
System.out.println(joinPoint.getSignature().getName() + "运行@Before...参数是:{" + Arrays.toString(joinPoint.getArgs()) + "}");
}
@After("pointCut()")
public void logEnd(JoinPoint joinPoint) {
System.out.println(joinPoint.getSignature().getName() + "结束... @After");
}
@AfterReturning(value = "pointCut()", returning = "result")
public void logReturn(JoinPoint joinPoint, Object result) {
System.out.println(joinPoint.getSignature().getName() + "正常返回 @AfterReturning...运行结果:{" + result + "}");
}
@AfterThrowing(value = "pointCut()", throwing = "exception")
public void logException(JoinPoint joinPoint, Exception exception) {
System.out.println(joinPoint.getSignature().getName() + "异常@AfterThrowing...异常信息:{" + exception + "}");
}
}
- 将切面类与被切面类加入到容器中,配置类必须加上
@EnableAspectJAutoProxy
注解用来开启基于注解的aop模式
package com.atguigu.spring_annotation.config;
import com.atguigu.spring_annotation.aop.LogAspects;
import com.atguigu.spring_annotation.aop.MathCalculator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@EnableAspectJAutoProxy
@Configuration
public class MainConfigOfAOP {
@Bean
public MathCalculator mathCalculator() {
return new MathCalculator();
}
@Bean
public LogAspects logAspects() {
return new LogAspects();
}
}
- 测试
package com.atguigu.spring_annotation.test;
import com.atguigu.spring_annotation.aop.MathCalculator;
import com.atguigu.spring_annotation.config.MainConfigOfAOP;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestAop {
@Test
public void test(){
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAOP.class);
MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class);
mathCalculator.div(12, 2);
/*
div运行@Before...参数是:{[12, 0]}
MathCalculator...div
div结束... @After
div异常@AfterThrowing...异常信息:{java.lang.ArithmeticException: / by zero}
*/
}
}