Why does Autowiring not function in a thread?

前端 未结 5 558
别那么骄傲
别那么骄傲 2021-02-06 07:13

I\'ve made a maven project in Spring 3.0, I\'ve made some DAO, services and controllers, in one of mine controller I call a service in which I start a thread, the problem is tha

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-06 07:30

    For a bean to be autowired by Spring, the bean must be a Spring bean (i.e. be declared in the context.xml file or be annotated with a Spring annotation (@Service, @Component, etc.).

    And of course, it must be instantiated by Spring, and not by your code. If you instantiate a Spring bean yourself with new, Spring doesn't know about the bean, and doesn't inject anything into it.

提交回复
热议问题