2

Hi I am new to spring technology. I have a class called Employee as follows which has 2 constructors with different argument types. I am able to inject values to one of the constructors as described in the xml file. May I know how to inject value to other constructor as well using constructor injection. I tried various possibilities but unable to figure out how to do it.

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); } } 

Trying to inject with spring.xml and the Java class for Injection is as follows:

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(); } } 

applicationContext.xml is as follows:

<?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> 
1
  • You could create another bean with a different id, but of the same class, and change the constructor args. Commented Jan 12, 2018 at 8:42

2 Answers 2

2

Well, that is not possible. What you are asking is
call two constructors to create one object.
This doesn't make any sense. (Just read the above line again).

You can always place multiple objects of the same class on the spring context, calling a different constructor in each case.

<?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> 

what you can do is create a constructor with all four arguments and pass null for objects which you don't want to initilize
or You can have a constructor with some arguments and others you can set through Field Injection <property name = ...>

Sign up to request clarification or add additional context in comments.

2 Comments

Yes but the output is not as expected. Eno : 0 name : null salary : 2000.0 desig : team lead Eno : 123 name : Employee salary : 0.0 desig : null first 2 values are being passed null for first bean and last 2 values are being null in case of 2nd bean. Hope you got the point
This is helpful. Was trying to figure out how to add static values in a bean outside of self-defined classes.
1
You can create a another bean like this for different arguments: <bean id = "employeeBean" class = "com.vidvaan.spring.Employee"> <constructor-arg type = "int" value = "2001"/> <constructor-arg type = "java.lang.String" value = "Employee"/> </bean> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.