我正面臨着一個非常奇怪的麻煩。我在我的應用程序中使用註解驅動的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>
您是否使用AspectJ或AspectJ風格的Spring AOP?有一個很大的區別! –
請參閱[Spring AOP或AspectJ?](http://static.springsource.org/spring/docs/current/spring-framework-reference/html/aop.html#aop-spring-or-aspectj) –
我已經滾動到那個文檔:( – user