美文网首页
@ControllerAdvice @ExceptionHand

@ControllerAdvice @ExceptionHand

作者: 5b9fb50f46a2 | 来源:发表于2019-07-18 17:34 被阅读0次

    最近代码中发现,使用@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

    相关文章

      网友评论

          本文标题:@ControllerAdvice @ExceptionHand

          本文链接:https://www.haomeiwen.com/subject/svqrlctx.html