Saturday, 26 May 2018


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



No comments:

Post a Comment