解决spring项目找不到Aspect依赖注解的问题

spring项目找不到Aspect依赖注解

昨天写一个项目的时候在使用Aspect时一直找不到依赖,alt+enter自动添加Maven依赖还是解决不了问题。

在这里记录下,希望能对大家有帮助。

在pom.xml中添加一下依赖:

 <dependency>            <groupId>org.aspectj</groupId>            <artifactId>aspectjweaver</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-aop</artifactId>            <scope>test</scope>        </dependency>

添加如上依赖,等待安装之后,@Aspect注解就能正常使用了。

【特殊情况】

SpringBoot @Aspect注解不起作用的问题

搭建项目使用SpringBoot的AOP时候,发现一个问题。

我AOP类已经搞好了之后,项目运行起来,调用前后不会在控制台打印。

我的AOP类是这样的:

@Component@Aspectpublic class LogAspect {    private final Logger logger = LoggerFactory.getLogger(this.getClass());//  @Pointcut("execution(* com.wz.blog.*.*(..))")指定拦截com.wz.blog包下所有类的所有方法    @Pointcut("execution(* com.wz.blog.*.*(..))")    public void log() {}    @Before(value = "log()")    public void doBefore(){        System.out.println("方法执行前。");        logger.info("—————————————————doBefore—————————————————");    }    //在每个日志横切面之后执行    @After(value = "log()")    public void doAfter(){        System.out.println("方法执行后。");        logger.info("—————————————————doAfter—————————————————");    }    @AfterReturning(returning = "result",pointcut = "log()")    public void doAfterReturn(Object result){        logger.info("Result : {}" + result);    }}

我的项目树结构是这样的

但是运行起来也没报错。

我是出现了这样的情况:

如上图,在index前后应该有INFO输出和控制台打印的,然而我并没有。

针对以上问题网上有多种解决方案: (而我的解决方案仍不在其中。)

1、检查pom.xml类中是否依赖。

2、检查启动类中是否有这两个注解

@ComponentScan("com.wz.blog.*")@EnableAspectJAutoProxy(proxyTargetClass = true)

3、检查AOP类中除了@Aspect注解还应该有@Component注解。(这里@Component用于开启组件扫描,使得SpringBoot可以找到它。即交由SpringBoot管理。)

4、检查AOP版本是否过老。

5、检查AOP类是否和启动类在同一等级。如果不在,应该使用2的方法进行指定。(这个方法和2有点重复)

好吧以上就是我搜罗的各种大牛提供的解决方法,无疑这些都是让人欲罢不能的干货。然而并没有解决我的问题。

在多个小时的反复测试之后,发现一个突破口:当我把AOP类中 @Pointcut(“execution(* com.wz.blog.*.*(..))”)的乱写成 @Pointcut(“execution(* com.xxx.xxx.*.*(..))”)时候,报错了,报错信息如下:

warning no match for this type name:com.xxx.xxx [Xlint:invalidAbsoluteTypeName]

它说名称不匹配?也就是说我的AOP类在项目运行时是被调用加载了的,这让我放心了,我一开始以为此类未参与项目运行。

那么我接着思考,有没有可能是AOP语法出错?导致我一开始指定的包压根没找到,或者说来没来得及找到?于是我在网上搜索了报错warning no match for this type name:com.xxx.xxx [Xlint:invalidAbsoluteTypeName]的原因。

发现,有的人在写成execution(* com.wz.blog.*.*(..))是没有任何错误的,说明很可能不是AOP语法的问题。

最终我发现,在IDEA不同的版本上运行,结果各不相同。

此前我使用IDEA正式版2020.1.1, 项目运行时AOP类就是不生效,而当我使用社区版运行时——

好吧,运行成功了。

但是我们不能一直使用社区版。

然后,就让我找到了IDEA正式版的解决方案。

代码如下:

@Component@Aspectpublic class LogAspect {    private final Logger logger = LoggerFactory.getLogger(this.getClass());    @Pointcut("execution(* com.wz.blog.*..*(..))")    public void log() {}    //在每个日志横切面之前执行    @Before(value = "log()")    public void doBefore(){        System.out.println("方法执行前。");        logger.info("—————————————————doBefore—————————————————");    }    //在每个日志横切面之后执行    @After(value = "log()")    public void doAfter(){        System.out.println("方法执行后。");        logger.info("—————————————————doAfter—————————————————");    }//    returning = "result"实现通过参数result捕获以上拦截方法的返回内容,pointcut = "log()"是取得切面    @AfterReturning(returning = "result",pointcut = "log()")    public void doAfterReturn(Object result){        logger.info("Result : {}" + result);    }}

没错,和一开始需要改动的地方,只有一个微小的地方不一样。(不是注释)把

@Pointcut("execution(* com.wz.blog.*.*(..))")

改成

@Pointcut("execution(* com.wz.blog.*..*(..))")

一个“.”的差别。

好吧,困扰大半天的谜团就此解开。

最后的最后,极度怀疑这个问题是IDEA2020.1.1的版本坑导致。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

“人无完人金无足赤”,只要是人就不会是完美的,

解决spring项目找不到Aspect依赖注解的问题

相关文章:

你感兴趣的文章:

标签云: