I recently started working on AspectJ
and 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
- aspectjrt-1.7.1.jar
- aspectjweaver-1.7.1.jar
- aspectjweaver-1.7.1.jar
- spring-aspects-3.2.1.RELEASE.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$1.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.1
and I think there are some issues with AspejctJ
and JBOSS
.
编辑1 -我正在使用JBOSS 7.1,我认为AspejctJ和JBOSS存在一些问题。
https://*.com/questions/33164216/load-time-weaving-with-aspectj-in-jboss-as-7-1
https://*.com/questions/33164216/load-time-weaving-with-aspectj-in-jboss-as-7-1
4 个解决方案
#1
10
A NoClassDefFoundError like 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:tree and verify you are using consistent version numbers of all of the aspectj depenendecies.
maven项目中的NoClassDefFoundError通常来自库冲突。问题中没有足够的信息,您只提供了pom的一个片段,没有显示正在使用的spring方面的版本。您需要运行mvn依赖项: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,肯定会出现某种库冲突。发布在问题中,但是堆栈跟踪包含了很多对spring 3.1.1的引用。版本库。
#2
3
java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Aspect
. lang。NoClassDefFoundError:org/aspectj/lang/annotation/Aspect
It seems jar containing class org.aspectj.lang.annotation.Aspect
is not loaded from classpath
at runtime, can you try removing below dependency:
看起来jar包含类org.aspectj.lang.annotation。方面在运行时未从类路径中加载,是否可以尝试删除下面的依赖项:
<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>
#3
1
Answer to your Question
回答你的问题
- 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.jar file is presented as a dependency. Please change your dependency for aspectjweaver as 1.7.4 version.
- 如果您使用的是Spring 3.2.1。释放,请将您的组id更改为net.anthavio。maven,我只找到aspectjweaver.1.7.4。jar文件作为依赖项呈现。请将您对aspectjweaver的依赖性更改为1.7.4版本。
- It is always recommended to use the Bill of Materials when you are using Spring 3.X and above.
- 当您使用Spring 3时,总是建议使用材料清单。X和。
-
I have given an example POM, you can validate accordingly to match with your existing POM.
我已经给出了一个示例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>
-
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 context only. Else I would recommend to upgrade your JAR files to Spring 4.X version.
建议选择Spring-3.2.10。作为标准版本发布的版本,比您在问题中提到的最新版本发布。它只适用于这个特定的上下文。否则,我建议您将JAR文件升级到Spring 4。X版本。
#4
1
In order to check that your war was wrapped with the suitable jar (aspectrj - that really contain this class -org.aspect.lang.annotation.Aspect).
为了检查您的war是否包含了合适的jar (aspectrj——它确实包含了这个类——org.aspec .lang. annotes.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.
右键单击项目->部署程序集-> add ->归档文件系统->选择aspectjrt-1.7.1。jar(您可以在本地的.m2 repo中找到它)。应用运行新的war ->现在战争应该包含这个依赖项。如果错误消息消失了——>表明war打包目标出现了错误。
#1
10
A NoClassDefFoundError like 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:tree and verify you are using consistent version numbers of all of the aspectj depenendecies.
maven项目中的NoClassDefFoundError通常来自库冲突。问题中没有足够的信息,您只提供了pom的一个片段,没有显示正在使用的spring方面的版本。您需要运行mvn依赖项: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,肯定会出现某种库冲突。发布在问题中,但是堆栈跟踪包含了很多对spring 3.1.1的引用。版本库。
#2
3
java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Aspect
. lang。NoClassDefFoundError:org/aspectj/lang/annotation/Aspect
It seems jar containing class org.aspectj.lang.annotation.Aspect
is not loaded from classpath
at runtime, can you try removing below dependency:
看起来jar包含类org.aspectj.lang.annotation。方面在运行时未从类路径中加载,是否可以尝试删除下面的依赖项:
<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>
#3
1
Answer to your Question
回答你的问题
- 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.jar file is presented as a dependency. Please change your dependency for aspectjweaver as 1.7.4 version.
- 如果您使用的是Spring 3.2.1。释放,请将您的组id更改为net.anthavio。maven,我只找到aspectjweaver.1.7.4。jar文件作为依赖项呈现。请将您对aspectjweaver的依赖性更改为1.7.4版本。
- It is always recommended to use the Bill of Materials when you are using Spring 3.X and above.
- 当您使用Spring 3时,总是建议使用材料清单。X和。
-
I have given an example POM, you can validate accordingly to match with your existing POM.
我已经给出了一个示例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>
-
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 context only. Else I would recommend to upgrade your JAR files to Spring 4.X version.
建议选择Spring-3.2.10。作为标准版本发布的版本,比您在问题中提到的最新版本发布。它只适用于这个特定的上下文。否则,我建议您将JAR文件升级到Spring 4。X版本。
#4
1
In order to check that your war was wrapped with the suitable jar (aspectrj - that really contain this class -org.aspect.lang.annotation.Aspect).
为了检查您的war是否包含了合适的jar (aspectrj——它确实包含了这个类——org.aspec .lang. annotes.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.
右键单击项目->部署程序集-> add ->归档文件系统->选择aspectjrt-1.7.1。jar(您可以在本地的.m2 repo中找到它)。应用运行新的war ->现在战争应该包含这个依赖项。如果错误消息消失了——>表明war打包目标出现了错误。