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



BeanFactory & ApplicationContext Example Spring
// Employee.java
//Student.java
//applicationContext.xml
// ClientEmp.java

package govind;

public class Employee {
            int empId;
            String empName;
            String empAddress;
            float empSalary;
            public int getEmpId() {
                        return empId;
            }
            public void setEmpId(int empId) {
                        this.empId = empId;
            }
            public String getEmpName() {
                        return empName;
            }
            public void setEmpName(String empName) {
                        this.empName = empName;
            }
            public String getEmpAddress() {
                        return empAddress;
            }
            public void setEmpAddress(String empAddress) {
                        this.empAddress = empAddress;
            }
            public float getEmpSalary() {
                        return empSalary;
            }
            public void setEmpSalary(float empSalary) {
                        this.empSalary = empSalary;
            }
           
            public void displayData()
            {
                        System.out.println("EmpId="+empId+" "+"EmpName="+empName+" "+"EmpAddress="+" "+empAddress+"EmpSalary="+empSalary);

                       
            }
            }
//Student.java
package govind;

public class Student {
           
            int sid;
            String sname;
            float smarks;
            float percentage;
            public int getSid() {
                        return sid;
            }
            public void setSid(int sid) {
                        this.sid = sid;
            }
            public String getSname() {
                        return sname;
            }
            public void setSname(String sname) {
                        this.sname = sname;
            }
            public float getSmarks() {
                        return smarks;
            }
            public void setSmarks(float smarks) {
                        this.smarks = smarks;
            }
            public float getPercentage() {
                        return percentage;
            }
            public void setPercentage(float percentage) {
                        this.percentage = percentage;
            }
           
           
            public void displayData()
            {
                        System.out.println("Student id="+sid+" "+"StudentName="+sname+" "+"StudentMarks="+smarks+" "+"Student percentege="+percentage);
            }

}



// 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">
<property name="empId" value="1001"></property>
<property name="empName" value="Govind Ballabh Khan"></property>
<property name="empAddress" value="Madhubani Bihar"></property>
<property name="empSalary" value="10000"></property>
</bean>


<bean id="stubean" class="govind.Student">
<property name="sid" value="1002"></property>
<property name="sname" value="Govind Ballabh Khan"></property>
<property name="smarks" value="430"></property>
<property name="percentage" value="75.8"></property>
</bean>

</beans>

// ClientEmp.java
package govind;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class ClientEmp {
public static void main(String[] args) {
Resource r=new ClassPathResource("applicationContext.xml");
BeanFactory b=new XmlBeanFactory(r);
Employee e = (Employee)b.getBean("empbean" );
e.displayData();

ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Student s= (Student)context.getBean("stubean");
s.displayData();
}
}



o/p:------

EmpId=1001 EmpName=Govind Ballabh Khan EmpAddress= Madhubani BiharEmpSalary=10000.0

Student id=1002 StudentName=Govind Ballabh Khan StudentMarks=430.0 Student percentege=75.8



                                                          Project Hierarchy