The create() pointcut works just fine, but I can’t get the setStatus() pointcut to work however I try… I’ve tried with @Before, @After, @AfterReturning, but nothing.
According to the debugger, both methods are called.
package com.baz;
@Aspect
public class ServiceAspect {
@Pointcut("execution(* com.foo.ServiceImpl.create(..))")
public void create() {}
@Pointcut("execution(* com.bar.Subscription.setStatus(..))")
public void setStatus() {}
// works
@AfterReturning(pointcut="create()", returning="retVal")
public void afterCreate(Object retVal) {
// omitted
}
// doesn't work
@Before("setStatus()")
public void status() {
// omitted
}
// doesn't work
@Before("setStatus() && args(status)")
public void status(int status) {
// omitted
}
// doesn't work
@After("setStatus()")
public void status() {
// omitted
}
// doesn't work
@AfterReturning(pointcut="setStatus()")
public void status2() {
// omitted
}
// doesn't work
@AfterReturning(pointcut="setStatus()", returning="retVal")
public void afterSetStatus(Object retVal) {
// omitted
}
// doesn't work
@Around("setStatus()")
public Object aroundStatus(ProceedingJoinPoint pjp) throws Throwable {
Object output = pjp.proceed();
return output;
}
}
The methods looks like this:
public class Subscription extends FooBar implements Baz {
public void setStatus(int status) { /* ... */ }
}
public class ServiceImpl implements Service {
public Subscription create(Session session, Subscription template) { /* ... */ }
}
Edit
I’ve tried using both <aop:aspectj-autoproxy />, <aop:aspectj-autoproxy proxy-target-class="true"/> and <aop:aspectj-autoproxy proxy-target-class="false"/>.
Edit 2
I’ve tried calling setStatus() directly on Subscription, but it didn’t catch that either.
Subscription subscription = new Subscription();
subscription.setStatus(1);
Subscription should be a spring-managed bean for the aspects to be applied, i.e. you should obtain objects of type
Subscriptionfrom theApplicationContextas follows:or get them injected using
@Resourceor@Autowiredannotations.Then, the call to
setStatus()onsubscriptionobject will go through the AOP proxy created by Spring framework and the code in the AOP advices that match the method definition get executed.Another way to get a spring-managed bean, while creating objects using
newoperator, is using the@Configurableannotation.