________________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)

转载于:https://www.cnblogs.com/qiqisx/p/9383604.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值