Reputation: 51159
Suppose I have a POJO instance, which is annotated by @Autowired
.
public class MyClass {
@Autowired
private Database database;
}
Can I invoke Spring processing for this instance programmatically, i.e. set database
field from beans in the given context?
Upvotes: 0
Views: 632
Reputation: 609
It can be done manually for POJO's.
Autowire AutowireCapableBeanFactory
in the caller class
@Autowired
AutowireCapableBeanFactory beanFactory;
In the function where this class needs to be used.
MyClass a = new MyClass().
beanFactory.autowireBean(a);
This will update all spring dependencies in the object a, including the database autowiring.
This is similar to How to inject dependencies into a self-instantiated object in Spring?
Upvotes: 1
Reputation: 807
Yes you can given a Spring ApplicationContext you should be able to call autowireBean() or configureBean() with the suitable parameters.
Upvotes: 0