17
votes

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

I have defined below dependencies into my pom file:

        <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:

  • 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$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.

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

4
I have gone through both the questions and their answers. No luck. In Local, I can run using the same configuration but it fails when I am deploying it to JBoss server. - Atul
Bad advise: try to package jars of aspectJ in deployment unit like in WEB-INF/lib/ if using war. Sometimes the execution environment do not provide class files of run-time dependencies as services. If it runs than you know the problem and fix it using proper way. Also Check that if any class from aspecJ package needs to be declared as bean. I am also facing similar problem with hibernate. I forced packaged jar and it ran. - abcdef12
What is your application? Did you use a quickstart or do you have a simple example piece of code? - K.Nicholas

4 Answers

16
votes

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.

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.

9
votes

java.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:

 <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>
2
votes

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.jar file 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 context only. Else I would recommend to upgrade your JAR files to Spring 4.X version.

1
votes

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

Try (within eclipse) to add this dependency manually.

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.