Java 如何在spring中向各种构造函数注入值

Java 如何在spring中向各种构造函数注入值,java,spring,dependency-injection,inversion-of-control,Java,Spring,Dependency Injection,Inversion Of Control,嗨,我是spring技术的新手。 我有一个名为Employee的类,如下所示,它有两个具有不同参数类型的构造函数。 我能够像xml文件中描述的那样将值注入其中一个构造函数。 我可以知道如何使用构造函数注入向其他构造函数注入值吗。 我尝试了各种可能性,但想不出怎么做 public class Employee { private int eno ; private String name ; private double salary ; private Strin

嗨,我是spring技术的新手。 我有一个名为Employee的类,如下所示,它有两个具有不同参数类型的构造函数。 我能够像xml文件中描述的那样将值注入其中一个构造函数。 我可以知道如何使用构造函数注入向其他构造函数注入值吗。 我尝试了各种可能性,但想不出怎么做

public class Employee {
    private int eno ;
    private String name ;
    private double salary ;
    private String desig ;

    public Employee(int eno, String name) {
        this.eno = eno;
        this.name = name;
    }

    public Employee(double salary, String desig) {
        this.salary = salary;
        this.desig = desig;
    }

    public void showInjectedValues() {
        System.out.println("Eno : " + eno);
        System.out.println("name : " + name);
        System.out.println("salary : " + salary);
        System.out.println("desig : " + desig);
    }

}
尝试使用spring.xml和Java类进行注入,如下所示:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InjectionTest {
    static ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springconfig.xml");

    public static void main(String[] args) {

        Employee employee = (Employee) applicationContext.getBean("employee");
        employee.showInjectedValues();

    }

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <bean id="employee" class="com.vidvaan.spring.Employee">

        <constructor-arg value="2000" index="0" type="double" />
        <constructor-arg value="team lead" index="1"
            type="java.lang.String"     />

    </bean>
</beans>
applicationContext.xml如下所示:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InjectionTest {
    static ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springconfig.xml");

    public static void main(String[] args) {

        Employee employee = (Employee) applicationContext.getBean("employee");
        employee.showInjectedValues();

    }

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

    <bean id="employee" class="com.vidvaan.spring.Employee">

        <constructor-arg value="2000" index="0" type="double" />
        <constructor-arg value="team lead" index="1"
            type="java.lang.String"     />

    </bean>
</beans>

您可以为不同的参数创建另一个类似这样的bean:

好吧,那是不可能的。你问的是
调用两个构造函数来创建一个对象。
这没有任何意义。(只需再次阅读上面的一行)

您始终可以将同一类的多个对象放置在spring上下文中,在每种情况下调用不同的构造函数

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

     <bean id="otherEmployee" class="com.vidvaan.spring.Employee">

            <constructor-arg value="100" index="0" type="int" />
            <constructor-arg value="team lead" index="1"
                type="java.lang.String"     />

        </bean>

    <bean id="employee" class="com.vidvaan.spring.Employee">

        <constructor-arg value="2000" index="0" type="double" />
        <constructor-arg value="team lead" index="1"
            type="java.lang.String"     />

    </bean>
</beans>  

您可以做的是创建一个包含所有四个参数的构造函数,并为您不想初始化的对象传递null

或者你可以有一个带有一些参数的构造函数,还有一些可以通过字段注入设置的构造函数。你可以创建另一个具有不同id但属于同一类的bean,并更改构造函数参数。是的,但输出不是预期的。Eno:0 name:null salary:2000.0 desig:team lead Eno:123 name:Employee salary:0.0 desig:null第一个bean传递的前2个值为null,第二个bean传递的后2个值为null。希望你明白这是有帮助的。他试图找出如何在自定义类之外的bean中添加静态值。