0

我面临着一个非常奇怪的麻烦。我在我的应用程序中使用注释驱动的 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>
4

3 回答 3

1

Your error-handling code is dangerously vulnerable:

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

There is a number of opportunities for a new exception that masks the one you just caught. Make the exception handler bulletproof: don't parse anything and don't expose yourself to an NPE by calling methods on possibly-null values.

于 2013-01-15T10:46:37.870 回答
1

我基于您的最后评论,这是 AspectJ 方面。

@Autowired不适用于 AspectJ 方面。解决方法是使用工厂方法aspectOf以这种方式注入您在方面所需的内容:

<bean class="SomeInterceptor" factory-method="aspectOf">
  <property name="webServiceResponseFactory" ref="..."/>
</bean>
于 2013-01-15T11:07:25.817 回答
0

找到了解决方案。问题是ltw 无法正常工作,因为Tomcat 的应用程序上下文中缺少“Loader”。因此,按照 Spring 中的 8.8.3 参考指南,几乎修复了它。由于上述原因,未找到“aspectOf”。每当编织失败时都会引发此错误。此外,由于编织错误,自动装配的字段变为空。

于 2013-01-16T11:14:25.513 回答