Springboot aop不起作用的问题

本文探讨了在Springboot项目中切面编程(AOP)失效的问题,作者分享了如何排查、确认配置及最后发现从JavaClass创建Aspect类才能正常工作的关键。重点在于创建Aspect类的不同方式对AOP影响的揭秘。

最近在研究Springboot切面编程,碰到一个莫名其妙的问题,aop怎么都不起作用。自定义的注解使用到了aop,却是有效的。

网上查找了几个小时, 各种方式都试了(使用的是InteliJ IDEA编辑器)。

首先确认了pom.xml文件里是载入了aop的

其次,确认了切面类是没有问题的,

package com.jeealfa.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class AopLog {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    //线程局部变量,用于解决多线程中相同变量的访问冲突问题
    ThreadLocal<Long> startTime = new ThreadLocal<>();

    @Pointcut("execution(* com.jeealfa.controller..*.*(..))")
    public void aopWebLog() {

    }

    @Before("aopWebLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        startTime.set(System.currentTimeMillis());
        //接手到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        log.info("URL:" + request.getRequestURI().toString());
        log.info("HTTP Method:" + request.getMethod());
        log.info("IP:" + request.getRemoteAddr());
        log.info("类的方法:" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        log.info("参数:" + request.getQueryString());
        System.out.println(request.getRequestURI().toString());
    }
}

再次,根据网上查的资料在启动类加了注解,且aspect文件夹与JeealfaBootApplication处于平级

package com.jeealfa;



import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.EnableAspectJAutoProxy;



@SpringBootApplication

@EnableAspectJAutoProxy(proxyTargetClass=true)

@ComponentScan({"com.jeealfa.*"})

public class JeealfaBootApplication {



public static void main(String[] args) {

SpringApplication.run(JeealfaBootApplication.class, args);

}

}

 

实际@SpringBootApplication包含了@CompentScan

另外,HelloController类hello方法如下

package com.jeealfa.controller; 

import lombok.extern.slf4j.Slf4j; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.RestController; 
import java.util.HashMap; 

@RestController 
@Slf4j 
public class HelloController { 

    @GetMapping("/hello") 
    public String hello(){ return "hello world"; } 
}

网上各种方法都试了, aop就是始终不起作用。 运行 /hello时,AopLog里的doBefore方法始终没有运行。并且发现一个奇怪现象,其他文件修改,springboot的热启动都会自动更新启动,修改这个切面类时,却无反应。

不过最终经过多次折腾终于发现了问题所在。

在创建这个切面类时,直接选择了 Aspect, 见下图

 

创建了之后, 然后在把aspect修改为class,

修改为class后,左边最前面的A标会自动变成C标,这样就跟从Java Class创建的切面类看起来一模一样,但就是不起作用。

 

正确的做法: 通过Java Class创建aspect类,然后加上 @Aspect和@Component注解。 我试了从Java Class创建aspect类,把之前无效的代码完整拷贝到这个新建的文件里,就生效了。至于上面那种创建aspect类为何会导致无效的内在原因,还不清楚,有知情的朋友请留言回复。 虽然两种创建方式不同,但最终代码呈现完全是一样的,就是一个有用一个无用。

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值