![](/img/trans.png)
[英]Create Pointcut for Spring AOP annotation to put annotation on class and execute advice on every method in class
[英]Spring AOP: Annotation pointcut not causing advice to execute
我正在使用Spring AOP(具有AspectJ注釋樣式支持),並且如果方法帶有特定注釋( WsTransaction
)進行注釋,則希望執行代碼。
這是我的方面:
@Aspect
@Component
public class ExampleAspect {
@Pointcut("execution(* example.*.ws.*.*(..))")
public void isWebService() {}
@Pointcut("@annotation(example.common.ws.WsTransaction)")
public void isAnnotated() {}
@Before("isWebService() && isAnnotated()")
public void before() {
System.out.println("before called");
}
}
這是一個示例類,我希望它可以在其中運行:
package example.common.ws;
@Endpoint
public class SomeEndpoint {
@WsTransaction() // I want advice to execute if this annotation present
@PayloadRoot(localPart = "SomeRequest", namespace = "http://example/common/ws/")
public SomeResponse methodToBeCalled(SomeRequest request) {
// Do stuff
return someResponse;
}
}
當我將@Before
更改為僅使用isWebService()
它將被調用,但是當我使用isWebService() && isAnnotated()
或只是isAnnotated()
嘗試時,似乎什么都沒有發生。
我的Spring配置中有<aop:aspectj-autoproxy/>
。
端點是由Spring創建的(使用component-scan
)。
注釋的保留策略是運行時。
春季版本是3.0.3.RELEASE
我不確定什么是錯誤的,或者我可以嘗試調試什么。
更新:Spring AOP似乎沒有使用@Endpoint注釋的類
更新2: AopUtils.isAopProxy(this)
和AopUtils.isCglibProxy(this)
均為false
(即使使用<aop:aspectj-autoproxy proxy-target-class="true"/>
)
首先,我必須使用<aop:aspectj-autoproxy proxy-target-class="true"/>
來使用基於類的(CGLIB)代理(而不是基於Java接口的代理)。
其次(這是我遇到的問題),我必須在處理SOAP請求的servlet的contextConfigLocation
中指定以上內容( MessageDispatcherServlet
),而不是在根應用程序上下文中進行指定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.