=============
package myspring;
public class Address {
private String city;
private String state;
private String country;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String toString(){
return city+" "+state+" "+country;
}
}
Employee.java
==============
package myspring;
public class Employee {
private int id;
private String name;
private Address address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
void show(){
System.out.println(id+" "+name);
System.out.println(address);
}
}
Beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="emp" class="myspring.Employee" >
<property name="id" value="1111"></property>
<property name="name" value="Thomas"></property>
<property name="address" ref="addr"> </property>
</bean>
<bean id="addr" class="myspring.Address" >
<property name="city" value="Bengaluru"> </property>
<property name="state" value="Karnataka"> </property>
<property name="country" value="India"> </property>
</bean>
</beans>
Test.java
==========
package myspring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
Employee emp = (Employee)context.getBean("emp");
emp.show();
}
}
No comments:
Post a Comment