0

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.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Marcelo Abiarraj
  • 199
  • 3
  • 18

1 Answers1

1

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

Curious
  • 453
  • 4
  • 15
  • The Transaction class is not a bean, can I pointcut it with @Aspect, jay? – Marcelo Abiarraj Nov 08 '16 at 16:54
  • If you are using Spring AOP then all your objects should be "spring managed". Because Spring would require to create proxies at runtime and it can't do that unless you configure it to be within the "spring context". However if you are not using Spring for any other use cases, then you could consider looking at AspectJ which is a different framework. With AspectJ, you should be able to do this - http://stackoverflow.com/questions/1606559/spring-aop-vs-aspectj – Curious Nov 08 '16 at 17:09
  • Aparently I can make Spring manage an object with @Configurable. Is it easier to make that work than migrating to AspectJ itself? My project uses Spring everywhere, except on persistence, which is managed by Hibernate – Marcelo Abiarraj Nov 08 '16 at 18:21
  • If you are trying to add pointcuts to objects using spring annotations then they are spring managed and aop should work. Have a look at this link http://stackoverflow.com/questions/8224465/spring-use-of-proxies-in-spring-aop and based on the version of spring, make the required changes. It talks about CGLIB as well which is required for objects without interfaces. – Curious Nov 08 '16 at 18:49
  • If you face more issues with the configurations may i advice to create a new question. Your original question has been answered, so kindly mark it as answered as well. – Curious Nov 08 '16 at 18:51