Spring autowired bean causes null pointer

后端 未结 5 1727
死守一世寂寞
死守一世寂寞 2020-12-19 04:55

I have a logger class that makes use of a service. Each time a new logger is created, I expect to have access to the singleton scoped logging service.

I autowire the

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-19 05:24

    Just want to add my 2 cents.

    I once encountered the same issue when I was not quite used to the life in the IoC world. The @Autowired field of one of my beans is null at runtime.

    The root cause is, instead of using the auto-created bean maintained by the Spring IoC container (whose @Autowired field is indeed properly injected), I am newing my own instance of that bean and using it. Of course this one's @Autowired field is null because Spring has no chance to inject it.

提交回复
热议问题