0

I am trying to compile a Spring application in which the ApplicationConfig file has @EnableAspectJAutoProxy anotation I am facing this error when trying to run the project:

Failed to instantiate [org.springframework.aop.aspectj.annotation.
 AnnotationAwareAspectJAutoProxyCreator]:
 Constructor threw exception; nested exception is    
 java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around

I have to say when I remove the above annotation the project is running successfully.

Having read on Internet I thought maybe it's because of incompatible versions for of libraries and jar files.

When I run mvn dependency:tree the output is as below:

[INFO]    task-segment: [dependency:tree]
[INFO] ------------------------------------------------------------------------
[INFO] [dependency:tree {execution: default-cli}]
[INFO] groupId:myProject:jar:1.0-SNAPSHOT
[INFO] +- junit:junit:jar:4.11:compile
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:compile
[INFO] +- org.springframework:spring-webmvc:jar:4.3.0.RELEASE:compile
[INFO] |  +- org.springframework:spring-aop:jar:4.2.6.RELEASE:compile (version m
anaged from 4.3.0.RELEASE)
[INFO] |  |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  +- org.springframework:spring-beans:jar:4.2.6.RELEASE:compile
[INFO] |  +- org.springframework:spring-context:jar:4.2.6.RELEASE:compile (versi
on managed from 4.3.0.RELEASE)
[INFO] |  +- org.springframework:spring-core:jar:4.2.6.RELEASE:compile
[INFO] |  +- org.springframework:spring-expression:jar:4.2.6.RELEASE:compile
[INFO] |  \- org.springframework:spring-web:jar:4.2.6.RELEASE:compile (version m
anaged from 4.3.0.RELEASE)
[INFO] +- org.aspectj:aspectjweaver:jar:1.8.9:compile
[INFO] +- javax.servlet:javax.servlet-api:jar:3.1.0:compile
[INFO] \- org.aspectj:aspectjrt:jar:1.8.9:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds

and this is the content of the pom.xml file :

 <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.0.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.9</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.9</version>
    </dependency>
</dependencies>

Question: I don't understand this output is saying that everything is fine or there might be some incompatibility in my project, and if there are how to solve them?

Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
Siavosh
  • 2,314
  • 4
  • 26
  • 50

1 Answers1

0

If you have worked on maven in your projects for dependency management, then you must have faced one problem at least once or may be more than that. And the problem is version mismatch. It generally happens when you got some dependencies which bring it’s related dependencies together with certain version. And if you have included those dependencies with different version numbers already, they can face undesired results in compile time as well as runtime also.

Ideally to avoid above issue you need to explicitly exclude the related dependency, but it is quite possible that you can forget to do so.

To solve version mismatch issue, you can use the concept of a “bill of materials” (BOM) dependency. A BOM dependency keep track of version numbers and ensure that all dependencies (both direct and transitive) are at the same version.

How to add BOM [Bill Of Materials] dependency

Maven provides a tag dependencyManagement for this purpose. You need to add the bom information in this tag as follows. I am taking the example of Spring bom file.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.3.0.RELEASE</version>
            <type>pom</type>
        <scope>import</scope>
    </dependency>
</dependencies>

An added benefit of using the BOM is that you no longer need to specify the version attribute when depending on Spring Framework artifacts. So it will work perfectly fine.

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-web</artifactId>
    </dependency>
<dependencies>

Doing like above will resolve all version comparability issues and application works fine as we are not going to use unmatched versions

rajadilipkolli
  • 3,475
  • 2
  • 26
  • 49
  • thanks for your answer, but I am looking for an answer whih helps me get the current code working not to start changing everything from scratch – Siavosh Jul 02 '16 at 17:17
  • You need to change only in pom.xml and I am sure it will solve the incompatibility issue as you always get compatible versions – rajadilipkolli Jul 02 '16 at 17:36
  • @rajadilipolli so could you please put a new version of my pom.xml file in answers I would test it and if it works I can thick it as the right answer – Siavosh Jul 02 '16 at 23:52
  • At the top of you dependencies tag copy and paste the bill of materials BOM code which I have submitted in the answer and then just remove version tag from each of the dependency there after, it will solve the issue – rajadilipkolli Jul 03 '16 at 04:30