________________springbootのAOP

AOP开发流程

1、加入依赖 spring-boot-starter-aop  自动开启AOP支持

2、写一个Aspect,封装横切关注点(日志、监控等),需要配置通知(前置、后置、环绕、异常...)和切入点(那些包的哪些方法等)

@Before("execution(* com.lsq.springboot..*.*(..))")

3、这个Aspect需要加入spring容器,切需要加入@Aspect

@Component

@Aspect

public class LogAspect {

@Before("execution(* com.lsq.springboot..*.*(..))")

public void log() {

System.out.println("Method Log done");

}

}

spring.aop.auto=true

spring.aop.proxy-target-class=true  

spring.aop.auto配置项决定是否启用AOP,默认启用

默认是使用基于JDK的动态代理来实现AOP

spring.aop.proxy-target-class-false 或者不配置,表示使用JDK的动态代理

=true表示使用cglib

如果配置了false,而类没有接口,则依然使用cglib

JoinPoint

//可以获取数据和方法名

@Component

@Aspect

public class LogAspect {

@Before("execution(* com.lsq.springboot..*.*(..))")

public void log() {

System.out.println("before  Method Log done");

}

@After("execution(* com.lsq.springboot..*.*(..))")

public void log1(JoinPoint joinpoint ) {

System.out.println("after Method Log done"+joinpoint.getTarget().getClass()

+"_____args"+Arrays.asList(joinpoint.getArgs())

+"__method"+joinpoint.getSignature());

}

}

不配置或者配置false,都不能启用代理对象

AopContext.currentProxy().getClass();

@EnableAspectJAutoProxy(exposeProxy=false)

猜你喜欢

转载自www.cnblogs.com/qiqisx/p/9383604.html