Saturday, 26 May 2018


Dependency injection through constructor
// Employee.java
// EmployeeClient.java
//applicationContext.xml
// Student.java
//StudentClient.java
//studentbean.xml



                                                                    Project Hierarchy:-








// Employee.java
package govind;
public class Employee {
int empId;
String empName;
String empAddress;
float empSalary;

public Employee()
{
            System.out.println("Default Constructor");
}


public Employee(int empId) {
            super();
            this.empId = empId;
}


public Employee(String empName) {
            super();
            this.empName = empName;
}

public Employee(float empSalary) {
            super();
            this.empSalary = empSalary;
}



public Employee(int empId, String empName, String empAddress, float empSalary) {
            super();
            this.empId = empId;
            this.empName = empName;
            this.empAddress = empAddress;
            this.empSalary = empSalary;
}

public void displayData()
{
            System.out.println("Empid="+empId+"EmpName="+empName+"EmpAddress="+empAddress+"EmpSalary="+empSalary);
}
}

//applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
               
          
  <bean id="empbean" class="govind.Employee">
   <!-- case 1 calling int arg constructor -->
 <!--  <constructor-arg value="1001" type="int"></constructor-arg> -->
 
     <!-- case 2 calling float arg constructor-->
   <!--  <constructor-arg value="10000.00" type="float"></constructor-arg> -->  

   <!-- case 3 calling String arg constructor-->
   <!-- <constructor-arg value="GovindBallabhKhan" type="java.lang.String"></constructor-arg>  -->

     <!-- case 4 calling parameterized constructor -->
   <constructor-arg value="1001"  type="int"></constructor-arg>
                <constructor-arg value="GovindBallabhKhan" ></constructor-arg>
        <constructor-arg value="Madhubani-Bihar" ></constructor-arg>
         <constructor-arg value="35000.00" type="float" ></constructor-arg>
      
       <!-- case 5 type not defined then string type arg call-->
       <!--   <constructor-arg value="1001" ></constructor-arg>-->
      
 
  </bean>              
 </beans>


//EmployeeClient.java
package govind;

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

public class EmployeeClient {

               
                public static void main(String[] args) {
                                ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
                                Employee e = (Employee)ac.getBean("empbean");
                                e.displayData();
                }
}




o/p view:----calling parametrized constructor
Empid=1001EmpName=GovindBallabhKhanEmpAddress=Madhubani-BiharEmpSalary=35000.0

=====================================================================

// Student.java

package govind;

public class Student {
           
            int sid;
            String sname;
            float percentage;
            public Student(int sid, String sname,float percentage) {
           
                        super();
                        System.out.println("3 arg constructor");
                        this.sid = sid;
                        this.sname = sname;
                        this.percentage=percentage;
            }
           
            public Student( String sname,int sid) {
                        super();
                        System.out.println("2 arg constructor");
                        this.sname = sname;
                        this.sid = sid;
                       
            }
           
            public void displayData()
            {
                        System.out.println("Student Name="+sname+" "+"Student Id="+sid+" Student Percentage="+percentage);
            }
           
           

}



//studentbean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
               
          
  <bean id="stubean1" class="govind.Student">
  <constructor-arg value="Govind Ballabh Khan" type="java.lang.String"></constructor-arg>
   <constructor-arg value="1001" type="int"></constructor-arg>
  </bean> 
<bean id="stubean2" class="govind.Student">
  <constructor-arg value="1001" type="int"></constructor-arg>
  <constructor-arg value="Govind Ballabh Khan" type="java.lang.String"></constructor-arg>
    <constructor-arg value="76.7" type="float"></constructor-arg>
  </bean>
</beans>


// StudentClient.java
package govind;

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

public class StudentClient {
            public static void main(String[] args) {
                        ApplicationContext ac=new ClassPathXmlApplicationContext("studentbean.xml");
                        Student s1 = (Student)ac.getBean("stubean1");
                        s1.displayData();
                        Student s2 = (Student)ac.getBean("stubean2");
                        s2.displayData();
                       
            }
            }


o/p  view :-----
2 arg constructor
3 arg constructor
Student Name=Govind Ballabh Khan Student Id=1001 Student Percentage=0.0
Student Name=Govind Ballabh Khan Student Id=1001 Student Percentage=76.7


No comments:

Post a Comment