记录一个常见的Spring @Autowired 注入对象为空的情况:
    其余代码一切按照规范来写的,但是注入对象的那个类本身,没有通过Spring注入的方式来构建,而是通过new构造,这样会导致其Autowired 的对象为空。这是因为其本身没有交由Spring 的IOC容器进行管理,而是由JVM构建,所以Spring将无法将其本身管理的对象注入进去。


The IoC container isn’t magic, and it has no way of knowing about Java objects unless you somehow inform it of them. When you call new, the JVM instantiates a copy of the new object and hands it straight to you–it never goes through the configuration process. There are three ways that you can get your beans configured.


reference: https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