Spring Inner Beans Example
November 03, 2021
On this page we will learn Spring inner beans example in XML configuration. Inner beans are always created within the parent bean. Inner beans are created within the <property>
tag in the setter method dependency injection or within <constructor-arg>
tag in constructor argument dependency injection. Inner beans do not require id
or name
properties. This is because inner beans are anonymous and no other beans can use it except enclosing bean. The declaration of bean scope in inner bean is ignored. Inner beans can be used in place of ref
attribute.
Contents
With <property> nested within <property>
Here parent and inner bean both are using<property>
.
app-conf-1.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.xsd"> <bean id="student" class="com.concretepage.bean.Student"> <property name="city"> <bean class="com.concretepage.bean.City"> <property name="cityName" value="Agra"/> <property name="population" value="1000"/> </bean> </property> <property name="name" value="Duryodhan"/> </bean> </beans>
package com.concretepage.bean; public class City { private String cityName; private int population; public City () {} public City(String cityName, int population) { this.cityName = cityName; this.population = population; } //Setters and Getters }
package com.concretepage.bean; public class Student { private City city; private String name; public Student () {} public Student(City city, String name){ this.city=city; this.name=name; } //Setters and Getters }
SpringDemo.java
package com.concretepage; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.concretepage.bean.Student; public class SpringDemo { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-conf-1.xml"); Student st=(Student) context.getBean("student"); System.out.println("Student Name:"+ st.getName()); System.out.println("City Nmae:"+st.getCity().getCityName()); System.out.println("City Population:"+st.getCity().getPopulation()); context.close(); } }
Student Name:Duryodhan City Nmae:Agra City Population:1000
With <property > nested within <constructor-arg>
Here parent bean is using<constructor-arg>
and inner bean is using <property>
.
app-conf-2.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.xsd"> <bean id="student" class="com.concretepage.bean.Student"> <constructor-arg name="city"> <bean class="com.concretepage.bean.City"> <property name="cityName" value="Agra"/> <property name="population" value="1000"/> </bean> </constructor-arg> <constructor-arg name="name" value="Duryodhan"/> </bean> </beans>
With <constructor-arg> nested within <property>
Here parent bean is using<property>
and inner bean is using <constructor-arg>
.
app-conf-3.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.xsd"> <bean id="student" class="com.concretepage.bean.Student"> <property name="city"> <bean class="com.concretepage.bean.City"> <constructor-arg name="cityName" value="Agra"/> <constructor-arg name="population" value="1000"/> </bean> </property> <property name="name" value="Duryodhan"/> </bean> </beans>
With <constructor-arg> nested within <constructor-arg>
Here parent and inner bean both are using<constructor-arg>
.
app-conf-4.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.xsd"> <bean id="student" class="com.concretepage.bean.Student"> <constructor-arg name="city"> <bean class="com.concretepage.bean.City"> <constructor-arg name="cityName" value="Agra"/> <constructor-arg name="population" value="1000"/> </bean> </constructor-arg> <constructor-arg name="name" value="Duryodhan"/> </bean> </beans>
With p-namespace
Inner bean can also use p-namespace for setter method based dependency injection.app-conf-5.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.xsd"> <bean id="student" class="com.concretepage.bean.Student"> <constructor-arg name="city"> <bean class="com.concretepage.bean.City" p:cityName="Agra" p:population="1000"/> </constructor-arg> <constructor-arg name="name" value="Duryodhan"/> </bean> </beans>
With c-namespace
Inner bean can use c-namespace for constructor argument based dependency injection.app-conf-6.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:c="http://www.springframework.org/schema/c" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student" class="com.concretepage.bean.Student"> <constructor-arg name="city"> <bean class="com.concretepage.bean.City" c:cityName="Agra" c:population="1000"/> </constructor-arg> <constructor-arg name="name" value="Duryodhan"/> </bean> </beans>