SpringBoot整合AOP

导入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

 

开启注解

@SpringBootApplication
@EnableAspectJAutoProxy  //开启aop注解
public class AOP_example {
    public static void main(String[] args) {
        SpringApplication.run(AOP_example.class,args);

    }
}

 

配置切面

@Component
@Aspect
public class AspectService {   //切面

    //切点
    @Pointcut("execution(public * com.qyc.service.impl..*(..))")
    public void webPointCut(){

    }

    @Before("webPointCut()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        System.out.println("通过");
        // 记录下请求内容
        System.out.println("URL : " + request.getRequestURL().toString());
        System.out.println("HTTP_METHOD : " + request.getMethod());
        System.out.println("IP : " + request.getRemoteAddr());
        System.out.println("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        System.out.println("ARGS : " + Arrays.toString(joinPoint.getArgs()));
    }

    @After("webPointCut()")
    public  void doAfter(){
        System.out.println("@doAfter");
    }

    @AfterReturning("webPointCut()")
    public  void doAfterReturning(){
        System.out.println("@doAfterReturning");
    }

    @AfterThrowing("webPointCut()")
    public  void doAfterThrowing(){
        System.out.println("@AfterThrowing");
    }


    @Around("webPointCut()")
    public Object doAroundAdvice(ProceedingJoinPoint proceedingJoinPoint){
        System.out.println("环绕通知的目标方法名:"+proceedingJoinPoint.getSignature().getName());
        Long start = System.currentTimeMillis();
        try {
            Object obj = proceedingJoinPoint.proceed();
            Long end = System.currentTimeMillis();
            System.out.println("环绕通知结束"+ (end-start));
            return obj;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

}

 

环绕通知的目标方法名:test02
通过
URL : http://localhost:8889/test/2
HTTP_METHOD : GET
IP : 127.0.0.1
CLASS_METHOD : com.qyc.service.impl.TestServiceImpl.test02
ARGS : [强月城]

环绕通知结束 7
@doAfter
@doAfterReturning

 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值