0

i'm trying simple aop with spring 3

app-context.xml:

<bean id="lambesis" class="by.aop.test.pack.Metalcore" >
    <constructor-arg value="UYUOOOOO" />
</bean>

<bean id="fans" class="by.aop.test.pack.Fans" />

<aop:config proxy-target-class="true">
    <aop:aspect ref="fans">
        <aop:pointcut id="mus" expression="execution(* by.aop.test.pack.Musician.play(..))" />

        <aop:before pointcut-ref="mus" method="takeSeats" />
    </aop:aspect>
</aop:config>

main method:

public static void main(String[] args) 
{
    ApplicationContext ctx = new ClassPathXmlApplicationContext("by/aop/test/pack/app-context.xml"); 

    Musician musician = ctx.getBean("lambesis", Metalcore.class);
    musician.play(); 
}

Metalcore implements Musician interface

got this error both for cglib and cglib-nodep

    log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lambesis' defined in class path resource [by/aop/test/pack/app-context.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: net/sf/cglib/asm/util/TraceClassVisitor
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at by.aop.test.pack.StartingPoint.main(StartingPoint.java:10)
Caused by: java.lang.NoClassDefFoundError: net/sf/cglib/asm/util/TraceClassVisitor
    at net.sf.cglib.core.DebuggingClassWriter.toByteArray(DebuggingClassWriter.java:73)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:26)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:144)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:116)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
    at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69)
    at org.springframework.aop.framework.Cglib2AopProxy.createEnhancer(Cglib2AopProxy.java:228)
    at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:170)
    at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:112)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:476)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:362)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:322)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:407)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1426)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    ... 11 more
Caused by: java.lang.ClassNotFoundException: net.sf.cglib.asm.util.TraceClassVisitor
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 28 more

dependencies:

<dependencies>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.7</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${spring.framework.version}</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.framework.version}</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.14</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.7.3</version>
</dependency>
   <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.7.3</version>
</dependency>
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib</artifactId>
    <version>3.0</version>
</dependency>
<dependency>
   <groupId>asm</groupId>
   <artifactId>asm-all</artifactId>
   <version>2.1</version>
</dependency>

additional asm dependency fixed first error, but now i have 2 packs of asm and the next error

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lambesis' defined in class path resource [by/aop/test/pack/app-context.xml]: Initialization of bean failed; nested exception is java.lang.IncompatibleClassChangeError: Implementing class

What should i do?

kassie
  • 727
  • 4
  • 11
  • 24
  • Please, add your pom.xml – lifus Jun 26 '13 at 17:27
  • You're missing a dependency for CGLIB. – Sotirios Delimanolis Jun 26 '13 at 17:38
  • [`proxy-target-class="true"`](http://static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch08s06.html) means you use CGLib to proxy classes. Add CGLib to your classpath or set the property to false. – LaurentG Jun 26 '13 at 17:39
  • @SotiriosDelimanolis Yeah, that was my first guess too. But OP mentioned that "got this error both for cglib and cglib-nodep", so perhaps OP tried it. – lifus Jun 26 '13 at 17:49
  • @lifus I don't know what OP means by _got this error both for cglib and cglib-nodep_. A `ClassNotFoundException` and `NoClassDefFoundError` means something is missing from classpath. – Sotirios Delimanolis Jun 26 '13 at 17:51
  • @SotiriosDelimanolis It is undoubtedly that `TraceClassVisitor` is missing, but maybe it's not OP fault since there are some [issues with cglib-nodep](http://sourceforge.net/p/cglib/patches/5/). – lifus Jun 26 '13 at 18:00
  • updated the main post – kassie Jun 26 '13 at 18:36
  • [Spring is inlining both CGLIB 3.0 and ASM 4.0 since 3.2](http://blog.springsource.org/2012/12/13/spring-framework-3-2-goes-ga/) so there is no need for both asm and cglib dependencies anymore. Go ahead and remove them and make sure that your spring framework version is `[3.2,)` – lifus Jun 26 '13 at 19:04
  • Another unrelated issue: you don't need dependency for `aspectjweaver` since [`spring-context` depends on `aspectjweaver`](http://mvnrepository.com/artifact/org.springframework/spring-context/3.2.3.RELEASE) It's always better to spend some time making your pom.xml shorter in order to avoid headache in a future. – lifus Jun 26 '13 at 19:59

1 Answers1

1

You may try this:

<properties>
    <junit.version>4.11</junit.version>
    <springframework.version>3.2.3.RELEASE</springframework.version>
    <log4j.version>1.2.17</log4j.version>
    <aspectj.version>1.7.2</aspectj.version>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${springframework.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectj.version}</version>
    </dependency>
</dependencies>

Spring is inlining both cglib and asm since 3.2 so I removed dependencies on cglib and asm alogether.

Also, I removed dependency on aspectjweaver since spring-context depends on aspectjweaver.

lifus
  • 8,074
  • 1
  • 19
  • 24
  • used this config, got java.lang.NoClassDefFoundError. Then added aspectjweaver dependency, it worked. – kassie Jun 26 '13 at 23:03