2011-12-08 47 views
4

我正在使用Spring AOP(帶有AspectJ註釋樣式支持),並且想要在特定註釋(WsTransaction)註釋方法時執行代碼。Spring AOP:不會導致建議執行的註釋切入點

這裏是我的方面:

@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)。

註解的保留策略是運行時。

Spring版本是3.0.3.RELEASE

我不知道什麼是錯的或者我可以試着調試。

更新:看來Spring AOP的不皮卡@Endpoint註釋類

更新2:AopUtils.isAopProxy(this)AopUtils.isCglibProxy(this)false(使用<aop:aspectj-autoproxy proxy-target-class="true"/>即使)

回答

2

首先我不得不用<aop:aspectj-autoproxy proxy-target-class="true"/>使用基於類的(CGLIB)代理(而不是基於Java接口的代理)。我不得不在處理SOAP請求的servlet(MessageDispatcherServlet)的contextConfigLocation而不是根應用程序上下文中指定上述內容。

0

我想可能有一些問題與切入點聲明。

@Pointcut("@annotation(example.common.ws.WsTransaction)") 

爲可能的解決方案請參見this link

+0

我試過在切入點中指定參數名稱('@Pointcut(「@ annotation(wsTransaction)」)'並指定了值和argNames),但沒有運氣。另外根據http://static.springsource.org/spring/docs/3.0.3.RELEASE/spring-framework-reference/html/aop.html我應該可以像我一樣使用它。 –

+0

像上面提供的切入點在Spring中對我來說工作得很好,並且帶有註釋的方法。 –