4

I have a class called let's say A with such a setter:

//class A
@Inject
public void setAProvider(Provider<B> b)
{
    this.b = b;
}

It works fine with javax.inject and annotation configuration when I want to have only one kind of A instance.. My problem is that I want to have two instances of class A, one with Provider<B1> and second with Provider<B2>. My question is how to express my requirements in Spring xml configuration?

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
Pawel Batko
  • 761
  • 7
  • 19

1 Answers1

1

Actually, it is briefly answered here, you need ProviderCreatingFactoryBean .

This is an example :

<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>
Community
  • 1
  • 1
Petrychenko
  • 453
  • 4
  • 5