【发布时间】:2013-11-07 14:39:56
【问题描述】:
我正在开发使用 Apache-CXF 开发的 REST 服务。我正在使用 Spring 3.1 注释来连接 bean。我编写了一个拦截器,它拦截我的 REST 方法以进行监控。为此,我必须自动装配我的 Monitor 类,该类作为库添加到我的项目中。 @Autowired 在这种情况下似乎不起作用并导致 NPE。我在这里做错了吗?
@Aspect
@Component
public class ApplicationMonitoring {
Logger logger = LoggerFactory.getLogger(ApplicationMonitoring.class);
@Autowired
private Monitor monitor;
@Around("execution(* com.abc.xyz.rest.CustomerResource.getCustomerByAccountNumber(..))")
public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
String methodName = joinPoint.getSignature().getName();
long start = System.currentTimeMillis();
try {
// proceed to original method call
Object result = joinPoint.proceed();
monitor.elapsedTime(methodName, System.currentTimeMillis() - start);
return result;
} catch (Exception e) {
throw e;
}
}
应用程序上下文:
.................
......
<context:spring-configured />
<context:component-scan base-package="com.abc">
<context:exclude-filter expression="org.springframework.stereotype.Controller"
type="annotation" />
</context:component-scan>
<context:annotation-config/>
.............
【问题讨论】:
-
您能否在此
@Aspect上显示您对组件进行扫描的上下文? -
@SotiriosDelimanolis 我已经用 applicationContext 更新了原始帖子
-
这个问题通常发生在 Spring bean 的注解最终会生成 2 个代理(例如事务和方面)时。 Spring 能够生成其中一个代理,但随后它会“丢失”元数据以创建第二个代理。避免这种情况的一种方法是使用 aspectj 编译时编织器,因为某些方面将在编译时添加。
标签: java spring rest spring-aop spring-annotations