异常 java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Aspect

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35392851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:37:34  来源:igfitidea点击:

Exception java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Aspect

javaspringaopaspectjspring-aop

提问by Atul

I recently started working on AspectJand wrote a simple Aspect for Logging.

我最近开始工作AspectJ并编写了一个简单的日志方面。

I have defined below dependencies into my pom file:

我已经在我的 pom 文件中定义了以下依赖项:

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>

And Following artifacts are present in ear/lib folder:

并且在 ear/lib 文件夹中存在以下工件:

  • aspectjrt-1.7.1.jar
  • aspectjweaver-1.7.1.jar
  • spring-aspects-3.2.1.RELEASE.jar
  • aspectjrt-1.7.1.jar
  • aspectjweaver-1.7.1.jar
  • spring-aspects-3.2.1.RELEASE.jar

I am getting below exception on service startup.:

我在服务启动时遇到异常。:

05:59:18,325 ERROR [org.springframework.web.context.ContextLoader] (MSC service thread 1-15) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'calNamingBean' defined in ServletContext resource [/WEB-INF/helixservicebeans.xml]: BeanPostProcessor before instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Aspect
                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:452) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
                    at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:294) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
                    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
                    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
                    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
                    at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:740) [spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE]
                    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:449) [spring-context-3.1.1.RELEASE.jar:3.1.1.RELEASE]
                    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:385) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
                    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:284) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
                    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]
                    at org.apache.catalina.core.StandardContext.contextListenerStart(StandardContext.java:3392) [jbossweb-7.0.17.Final.jar:]
                    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3850) [jbossweb-7.0.17.Final.jar:]
                    at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:89) [jboss-as-web-7.1.3.Final.jar:7.1.3.Final]
                    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
                    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_45]
                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_45]
                    at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_45]
            Caused by: java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Aspect
                    at org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactory.hasAspectAnnotation(AbstractAspectJAdvisorFactory.java:119) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
                    at org.springframework.aop.aspectj.annotation.AbstractAspectJAdvisorFactory.isAspect(AbstractAspectJAdvisorFactory.java:115) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
                    at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.isInfrastructureClass(AnnotationAwareAspectJAutoProxyCreator.java:100) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
                    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessBeforeInstantiation(AbstractAutoProxyCreator.java:278) [spring-aop-3.1.1.RELEASE.jar:3.1.1.RELEASE]
                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:880) [spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]

Edit 1- I am using JBOSS 7.1and I think there are some issues with AspejctJand JBOSS.

编辑1-我使用JBOSS 7.1,我觉得有一些问题AspejctJJBOSS

https://stackoverflow.com/questions/33164216/load-time-weaving-with-aspectj-in-jboss-as-7-1

https://stackoverflow.com/questions/33164216/load-time-weaving-with-aspectj-in-jboss-as-7-1

回答by hyness

A NoClassDefFoundErrorlike this in a maven project is usually from a library conflict. There is insufficient information in the question, you only provide a snippet of your pom and don't show the version of spring aspects you are using. You need to run mvn dependency:treeand verify you are using consistent version numbers of all of the aspectj depenendecies.

Maven 项目中像这样的NoClassDefFoundError通常来自库冲突。问题中的信息不足,您只提供了 pom 的片段,而没有显示您正在使用的 spring 方面的版本。您需要运行mvn dependency:tree并验证您使用的是所有 aspectj 依赖项的一致版本号。

Edit:There is definitely some kind of library conflict because you mention spring-aspects 3.2.1.RELEASE in the question, but the stack trace contains a lot of references to spring 3.1.1.RELEASE libraries.

编辑:肯定存在某种库冲突,因为您在问题中提到了 spring-aspects 3.2.1.RELEASE,但堆栈跟踪包含大量对 spring 3.1.1.RELEASE 库的引用。

回答by Praveen Kumar K S

Answer to your Question

