Search code examples
springspring-aoppointcut

Spring AOP pointcut expression for Subclass only method


I have a scenario in which i need to intercept some subclass methods, but i couldn't find a proper pointcut expression to do so.

I have a client facing interface InfoService which has a method getClientDetails.

package sample;

public interface InfoService {
    InfoVO getClientDetails(int id);
}

The implementation class has some nested methods like get*Info().

package sample;

public class InfoServiceImpl implements InfoService{

    public InfoVO getClientDetails(int id) {
        InfoVO clientInfo = new InfoVO();

        clientInfo.setA(getAInfo(id));
        clientInfo.setB(getBInfo(id));
        clientInfo.setC(getCInfo(id));

        return clientInfo;
    }

    public Object getAInfo(int id) {
        return null;
    }

    public Object getBInfo(int id) {
        return null;
    }

    public Object getCInfo(int id) {
        return null;
    }
}

When the user invoke the getClientDetails method i would like to intercept the get*Info() methods. I can easily intercept the getClientDetails but nothing seems to intercept the subclass method. I even tried annotating those info methods using custom annotation but with no luck. So far I came up with the following aspect

<aop:aspect ref="infoAspect">
    <aop:pointcut expression="execution(* sample.InfoService+.*Info(..))"
        id="infoMethods" />
    <aop:around method="aroundAdviceForinfoMethods" pointcut-ref="infoMethods" />
</aop:aspect>

Setting the <aop:aspectj-autoproxy proxy-target-class="false"/> to true or false didn't help either. I know AOP cannot intercept on private subclass methods but these are public ones. Is it even possible to do this? Any help is much appreciated.

PS: The example shown is for demo purpose. The actual implementations are huge, so moving those inner methods to a different bean and invoking won't be feasible.


Solution

  • This is a classic one, having been asked here dozens of times already. I guess you have not read the Spring manual's info about Spring AOP being proxy-based and that for this reason Spring aspects cannot intercept self-invocation. If you really need AOP to work for self-invoked methods (even private ones if necessary), say goodbye to Spring AOP and hello to full AspectJ and LTW (load-time weaving).