我面临着一个非常奇怪的麻烦。我在我的应用程序中使用注释驱动的 AspectJ。当我尝试继续我的方法时,我在自动装配的字段上收到空指针。(在服务类中,我在拦截器中调用了哪些方法)。在使用 AspectJ 之前,spring 上下文工作得很好。我正在提供我的代码,也许我只是没有看到那个小错误:(花了太多时间找到它。:(
这里是 Aspect 类:
@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);
}
}
}
我的 app-context.xml 包含并启用了所有组件扫描包。
非常感谢任何帮助。
编辑
另外,在调用之后我收到这样的错误:
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="(!@NoWeave com.store.services.*) AND com.bsro.storewebsrv.services.*" />
<dump within="com.store.services.*" />
</weaver>