springAOP 是指程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式
导入依赖
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
下面Demo是自定义注解和切面共同使用
自定义注解
//自定义注解
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.ANNOTATION_TYPE,ElementType.TYPE})
public @interface AspectTest {
}
业务类
//业务逻辑类 spring默认接口类型使用JDK动态代理 其他使用CGlib
public class ServiceLogic {
@AspectTest
public Integer payMent(int amount1,int amount2){
return amount1 - amount2;
}
// @AspectTest
public Integer payMent1(int amount1,int amount2){
return amount1 - amount2;
}
}
切面类
@Aspect//标注一下该类是一个切面类
@Order(1) //多个切面值越小优先级越高
public class LogAspect {
@Pointcut("execution(public * com.shilei.atguigu.springAop.ServiceLogic.*(..))")//定义切面
public void pointCut(){}
@Pointcut("@annotation(com.shilei.atguigu.springAop.AspectTest)")//自定义注解切面
public void pointAnnotation(){
}
@Before(value = "pointCut() && pointAnnotation()")//定义两个条件同时满足的切面
public void logBefore(JoinPoint joinPoint){
Object[] args = joinPoint.getArgs();
System.out.println("方法:"+joinPoint.getSignature().getName()+"before参数:"+ Arrays.asList(args));
}
@After("pointCut()")
public void logAfter(JoinPoint joinPoint){
Object[] args = joinPoint.getArgs();
System.out.println("方法:"+joinPoint.getSignature().getName()+"After参数:"+ Arrays.asList(args));
}
@AfterReturning(value = "pointCut()",returning = "object")//指定返回接受参数 Object object
public void logAfter(JoinPoint joinPoint,Object object){//多个参数jointPoint 必须写在第一个参数
System.out.println("方法:"+joinPoint.getSignature().getName()+"AfterReturning返回值:"+object);
}
@AfterThrowing(value = "pointCut()",throwing = "exception")//指定异常接收参数 Exception exception
public void logAfter(JoinPoint joinPoint,Exception exception){
System.out.println("方法:"+joinPoint.getSignature().getName()+"AfterThrowing 异常信息:"+ exception);
}
@Around("pointCut()")
public Object logAround(ProceedingJoinPoint joinPoint){
System.out.println("------环绕通知开始-------------");
Object proceed = null;
try {
proceed = joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("------环绕通知结束-------------");
return proceed;
}
}
配置类
@Configuration
@EnableAspectJAutoProxy//开启aop切面
public class MainConfiAop {
@Bean//将切面类加入到IOC容器中
public LogAspect logAspect(){
return new LogAspect();
}
@Bean//将业务逻辑类加入到IOC容器中
public ServiceLogic serviceLogic(){
return new ServiceLogic();
}
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfiAop.class);
ServiceLogic bean = applicationContext.getBean(ServiceLogic.class);
bean.payMent(1,1);
// bean.payMent1(2,2);
}
}
测试结果
------环绕通知开始-------------
方法:payMentbefore参数:[1, 1]
------环绕通知结束-------------
方法:payMentAfter参数:[1, 1]
方法:payMentAfterReturning返回值:0