Marcelo Abiarraj Marcelo Abiarraj - 20 days ago 7
Java Question

AspectJ not working with classes without interface

I'm trying to execute a method before the setter of a class noted with @Entity is called.
So I have this code by now:

@Component
@Aspect
public class Observable {

@Before("execution(* br.com.persistence.Transaction.setStatus(..))")
public void beforeSetStatus(JoinPoint joinPoint) {
System.out.println(joinPoint.getSignature().getName());
}
}


My pom.xml:

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<complianceLevel>1.6</complianceLevel>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.5</version>
</dependency>
</dependencies>
</plugin>


And in spring.xml I added:

<aop:aspectj-autoproxy proxy-target-class="true"/>


If I try to pointcut an interface, it works right, but it doesn't work with classes in persistence. I don't know if it is because they don't implement an interface or because of the @Entity annotation thats troubling.

jay jay
Answer

In order for the proxies to be created at runtime your class should either have an interface or else you should configure cglib(like in your case). http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/aop.html

Comments