2

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);
4
  • Is com.bar the correct package name for the setStatus pointcut? Oct 19, 2012 at 17:56
  • @nicholas.hauschild yep, would get a RuntimeException otherwise during initialization
    – Oscar
    Oct 20, 2012 at 5:46
  • Did you try calling the setStatus() method directly on an object of type Subscription and see if the @Before advice was called?
    – Vikdor
    Oct 20, 2012 at 6:06
  • @Vikdor just tried it, didn't work either... See edit 2.
    – Oscar
    Oct 20, 2012 at 7:44

1 Answer 1

2

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 Subscription from the ApplicationContext as follows:

ClassPathXmlApplicationContext context = 
    new ClassPathXmlApplicationContext("classpath:META-INF/Spring.xml");
Subscription subscription = context.getBean(Subscription.class);

or get them injected using @Resource or @Autowired annotations.

Then, the call to setStatus() on subscription object 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 new operator, is using the @Configurable annotation.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.