문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top