When creating instances of Spring-managed classes without using the Spring context (e.g., new MyClass()), it may be necessary to manually inject dependencies into those objects.
Spring provides the AutowireCapableBeanFactory to address this scenario. By autowiring your beanFactory, you can inject dependencies into any arbitrary object:
<code class="java">@Autowired private AutowireCapableBeanFactory beanFactory; // Later in your code: MyBean obj = new MyBean(); beanFactory.autowireBean(obj);</code>
After this, obj will have its dependencies automatically injected, as if it had been created by the Spring context.
The above is the detailed content of How can I inject dependencies into self-instantiated objects in Spring?. For more information, please follow other related articles on the PHP Chinese website!