2

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?

Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
saravana_pc
  • 2,607
  • 11
  • 42
  • 66

3 Answers3

4
<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?

Community
  • 1
  • 1
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
2

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>
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
1

You can configure Manager bean as follows

<bean class="xxx.Manager">
    <property name="apple">
        <bean class="yyy.AppleFactory" factory-method="createInstance" />
    </property>
</bean>
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275