Simeon
Simeon

Reputation: 7792

Spring throws ClassNotFound for custom Aspect

I'm trying to use Spring AOP, but I'm having trouble. Here it is:

Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.codarama.diet.event.aop.ProfilingAdvice] for bean with name 'profilingAdvice' defined in class path resource [META-INF/test-contexts/testProfilingAdvice.xml]; nested exception is java.lang.ClassNotFoundException: org.codarama.diet.event.aop.ProfilingAdvice

Here is the profiling advice:

package org.codarama.diet.event.aop;

import com.google.common.base.Stopwatch;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.codarama.diet.component.ListenableComponent;
import org.codarama.diet.event.model.MinimizationEndEvent;
import org.codarama.diet.event.model.MinimizationEvent;
import org.codarama.diet.event.model.MinimizationStartEvent;

import java.util.Set;

@Aspect
public class ProfilingAdvice extends ListenableComponent {

  @Around("org.codarama.diet.event.aop.IndexingAspect.get()")
  public Object profileGet(ProceedingJoinPoint pjp) throws Throwable {
    // some code
  }

  @Around("org.codarama.diet.event.aop.IndexingAspect.index()")
  public Object profileIndex(ProceedingJoinPoint pjp) throws Throwable {
    // some more code  
  }

  @Around("org.codarama.diet.event.aop.MinimizationAspect.minimize()")
  public Object profileMinimize(ProceedingJoinPoint pjp) throws Throwable {
    // and some more code
  }
}

I'm using XML config, here are the relevant parts (I hope):

<context:spring-configured/>
<context:annotation-config/>
<aop:aspectj-autoproxy/>

...

<bean id="indexingAspect" class="org.codarama.diet.event.aop.IndexingAspect"/>
<bean id="minimizationAspect" class="org.codarama.diet.event.aop.MinimizationAspect"/>
<bean id="profilingAdvice" class="org.codarama.diet.event.aop.ProfilingAdvice" parent="listenableComponent"/>

...

<bean id="listenableComponent" class="org.codarama.diet.component.ListenableComponent" abstract="true">
    <property name="eventBus" ref="statusUpdateEventBus"/>
</bean>

<bean id="statusUpdateEventBus" class="com.google.common.eventbus.AsyncEventBus">
    <constructor-arg ref="eventBusExecutor"/>
</bean>

<bean id="eventBusExecutor" class="java.util.concurrent.Executors" factory-method="newFixedThreadPool">
    <constructor-arg value="1"/>
</bean>

and the POM:

    <org.springframework.version>
        4.3.1.RELEASE
    </org.springframework.version>
    <aspectj.version>1.8.9</aspectj.version>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${org.springframework.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${aspectj.version}</version>
    </dependency>

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>${aspectj.version}</version>
    </dependency>

    <dependency>
        <groupId>aopalliance</groupId>
        <artifactId>aopalliance</artifactId>
        <version>1.0</version>
    </dependency>

Sorry about the long post, but I read and re-read several guides and I'm currently stumped a bit.

Upvotes: 0

Views: 414

Answers (1)

Simeon
Simeon

Reputation: 7792

Found it.

It turns out I had previously defined the aspect with the aspect AspectJ keyword. Like this:

@Aspect
public aspect ProfilingAdvice extends ListenableComponent {
    // code
}

I defined it with class later on, but regardless, what this did is that it made the file .aj instead of .java, thus javac was not compiling it and so the .class file was missing at runtime.

All I had to do was rename the .aj to .java and everything was fine.

Upvotes: 1

Related Questions