简体   繁体   English

春天如何通过xml文件将值传递给多个参数化构造函数?

[英]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.

相关问题 我们可以在servlet中创建非参数化的构造函数吗? - Can we create a non parameterized constructor in servlet? 如何在Spring XML中将通用参数传递给构造函数? - How to pass a generic parameter to a constructor in Spring XML? 春季及格班 <?> 通过xml文件中的构造函数 - Spring passing Class<?> through constructor from xml file 如何在参数化查询中使用Spring MVC jdbcTempates传递用于删除多行的字符串 - How to Pass String for delete multiple row by using spring MVC jdbcTempates in parameterized query 传递参数化构造函数作为方法引用 - pass parameterized constructor as method reference 如何将值从一个构造函数传递给另一个构造函数? - How can I pass a value from one constructor to another constructor? 我们可以动态地将值传递给Spring bean定义的参数吗? - Can we dynamically pass value to an argument for Spring bean definition? 我们如何通过GWT java文件将带有url的隐藏参数传递给jsp? - How can we pass a hidden parameter with a url to a jsp through a GWT java file? 如何在没有编译器警告的情况下将任意数量的参数化函数传递给构造函数 - How to pass an Arbitrary number of Parameterized Functions into constructor without compiler Warnings 如何将具有多个变量的构造函数的依赖项注入到 spring xml? - How to inject a dependency of a constructor with multiple variables to spring xml?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM