As we know, @Autowired
can be used only in instances managed by spring container, If you new an instance, and @Autowired
member in it will not effect.
But I think In some situation, new an instance can't be avoid.
Such as a RunnableTask. which contains the DAOService, which managed by spring. Because the task is manually new. So I can't use the DAOService in the ThreadTask.
So I want to know how to get the ApplicationContext in Spring Boot, and so I can get the bean by context.getBean()
.
I knew in main() I can Autowired the ApplicationContext. But I can't pass the context as a parameter everywhere!
I want to get the ApplicationContext anywhere.
Any help would be greatly appreciated.
I want to get the ApplicationContext anywhere.
That's an anti-pattern. Try to avoid it.
Why can't you inject your DAOService
into the thing that creates the RunnableTask
?
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments