Spring Boot中的AspectJ在启动期间导致ClassNotFoundException

时间:2017-09-06 09:44:50

标签: java eclipse maven spring-boot aspectj

我创建了这个简单的Spring Boot应用程序来说明我发现的问题,这会使我的应用程序在启动时因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

我正在检查类路径上的maven依赖项 - 我使用Eclipse IDE - 我在那里看到了aspectjweaver.jar。它有正确的课程。

欢迎任何提示。

编辑:我已经尝试在Eclipse中安装AspectJ编译器,正如几年前的另一个问题类似。没有变化。

2 个答案:

答案 0 :(得分: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。确保你在本地仓库中有这种依赖。

希望它能解决你的问题。

答案 1 :(得分:0)

最后,我发现了这个问题的根本原因:maven下载了相关aspectjweaver jar的损坏版本。当我从我的硬盘驱动器中物理删除它(mvn -U编译不起作用)并强制它重新加载时,问题就消失了。