AOP pointcut expression for any public method of a service

纵然是瞬间 提交于 2020-01-14 10:07:13

问题


What is the simplest pointcut expression that would intercept all public methods of all beans annotated with @Service? For instance, I expect it to affect both public methods of this bean:

@Service
public MyServiceImpl implements MyService {
    public String doThis() {...}
    public int doThat() {...}
    protected int doThatHelper() {...} // not wrapped
}

回答1:


This documentation should be extremely helpful.

I would do by creating two individual point cuts, one for all public methods, and one for all classes annotated with @Service, and then create a third one that combines the pointcut expressions of the other two.

Take a look at (7.2.3.1 Supported Pointcut Designators) for which designators to use. I think you are after 'execution' designator for finding public methods, and the 'annotation' designator for finding your annotation.

Then take a look at (7.2.3.2 Combining pointcut expressions) for combining them.

I have provided some code below (which I have not tested). It is mostly taken from the documentation.

@Pointcut("execution(public * *(..))") //this should work for the public pointcut
private void anyPublicOperation() {}

//@Pointcut("@annotation(Service)") this might still work, but try 'within' instead
@Pointcut("@within(Service)") //this should work for the annotation service pointcut
private void inTrading() {}

@Pointcut("anyPublicOperation() && inTrading()")
private void tradingOperation() {}


来源:https://stackoverflow.com/questions/6861016/aop-pointcut-expression-for-any-public-method-of-a-service

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