2013-01-15 41 views
0

我正面臨着一個非常奇怪的麻煩。我在我的應用程序中使用註解驅動的AspectJ。當我嘗試繼續我的方法時,我收到我的autowired字段上的空指針。 (在服務類中,我在攔截器中調用哪些方法)。在使用AspectJ之前,spring的環境工作得很好。我提供我的代碼,也許我只是沒有看到小錯誤:(花了太多的時間找到它:(春季AOP方面毀了背景

這裏去看點類:

@Aspect 
@Component 
public class SomeInterceptor{ 

private static final Logger LOGGER = LoggerFactory.getLogger(SomeInterceptor.class); 

@Autowired 
@Qualifier("webServiceResponseFactory") 
protected WebServiceResponseFactory responseFactory; 

@Autowired 
@Qualifier("configBean") 
protected ConfigBean configBean; 

public SomeServiceInterceptor() { 
    LOGGER.info("Some service interceptor was created"); 
} 

@Around("execution(public * pathToMyClassIsCorrect.*(..))") 
public Object invoke(ProceedingJoinPoint pjp) throws Throwable { 
    String securityTokenForMethod = pjp.getArgs()[0].toString(); 
    if (securityTokenForMethod != null) { 
     LOGGER.info("Proceeding the securityTokenCheck before calling method " 
       + pjp.getSignature().getName()); 
     if (validateAccessToken(securityTokenForMethod)) { 
      return responseFactory 
        .buildFailedResponse("securityAccessToken is invalid for this request"); 
     } 
    } else { 
     throw new Throwable("There was an exception before calling method " 
       + pjp.getSignature().getName()); 
    } 

    try { 
     LOGGER.info("Calling method " + pjp.getSignature().getName()); 
     Object methodToInvoke = pjp.proceed(); 
     LOGGER.info("Method " + pjp.getSignature().getName() 
       + " was called successfully"); 
     return methodToInvoke; 
    } catch (Throwable e) { 
     LOGGER.info("Method " + pjp.getSignature().getName() + "threw an exception. "); 
     boolean verboseError = Boolean.parseBoolean(pjp.getArgs()[1].toString()); 

     return responseFactory.buildErrorResponse(e, verboseError); 
    } 
} 

} 

我的應用程序上下文。 XML已經全部compontent掃描包包括和啓用。

任何幫助是非常讚賞。

編輯

此外,調用之後我收到這樣的錯誤:

SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container 

編輯2.0:

只是想而不方面推出的服務,工作正常。所以麻煩實際上是由截取服務方法的方面引起的。 :\

編輯3.0:

我已經添加了一個記錄器的構造函數。所以,如我所見,該對象甚至沒有創建。不知道可能是什麼原因。

編輯4.0: AspectJ的配置文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE aspectj PUBLIC 
    "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd"> 
<aspectj> 
<weaver options="-verbose -showWeaveInfo -debug"> 
    <!-- only weave classes in our application-specific packages --> 
    <include within="com.store.*"/> 
    <include within="com.store.services.*"/> 
</weaver> 
<aspects> 
    <!-- weave in just this aspect --> 
    <aspect name="com.store.services.StoreServiceAspect"/> 
</aspects> 
</aspectj> 

<weaver options="-verbose"> 
<include within="javax.*" /> 
<include within="org.aspectj.*" /> 
<include 
    within="([email protected] com.store.services.*) AND com.bsro.storewebsrv.services.*" /> 

<dump within="com.store.services.*" /> 
</weaver> 
+1

您是否使用AspectJ或AspectJ風格的Spring AOP?有一個很大的區別! –

+0

請參閱[Spring AOP或AspectJ?](http://static.springsource.org/spring/docs/current/spring-framework-reference/html/aop.html#aop-spring-or-aspectj) –

+0

我已經滾動到那個文檔:( – user

回答

0

找到了解決辦法。問題在於,由於缺少Tomcat中應用上下文中的「Loader」,ltw無法正常工作。所以遵循Spring的8.8.3參考指南,幾乎修復了它。由於上述原因未能找到「aspectOf」。這個錯誤會在織造失敗時拋出。此外,由於編織錯誤,自動佈線字段變爲空。

1

你的錯誤處理代碼是危險的脆弱:

LOGGER.info("Method " + pjp.getSignature().getName() + "threw an exception. "); 
boolean verboseError = Boolean.parseBoolean(pjp.getArgs()[1].toString()); 

有一個新的異常,一些機會,掩蓋了一個你剛剛發現。使異常處理程序具有防彈性:不要解析任何內容,也不要通過調用可能爲空值的方法將自己暴露給NPE。

+0

好的。謝謝你,只是修正了這一點。但沒有變化。 – user

1

我基於你的最新評論,這是AspectJ方面。

@Autowired將不適用於AspectJ方面。解決方法是使用一個工廠方法aspectOf你在你需要的方面是什麼這樣的方式注入:

<bean class="SomeInterceptor" factory-method="aspectOf"> 
    <property name="webServiceResponseFactory" ref="..."/> 
</bean> 
+0

剛剛得到「沒有找到匹配的工廠方法:工廠方法'aspectOf'錯誤。」 – user

+0

好的,那麼在AspectJ的設置中有些東西是關閉的,因爲AspectJ編譯器會將aspectOf和hasAspect方法注入方面。你可以請分享一點點你的方面相關的配置 –

+0

剛剛添加aop.xml – user