Address.java
=========
package
myspring;
public
class Address {
private
String city;
private
String state;
private
String country;
public
Address(){}
public
Address(String city, String state, String country) {
System.out.println("Address
constructor");
this.city = city;
this.state = state;
this.country = country;
}
public
String toString(){
return city+" "+state+"
"+country;
}
}
Employee.java
==========
package
myspring;
import
org.springframework.beans.factory.annotation.Autowired;
public
class Employee {
private
int id;
private
String name;
private
Address address12;//Aggregation
@Autowired
public
Employee(int id, String name, Address address12) {
this.id = id;
this.name = name;
this.address12 = address12;
}
void
show(){
System.out.println(id+"
"+name);
System.out.println(address12);
}
}
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="myspring.Employee" >
<constructor-arg name="id"
value="1111"></constructor-arg>
<constructor-arg
name="name" value="Thomas"></constructor-arg>
<!--<property
name="address" ref="addr"> </property> -->
</bean>
<bean id="address12"
class="myspring.Address" >
<constructor-arg
name="city" value="Chennai"> </constructor-arg>
<constructor-arg
name="state" value="Karnataka"> </constructor-arg>
<constructor-arg name="country"
value="India" >
</constructor-arg>
</bean>
<bean id="address"
class="myspring.Address" >
<constructor-arg
name="city" value="Bengaluru"> </constructor-arg>
<constructor-arg
name="state" value="Karnataka"> </constructor-arg>
<constructor-arg
name="country" value="India" > </constructor-arg>
</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