1
votes
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.config.internalAutoProxyCreator': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1040)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:220)
    at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:615)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:465)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.springaoppractice.aop.AopMain.main(AopMain.java:12)

Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1088)
    ... 13 more
Caused by: java.lang.NoClassDefFoundError: org/aspectj/lang/annotation/Around
    at org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory.<clinit>(ReflectiveAspectJAdvisorFactory.java:74)
    at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.<init>(AnnotationAwareAspectJAutoProxyCreator.java:53)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    ... 15 more

I can not figure out why am I getting this error my AspectJ Lib includes following jars:

  • aspectjrt.jar
  • aspectjweaver.jar
  • cglib-2.2.2.jar
  • aopalliance-1.0.jar
  • asm-common-3.3.1.jar

Here is my main method:

package com.springaoppractice.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.springaoppractice.aop.service.ShapeService;

public class AopMain {

    public static void main (String[] args){

        ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
        ShapeService shapeService = context.getBean("shapeService", ShapeService.class);
        System.out.println(shapeService.getCircle().getName());

    }
}

Here is my Aspect:

package com.springaoppractice.aop.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class LoggingAspect {

    @Before("execution(public String getName())")
    public void LoggingAdvice (){
        System.out.println("Logging advice initiazlied, get method is called!");
    }
}

Heres my ServiceClass:

package com.springaoppractice.aop.service;

import com.springaoppractice.aop.model.Circle;
import com.springaoppractice.aop.model.Triangle;

public class ShapeService {

    private Circle circle;
    private Triangle triangle;

    public Circle getCircle() {
        return circle;
    }
    public void setCircle(Circle circle) {
        this.circle = circle;
    }
    public Triangle getTriangle() {
        return triangle;
    }
    public void setTriangle(Triangle triangle) {
        this.triangle = triangle;
    }
}

Here are my model:

package com.springaoppractice.aop.model;

public class Circle {

    private String name;

    public String getName(){
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

package com.springaoppractice.aop.model;

public class Triangle {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Here is my SPRING.XML

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <aop:aspectj-autoproxy/>

    <bean name="triangle" class="com.springaoppractice.aop.model.Triangle">
        <property name="name" value="TRIANGLE NAME" />
    </bean>

    <bean name="circle" class="com.springaoppractice.aop.model.Circle">
        <property name="name" value="CIRCLE NAME" />
    </bean>

    <bean name="shapeService" class="com.springaoppractice.aop.service.ShapeService" autowire="byName" />
    <bean name="loggingAspect" class="com.springaoppractice.aop.aspect.LoggingAspect" />



</beans>

Jars that are added to classpath: AspectJ Jars

5
What do you mean by my AspectJ Lib includes following jars? Where do you have those libraries? - Sotirios Delimanolis
@SotiriosDelimanolis I mean I have a user library name aspectJ and it includes the jars that I have listed, I have downloaded all of them and made a custom library and included it in my project for spring AOP. - Haris Mehmood
I don't know what you mean by user library. What IDE are you using? How do you run your application? Have you included those listed libraries on the runtime classpath? - Sotirios Delimanolis
@SotiriosDelimanolis yes, included it in my runtime class path. I am using Eclipse Luna - Haris Mehmood
Can you browse your aspectjrt.jar file using tool like WinRAR to make sure Around class in already existed in.!! - Wundwin Born

5 Answers

3
votes

So I was having the same issue due to incorrect aopalliance jar. What I had earlier was aopalliance-alpha1.jar

But then I changed it to aopalliance-1.0.jar which you can download from http://mvnrepository.com/artifact/aopalliance/aopalliance/1.0

The program is working for me.

1
votes

For me it works nicely with this on the classpath (I did not use Maven on purpose and manually added all libs in Eclipe in order to replicate your setup):

Eclipse build path

The aspect's output appears on the console as expected. Maybe you want to compare your libs to mine. I strongly recommend to use Maven as a build tool though. This way you have more control over your dependencies.

Edit: I forgot to mention that my Eclipse project is an AspectJ project, not a normal Java project. I.e. I use ajc as a compiler, not javac. The result is compile-time weaving. If you want to use load-time weaving, please tell me and I will test it that way.

Edit 2: I have just converted my project to a normal Java project, i.e. javac compiles all classes/aspects and Spring AOP with LTW are now used to apply aspects. It still works flawlessly. My current library setup looks like this:

enter image description here

1
votes

Had the same error. I downloaded aopalliance-1.0-sources.jar and aspectjweaver-1.7.1-sources.jar instead of aopalliance-1.0.jar and aspectjweaver-1.7.1.jar (without "source"). After downloading proper jars (@kriegaex links) it works well.

1
votes

Thanks for this question, I encounterd exactly the same error, its purely the Jar error. I resolved this issue by using asm-3.3.1.jar, aspectjrt.jar, cglib-3.1.jar, aopalliance-1.0.jar and aspectweaver-1.7.2.jar. I think its aopalliance and aspectweaver dependency which causes this error. Hope its helpful

1
votes

I had the same problem and im sure you are following java brains as i am :) just change the aopalliance -jar to aopalliance1.0-jar it works for me.