The following sections show how to use Spring inner bean.
In the following we have the Customer bean. In the customer object we have a custom object Person.
package com.java2s.common; public class Customer { private Person person; public Customer() { } public Customer(Person person) { this.person = person; } public void setPerson(Person person) { this.person = person; } @Override public String toString() { return "Customer [person=" + person + "]"; } }
Here is the definition for Person class.
package com.java2s.common; public class Person { private String name; private String address; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person [address=" + address + ",age=" + age + ", name=" + name + "]"; } }
One way to inject Person Java bean to Customer bean is to use ref attribute as follows.
<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-2.5.xsd"> <bean id="CustomerBean" class="com.java2s.common.Customer"> <property name="person" ref="PersonBean" /> </bean> <bean id="PersonBean" class="com.java2s.common.Person"> <property name="name" value="java2s" /> <property name="address" value="address1" /> <property name="age" value="28" /> </bean> </beans>
The Person bean is defined in the same level with Customer bean and we use ref attribute to reference the Person bean.
If the Person bean is only used in Customer bean we can nest the definition of Person bean inside the Customer bean.
<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-2.5.xsd"> <bean id="CustomerBean" class="com.java2s.common.Customer"> <property name="person"> <bean class="com.java2s.common.Person"> <property name="name" value="java2s" /> <property name="address" value="address1" /> <property name="age" value="28" /> </bean> </property> </bean> </beans>
The following xml code shows how to use inner bean for constructor injection.
<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-2.5.xsd"> <bean id="CustomerBean" class="com.java2s.common.Customer"> <constructor-arg> <bean class="com.java2s.common.Person"> <property name="name" value="java2s" /> <property name="address" value="address1" /> <property name="age" value="28" /> </bean> </constructor-arg> </bean> </beans>
The following code shows how to run the configuration above.
package com.java2s.common; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"}); Customer cust = (Customer)context.getBean("CustomerBean"); System.out.println(cust); } }
Output