Home  >  Spring Core

Example of Inner beans in Spring

By Arvind Rai, April 09, 2013
Inner beans are the <bean> tag that is created inside the <property></property> in the setter method dependency injection and inside <constructor-arg> </constructor-arg> in case of constructor argument dependency injection. The dependency injection for inner beans can be achieved by using <property>, <constructor-arg>, p-namespace and c-namespace. Inner beans do not require id or name. This is because no other outer bean can use inner beans. The declaration of bean scope in inner bean will be ignored. Inner beans are used in place of ref attribute of bean tag. Inner beans are always created within the parent bean. Find the examples to create inner bean.

Inner Bean 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> 



Create Beans

For the demo we are using following beans.
City.java
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;
	}
	public String getCityName() {
		return cityName;
	}
	public int getPopulation() {
		return population;
	}
	public void setCityName(String cityName) {
		this.cityName = cityName;
	}
	public void setPopulation(int population) {
		this.population = population;
	}
} 
Student.java
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;
	}
	public City getCity() {
		return city;
	}
	public String getName() {
		return name;
	}
	public void setCity(City city) {
		this.city = city;
	}
	public void setName(String name) {
		this.name = name;
	}
} 

Run Application

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();
	}
} 
Find the output.
Student Name:Duryodhan
City Nmae:Agra
City Population:1000 

Inner Bean 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> 

Inner Bean 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> 

Inner Bean 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> 

Inner Bean 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> 

Inner Bean 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> 

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us