@ControllerAdvice @ExceptionHand
2019-07-18 本文已影响0人
5b9fb50f46a2
最近代码中发现,使用@ControllerAdvice注解过的controller,@Autowired注入的bean直接引用为null,见代码中(3).
但是使用this的totring函数却发现是有值的——代码(1)部分,增加getter方法后,使用get方式也能获取到(2)
原因未知,怀疑是@ControllerAdvice生成的代理访问不了private对象,找到原因后更新
代码
@ControllerAdvice
@ToString
@Data
public class HandleExceptionController {
private static final Logger LOG = LoggerFactory.getLogger(HandleExceptionController.class);
private VehicleBookConfig vehicleBookConfig;
private AccountService accountService;
@Autowired
public HandleExceptionController(VehicleBookConfig vehicleBookConfig, AccountService accountService) {
this.vehicleBookConfig = vehicleBookConfig;
this.accountService = accountService;
}
@ExceptionHandler
@ResponseBody
public final String handleAllException(Exception ex) {
LOG.error("exception", ex);
LOG.error("HandleExceptionController{}", this); // (1)
LOG.error("vehicleBookConfig{}", this.getVehicleBookConfig()); // (2)
LOG.error("accountService{}", accountService); // (3)
return "default exception";
}
}
结果
image.png