其實它簡單回答了here,你需要ProviderCreatingFactoryBean
。
這是一個例子:
<bean id="a" class="a.b.b.A" scope="prototype">
<property name="xxx" value="15000"/>
</bean>
<bean id="b" class="a.b.b.B" scope="prototype">
<property name="zzz" value="-1"/>
</bean>
<bean id="providerOfA" class="org.springframework.beans.factory.config.ProviderCreatingFactoryBean">
<property name="targetBeanName" value="a"/>
</bean>
<bean id="providerOfB" class="org.springframework.beans.factory.config.ProviderCreatingFactoryBean">
<property name="targetBeanName" value="b"/>
</bean>
<bean id="barServiceA" class="a.b.c.BarService">
<property name="provider" ref="providerOfA"/>
</bean>
<bean id="barServiceB" class="a.b.c.BarService">
<property name="provider" ref="providerOfB"/>
</bean>