2017-09-06 74 views
0

我創建了這個簡單的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編譯器,根據幾年前類似的另一個問題。沒有變化。

回答

0

你需要用java-agent來測試spring-boot-plugin。配置如下。

<plugin> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-maven-plugin</artifactId> 
    <configuration> 
     <executable>true</executable> 
     <jvmArguments> 
      -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000 
      -javaagent:${settings.localRepository}/org/springframework/spring-instrument/${spring.version}/spring-instrument-${spring.version}.jar 
     </jvmArguments> 
     <attach>false</attach> 
    </configuration> 
</plugin> 

PS。確保你在你的本地回購中有這種依賴性。

希望它能解決您的問題。

0

最後,我找到了這個問題的根源:maven下載了相關aspectjweaver jar的損壞版本。當我從硬盤上刪除它(mvn -U編譯失效)並強制重新加載時,問題就消失了。

相關問題