This method is especially useful when you cannot change the class you are trying to create, as in the previous answer, but rather work with the API and should use the provided bean as it is.
You can always create a class (MyObjectFactory) that implements FactoryBean and inside the getObject () method, which you should write:
@Autowired private MyReferenceObject myRef; public Object getObject() { MyObject myObj = new MyObject(); myObj.init(myRef); return myObj; }
And in spring context.xml you will have a simple:
<bean id="myObject" class="MyObjectFactory"/>
Ionel source share