解决报错Error creating bean with name ‘logAspect’: Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [xxx] from ClassLoader
背景:我写了一个SpringAOP+自定义@注解,实现简单的日志管理的项目(项目1),然后将项目1打包成jar包,在项目2中引入使用,但是我发现这个自定义注解确实是引入成功了,也没有报错,但是缺没有生效(没有对应的日志打印)
首先,我查阅资料发现是扫描包的问题,springboot是默认自动扫描和启动类在同一目录下的包和java文件,所以对于引入的jar包需要在启动类上手动指明要扫描的包
引入后,我启动整个项目2,发现报错了!!!
2021-08-01 17:08:48.755 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'logAspect': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [com.buaa.web.anno.LogAspect] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
报错的大概意思就是不能生成logAspect
(这个是我项目1中用到的自定义aop类)这个Bean,然后查看相关资料发现应该是少了依赖,在项目2中我并没有aop相关的依赖,所以我在pom.xml文件中添加了spring-boot-starter-aop
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.4.5</version>
</dependency>
然后启动项目2,成功了!日志也成功打印了
来,试试看~