Question

I have the following in my class:

public class Manager {        
    private Apple apple = AppleFactory.createInstance();
    // .....
}

appContext.xml:

<bean id="manager" class="Manager"/>

AppleFactory is an external library on which I do not have any control over. I use xml configuration(appContext.xml) for wiring the beans. How can I inject the field apple from appContext.xml?

Was it helpful?

Solution

<bean id="apple" class="AppleFactory" factory-method="createInstance" />
<bean id="manager" class="Manager"/>
<context:annotation-config />

Your manager

public class Manager {

    @Autowired
    private Apple apple;

}

Should do the trick.

See the reference guide and Initializing Spring bean from static method from another Class?

OTHER TIPS

You can use following configuration :

<bean id="apple" class="jarpackagename.AppleFactory"
      factory-method="createInstance">
</bean>

<bean id="manager" class="pkgname.Manager">
    <property name="apple" ref="apple">
</bean>

You can configure Manager bean as follows

<bean class="xxx.Manager">
    <property name="apple">
        <bean class="yyy.AppleFactory" factory-method="createInstance" />
    </property>
</bean>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top