Spring AOP pointcut expression for Subclass only method

冷暖自知 提交于 2020-01-25 10:10:31

问题


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.


回答1:


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).



来源:https://stackoverflow.com/questions/48804837/spring-aop-pointcut-expression-for-subclass-only-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!