Pawel Batko
Pawel Batko

Reputation: 771

How to use javax.inject Provider<T> within spring xml configuration

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?

Upvotes: 4

Views: 2175

Answers (1)

Petrychenko
Petrychenko

Reputation: 463

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>

Upvotes: 2

Related Questions