Could not autowire field in spring. why?

后端 未结 8 1794
长情又很酷
长情又很酷 2020-12-08 20:09

I keep getting this error, and can\'t figure out why.. yes I know there many people had similar issues, but reading the answers they got, does not solve my problem.

8条回答
  •  庸人自扰
    2020-12-08 20:49

    Well there's a problem with the creation of the ContactServiceImpl bean. First, make sure that the class is actually instantiated by debugging the no-args constructor when the Spring context is initiated and when an instance of ContactController is created.

    If the ContactServiceImpl is actually instantiated by the Spring context, but it's simply not matched against your @Autowire annotation, try being more explicit in your annotation injection. Here's a guy dealing with a similar problem as yours and giving some possible solutions:

    http://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/

    If you ask me, I think you'll be ok if you replace

    @Autowired
    private ContactService contactService;
    

    with:

    @Resource
    @Qualifier("contactService")
    private ContactService contactService;
    

提交回复
热议问题