场景:实战项目通过GlobalExceptionAdvice与ExceptionCodeConfiguration实现了配置化的异常抛出,想自己再封一个正常返回的封装类

问题:创建了一个普通的类为CommonResponse,在里面引入了ExceptionCodeConfiguration,如下

 

 

 通过new CommonResponse去返回相应的参数,提示ExceptionCodeConfiguration为null,导致无法取出对应的配置message

结论:springboot的生命周期一般是当对象里面被new的时候,引入的类、容器等,也会被重新new一次

方案:重写一个ResponseUtil工具类,在里面引入了ExceptionCodeConfiguration,并在使用ResponseUtil使用Autowired的方式引入,不new,如下

 

posted on 2020-08-07 10:12  lsz0210  阅读(259)  评论(0编辑  收藏  举报