Spring AOP: Advice not called in abstract class

1.1k views Asked by At

I am trying save some data after and whenever a getResponse() is called from an implementation of class DefaultRequestHandler. But this not work, what am I missing?

@Aspect
public abstract class RequestHandler {
   protected ServiceProvider response;
   protected  ServiceProvider request;

    @Pointcut
    public abstract ServiceProvider getResponse();


    @After("com.mypackage.RequestHandler.getResponse()")
    public void save(ServiceProvider request, ServiceProvider response){
       persistence.save(request, response);
 }

}
1

There are 1 answers

0
M. Deinum On BEST ANSWER

Feels like you are using a canon to shoot a bug. Just create a base method which first saves and afterwards calls an abstract method. Subclasses need to implement that method.

public abstract class DefaultRequestHandler {
    protected ServiceProvider response;
    protected  ServiceProvider request;

    public final ServiceProviderTxn getResponse() {
       ServiceProviderTxn responseTxn = doGetResponse();
       save(request,responseTxn);
       return responseTxn;
    }

    protected abstract ServiceProviderTxn deGetResponse();

    public void save(ServiceProviderTxn request, ServiceProviderTxn response){
        persistenceService.save(request, response);
    }

}

A solution without AOP and which will still work. You might want to make the save method also final (or private depending on your needs).