一,报错
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
用Springboot 框架写了个加密工具类,配置了 拦截
在写配置类的时候 WebMvcConfigurer,new 了一个拦截类的实例,没有选择交给Spring 注入管理,导致拦截类报错。
解决方法,全部交给Spring 管理
这里总结一个bean 报为null 的可能性
1.调用工具类的时候,new 实例,spring IOC容器是没有去看管他的,所以并不在同一个上下文中,那么new出来的这个类里面的实例方法,当然也调用不到spring IOC上下文里的实例了----所以导致会导致你工具类注入的bean 拿不到实例-------解决办法,别new ,给Spring 管理。如果不好归类的类,可以用 注解,@Component
2.一个是工具类中的方法为静态方法,静态方法调用了bean,但是由于spring的加载机制,是加载静态代码-》构造函数-》注入bean-》postconstuct的顺序执行,因此静态方法里调用的bean,并没有注入相应的bean,因此为空,所以会出现bean为null的情况。
参考文章:(7条消息) 关于Spring中,注入的bean为null的情况_springboot注入bean为null_zhuangyyyyyyyyyyyy的博客-CSDN博客