我創建了這個簡單的Spring Boot應用程序來說明我發現的問題,這使得我的應用程序在啓動過程中失敗並出現ClassNotFoundException異常。Spring引導中的AspectJ在啓動過程中導致ClassNotFoundException
的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.code</groupId>
<artifactId>aspectj</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>AspectJ test for spring boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
DemoApplication.java
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
DemoConfiguration.java
@Configuration
@ComponentScan({"my.code"})
@EnableAspectJAutoProxy
public class DemoConfiguration {
}
MyAnnotation.java
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
}
MyAnnotationAspect.java
@Aspect
@Component
public class MyAnnotationAspect {
private Logger logger = LoggerFactory.getLogger(MyAnnotationAspect.class);
@Around("@annotation(MyAnnotation)")
public Object myAnnotation (ProceedingJoinPoint joinPoint) throws Throwable {
logger.debug("Before");
Object proceed = joinPoint.proceed();
logger.debug("After");
return proceed;
}
}
正如你可以推斷出,我試圖創建使用AspectJ和Spring AOP定製的註解。不幸的是,在這個設置中,我在Spring啓動時遇到異常。
Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.8.0_121]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_121]
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) ~[na:1.8.0_121]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_121]
... 25 common frames omitted
我在classpath中檢查Maven依賴 - 我使用Eclipse IDE - ,我看到aspectjweaver.jar那裏。它有正確的課程。
歡迎任何提示。
編輯:我已經嘗試在Eclipse中安裝AspectJ編譯器,根據幾年前類似的另一個問題。沒有變化。