回答你的问题

  1. If you are using Spring 3.2.1.RELEASE, please change your group id as net.anthavio.maven, I find only aspectjweaver.1.7.4.jarfile is presented as a dependency. Please change your dependency for aspectjweaver as 1.7.4 version.
  2. It is always recommended to use the Bill of Materials when you are using Spring 3.X and above.
  3. I have given an example POM, you can validate accordingly to match with your existing POM.

                                        <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>com.yourcompany.app</groupId>
                                        <artifactId>springBOM</artifactId>
                                        <version>0.0.1-SNAPSHOT</version>
    
                                        <dependencyManagement>
                                            <dependencies>
                                                <dependency>
                                                    <groupId>org.springframework</groupId>
                                                    <artifactId>spring-framework-bom</artifactId>
                                                    <version>3.2.10.RELEASE</version>
                                                    <type>pom</type>
                                                    <scope>import</scope>
                                                </dependency>
                                            </dependencies>
                                        </dependencyManagement>
                                        <dependencies>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-aop</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-aspects</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-beans</artifactId>
                                         </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-context</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-context-support</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-core</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-expression</artifactId>
                                        </dependency>
    
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-jdbc</artifactId>
                                        </dependency>
    
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-orm</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-oxm</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-test</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-tx</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-web</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-webmvc</artifactId>
                                        </dependency>
                                       </dependencies>
                                        <build>
                                            <finalName>springBOM</finalName>
                                            <plugins>
                                          <plugin>
                                            <groupId>org.apache.maven.plugins</groupId>
                                            <artifactId>maven-compiler-plugin</artifactId>
                                            <version>3.5.1</version>
                                            <configuration>
                                              <source>1.7</source>
                                              <target>1.7</target>
                                            </configuration>
                                          </plugin>
                                        </plugins>
                                        </build>
                                      </project>
    
  4. It is recommended to choose Spring-3.2.10.RELEASE version as a standard version at is released on latest than your version mentioned in your question. It applies to this specific contextonly. Else I would recommend to upgrade your JAR files to Spring 4.X version.

  1. 如果您使用的是Spring 3.2.1.RELEASE,请将您的组 ID 更改为 net.anthavio.maven,我发现只有aspectjweaver.1.7.4.jar文件作为依赖项出现。请将您对 aspectjweaver 的依赖更改为 1.7.4 版本。
  2. 当您使用 Spring 3.X 及更高版本时,始终建议使用材料明细表。
  3. 我已经给出了一个示例 POM,您可以相应地进行验证以与您现有的 POM 匹配。

                                        <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>com.yourcompany.app</groupId>
                                        <artifactId>springBOM</artifactId>
                                        <version>0.0.1-SNAPSHOT</version>
    
                                        <dependencyManagement>
                                            <dependencies>
                                                <dependency>
                                                    <groupId>org.springframework</groupId>
                                                    <artifactId>spring-framework-bom</artifactId>
                                                    <version>3.2.10.RELEASE</version>
                                                    <type>pom</type>
                                                    <scope>import</scope>
                                                </dependency>
                                            </dependencies>
                                        </dependencyManagement>
                                        <dependencies>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-aop</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-aspects</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-beans</artifactId>
                                         </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-context</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-context-support</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-core</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-expression</artifactId>
                                        </dependency>
    
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-jdbc</artifactId>
                                        </dependency>
    
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-orm</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-oxm</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-test</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-tx</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-web</artifactId>
                                        </dependency>
                                        <dependency>
                                            <groupId>org.springframework</groupId>
                                            <artifactId>spring-webmvc</artifactId>
                                        </dependency>
                                       </dependencies>
                                        <build>
                                            <finalName>springBOM</finalName>
                                            <plugins>
                                          <plugin>
                                            <groupId>org.apache.maven.plugins</groupId>
                                            <artifactId>maven-compiler-plugin</artifactId>
                                            <version>3.5.1</version>
                                            <configuration>
                                              <source>1.7</source>
                                              <target>1.7</target>
                                            </configuration>
                                          </plugin>
                                        </plugins>
                                        </build>
                                      </project>
    
  4. 建议选择 Spring-3.2.10.RELEASE 版本作为标准版本,发布时间比您问题中提到的版本最新。它仅适用于此特定上下文。否则,我建议将您的 JAR 文件升级到 Spring 4.X 版本。

回答by Arpit Aggarwal

java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Aspect

java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Aspect

It seems jar containing class org.aspectj.lang.annotation.Aspectis not loaded from classpathat runtime, can you try removing below dependency:

似乎org.aspectj.lang.annotation.Aspect没有classpath在运行时加载包含类的 jar ,您可以尝试删除以下依赖项:

 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-aspects</artifactId>
 </dependency>

and add below in your classpath:

并在您的类路径中添加以下内容:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>3.2.1.RELEASE</version>
</dependency>

回答by Hezi Schrager

In order to check that your war was wrapped with the suitable jar (aspectrj - that really contain this class -org.aspect.lang.annotation.Aspect).

为了检查您的战争是否用合适的 jar(aspectrj - 真正包含此类 -org.aspect.lang.annotation.Aspect)包装。

Try (within eclipse) to add this dependency manually.

尝试(在 eclipse 中)手动添加此依赖项。

right click on the project -> Deployment Assembly -> add -> Archives from file system -> pick the aspectjrt-1.7.1.jar (you can find it in your local .m2 repo). apply run the new war -> now the war should include this dependency. If the error message is gone -> that indicates that something went wrong with war packaging goal.

右键单击项目 -> 部署程序集 -> 添加 -> 文件系统存档 -> 选择 aspectjrt-1.7.1.jar(您可以在本地 .m2 存储库中找到它)。apply run the new war -> 现在战争应该包含这个依赖。如果错误消息消失 -> 表示战争打包目标出现问题。