[英]How can we pass the value to multiple parameterized constructor through xml file in spring?
I have three files
1) Test.java (in which i have created two parameterized constructor)
2) spring.xml (xml file)
3) Client.java(in which i have created main method)
1)Test.java
public class Test {
private String name,email;
private int age;
public Test(String name,int age)
{
this.name=name;
this.age=age;
}
public Test(String name)
{
this.name=name;
}
public void display()
{
System.out.println("First Constructor "+name+" "+age);
System.out.println("Second Constructor "+name);
}
}
2) spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="testObj" class="Test" >
<constructor-arg value="name" index="0"/>
<constructor-arg value="123" index="1"/>
<constructor-arg value="name"/>
</bean>
</beans>
3) Client.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client { 公共类客户{
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("resource/spring.xml");
Test t1=(Test)context.getBean("testObj");
t1.display();
}
}
when i am calling only single constructor its working fine but in case of calling both of them its giving exception: Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testObj' defined in class path resource [resource/spring.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities) 当我仅调用单个构造函数时,它的工作正常,但是在调用它们的两个给定异常的情况下:线程“ main”中的异常org.springframework.beans.factory.BeanCreationException:创建在类路径中定义名称为“ testObj”的bean时出错资源[resource / spring.xml]:无法解析匹配的构造函数(提示:为简单参数指定index / type / name参数以避免类型歧义)
The comments are right. 评论是正确的。 The point of the the spring context file is to construct an instance of a class or a prototype that is constructed as needed to be injected as a dependency. Spring上下文文件的重点是构造一个类或原型的实例,该实例或原型根据需要构造为作为依赖项注入。
You only ever call one constructor to create an instance of an object (whether spring constructs your objects or you do it yourself in code). 您只能调用一个构造函数来创建对象的实例(无论spring是构造您的对象还是您自己在代码中进行操作)。
You can always place multiple objects of the same class on the spring context, calling a different constructor in each case. 您总是可以将相同类的多个对象放在spring上下文中,在每种情况下都调用不同的构造函数。
<bean id="testObj" class="Test" >
<constructor-arg value="name" index="0"/>
<constructor-arg value="123" index="1"/>
</bean>
<bean id="testObj1" class="Test" >
<constructor-arg value="name"/>
</bean>
You can try parameterized JUnits instead. 您可以尝试使用参数化的JUnits。 Where you will be able to provide as many test as you can with different test scenarios. 在这里,您将能够为不同的测试方案提供尽可能多的测试。 You can check here 你可以在这里查看
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.