Address.java
=========
package
myspringIOC;
public
class Address {
private
String city;
private
String state;
private
String country;
public
Address(){
System.out.println("Address default
constructor...");
}
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
myspringIOC;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.beans.factory.annotation.Qualifier;
import
org.springframework.beans.factory.annotation.Required;
public
class Employee {
private
int id;
private
String name;
private
Address address;
public
Employee(){
System.out.println("Employee
default constructor...");
}
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;
}
@Autowired
@Qualifier("addr")
public void setAddress(Address address) {
System.out.println("Calling
setAddress");
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:annotation-config />
<!--<bean id="emp"
class="myspringIOC.Employee" autowire="byName"> -->
<bean id="emp"
class="myspringIOC.Employee" >
<property name="id"
value="1111"></property>
<property name="name"
value="Thomas"></property>
<!--<property
name="address" ref="addr"> </property> -->
</bean>
<bean id="addr" class="myspringIOC.Address"
>
<property name="city"
value="Bengaluru">
</property>
<property name="state"
value="Karnataka">
</property>
<property name="country"
value="India">
</property>
</bean>
<bean id="addr1" class="myspringIOC.Address"
>
<property name="city"
value="Chennai">
</property>
<property name="state"
value="Tamalnadu">
</property>
<property name="country"
value="India">
</property>
</bean>
<!-- -->
</beans>
Test.java
======
package
myspringIOC;
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