任何人都知道如何向@ControllerAdvice注入依赖关系?@ControllerAdvice未注入@Autowired依赖关系
My @ControllerAdvice扩展Spring的ResponseEntityExceptionHandler并实现无接口。
@ControllerAdvice被调用正确,但@Autowired依赖从未被注入。没有启动注入错误,依赖关系是空的。
我想这与Spring如何使用cglib代理@ControllerAdvice以使@Autowired注解丢失有关。
我通过实现一个接口来测试,以便Spring可以创建一个JDK代理,但它也不起作用。实际上,使用一个接口,它甚至不会被调用......即使我也使用@ControllerAdvice对接口进行了注释。
有没有一种方法可以指定Spring应该为这种特定情况使用JDK代理?
编辑:顺便说一句,我使用的是Spring 3.2.4.RELEASE。
Sample类:
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
@Autowired(required = true)
public AuditService auditService;
@ExceptionHandler(value = { RuntimeException.class })
public final ResponseEntity<Object> handleRuntimeException(Exception ex, WebRequest request) {
// auditService is null here!
}
}
你确定它的方法(引用为null)不是'final'吗?你确定你在正确的时间检查了正确的对象的状态 - CGLIB代理仍然是一个不同的实例,哪些字段为空 - 它只是将调用委托给后面的bean,因为不能创建方法最后。 –