Hi I working on a project, in which I need to create 2 instances of bean of same class but different constructor-args. Right now have functionality only for "production environment", so I have xml file like:
<context:component-scan base-package="com.xxx.yyy" />
<bean id="id1" class="someCompanySpecificURLForTheClass" scope="singleton"/>
<bean name="name1" factory-bean="id1" factory-method="createFullClient">
<constructor-arg index="0">
<ref bean="someJSONBean"/>
</constructor-arg>
<constructor-arg value="productionEnv" index="1"/>
</bean>
</beans>
Now I need to include functionality for testing environment also. So I changed the xml file to:
<context:component-scan base-package="com.xxx.yyy" />
<context:component-scan base-package="com.zzz.yyy" />
<bean id="id1" class="someCompanySpecificURLForTheClass" scope="prototype"/>
<bean name="name1" factory-bean="id1" factory-method="createFullClient">
<constructor-arg index="0">
<ref bean="someJSONBean"/>
</constructor-arg>
<constructor-arg value="${value.name}" index="1"/>
</bean>
In the 2 Java files, I use @Autowired annotations on the method of setClients() and on creating object of someJSONBean .
But I am receiving errors:
BeanCreationException: could not autowire method: public void com.zzz.yyy.sometthing.setClients(someCompanySpecificURLForTheClass) throws IllegalArgumentException and UnsupportedEncodingException.
nested exception is defined: NoSuchBeanDefinitionException: no unique bean of type(someCompanySpecificURLForTheClass) is defined: expected single matching bean, found 2(name1, name2).
I am new to spring framework. can anyone tell whats happening wrong, is the autowiring that i am doing wrong? how can i solve this?
Edit: Here someCompanySpecificURLForTheClass is the same class and beans(name1 and name2) use this bean as a factory bean with different constructor-arguments.
I am using spring 3.1.
After searching a lot, I think I can use placeholder for the constructor-args value right? The above is the edited xml file being used. Updated code in the Java classes: 1.
@ConfigAdapter
class class1{
@Autowired
private someJSONBean obj;
@Autowired
@Value("${value.name:thisismyvalue}")
public void setClients(){}
}
@ConfigAdapter
class class2{
@Autowired
private someJSONBean obj;
@Autowired
@Value("${value.name:thisismyvalue}")
public void setClients(){}
}
But I get the following error:
ConversionNotSupportedException: failed to convert 'java.lang.string' to 'someCompanySpecificURLForTheClass'.
nested execption is IllegalStateException
Cannot convert 'java.lang.string' to 'someCompanySpecificURLForTheClass' of required type: no matching editors or conversion strategy found.
Am I specifying the annotation value wrongly? Why is this happening?
You have to use @Qualifier
Annotation along withe @Autowired
. Since your are creating two beans of the same class, spring does not know which one to autowire
. You can give a hint using @Qualifer("id/name")
annotation.
@Autowired
@Qualifier("name1")
//yourProperty
or
@Autowired
@Qualifier("name2")
//yourProperty
Check out this link .
If you want to inject both the beans into your class, then your have two create two different properties, each pointing to a different instance.
@Autowired
@Qualifer("name1")
//property1
@Autowired
@Qualifer("name2")
//property2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.