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

I have a class called let say A with this installer:

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

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

+4
source share
1 answer

Actually, briefly answered here , you need a ProviderCreatingFactoryBean .

This is an example:

 <bean id="a" class="abbA" scope="prototype"> <property name="xxx" value="15000"/> </bean> <bean id="b" class="abbB" 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="abcBarService"> <property name="provider" ref="providerOfA"/> </bean> <bean id="barServiceB" class="abcBarService"> <property name="provider" ref="providerOfB"/> </bean> 
+1
source

Source: https://habr.com/ru/post/1437557/


All Articles