Main Tutorials

Spring inner bean examples

In Spring framework, whenever a bean is used for only one particular property, it’s advise to declare it as an inner bean. And the inner bean is supported both in setter injection ‘property‘ and constructor injection ‘constructor-arg‘.

See a detail example to demonstrate the use of Spring inner bean.


package com.mkyong.common;

public class Customer 
{
	private Person person;

	public Customer(Person person) {
		this.person = person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	@Override
	public String toString() {
		return "Customer [person=" + person + "]";
	}
}

package com.mkyong.common;

public class Person 
{
	private String name;
	private String address;
	private int age;
	
	//getter and setter methods
	
	@Override
	public String toString() {
		return "Person [address=" + address + ", 
                               age=" + age + ", name=" + name + "]";
	}	
}

Often times, you may use ‘ref‘ attribute to reference the “Person” bean into “Customer” bean, person property as following :


<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.mkyong.common.Customer">
		<property name="person" ref="PersonBean" />
	</bean>

	<bean id="PersonBean" class="com.mkyong.common.Person">
		<property name="name" value="mkyong" />
		<property name="address" value="address1" />
		<property name="age" value="28" />
	</bean>
	
</beans>

In general, it’s fine to reference like this, but since the ‘mkyong’ person bean is only used for Customer bean only, it’s better to declare this ‘mkyong’ person as an inner bean as following :


<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.mkyong.common.Customer">
		<property name="person">
			<bean class="com.mkyong.common.Person">
				<property name="name" value="mkyong" />
				<property name="address" value="address1" />
				<property name="age" value="28" />
			</bean>
		</property>
	</bean>
</beans>

This inner bean also supported in constructor injection as following :


<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.mkyong.common.Customer">
		<constructor-arg>
			<bean class="com.mkyong.common.Person">
				<property name="name" value="mkyong" />
				<property name="address" value="address1" />
				<property name="age" value="28" />
			</bean>
		</constructor-arg>
	</bean>
</beans>
Note
The id or name value in bean class is not necessary in an inner bean, it will simply ignored by the Spring container.

Run it


package com.mkyong.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[] {"Spring-Customer.xml"});

    	Customer cust = (Customer)context.getBean("CustomerBean");
    	System.out.println(cust);
     
    }
}

Output


Customer [person=Person [address=address1, age=28, name=mkyong]]

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
17 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Abhay Kokate
4 years ago

in above example how to get person object only through application context?

Vatan
4 years ago

How can we achieve this using annotation?

akbarali
6 years ago

what is the benefit of associate child to parent
or nested bean factory

Pawan Pandey
6 years ago

Thank you sir for above information.

mao duy
11 years ago

Hi mkyong,
Can you explain to me why inner bean method is better than ref method ?
I’m a new comer to Spring;
Thanks;

knguyen
11 years ago
Reply to  mao duy

In general, it’s fine to reference like this, but since the ‘mkyong’ person bean is only used for Customer bean only.
“PersonBean” can use for other bean

naresh
11 years ago

does inner bean supports interface injection?

S@gS.......
11 years ago

To run this code, you have to declare a default constructor. 🙂

abc
9 years ago
Reply to  S@gS.......

Can you please throw some light for the reason of declaring default constructor? I am not able to understand the logic.

akbarali
6 years ago
Reply to  abc

mainly you should to know constructor itself sound to build some thing but in java why constructor ,
when we run the program without making any constructor then jvm create default constructor to create object of super class java.lang.object bcs it is super class of all .
without object you cant run java program bcs java is object oriented language so
default constructor create by Jvm reason behind it..

Bharathiraja
10 years ago
Reply to  S@gS.......

yes, you are right and at the same time you need to declare setter or constructor to set values for Person Class

Vijay
11 years ago

Hi Mkyong,

for the above example I am giving the inner bean with P:namespaces like in the following way


<bean id="Customer"  class="intra4_inner.Customer">
	<property name="per">
	<bean  class="intra4_inner.Person" p:name="Vijay" p:age="25"          
                                            p:address="address" />
	</property>
	</bean>

Then i am getting the following out with NULL VALUES
Customer :[ person = person [ address = null age = null name = null]]

please explain why the null values came.

if it is possible pls give a good example of bean obj creation scope in spring

Thanks in advance
Vijay

Vijay
11 years ago

Hi Mkyong,

Can you please tell me the advantage of using inner bean.

Thanks,

Vijay

soniya
11 years ago

thanks a ton for such a gr8 help on java.

Technical Lobby
12 years ago
 Hi Yong, your site is very great resource of Java.
Congrats for your efforts. 
Amit
14 years ago

Good article! Also another important point about inner beans is that they are always prototypes, the singleton flag is always ignored.

Nommi
14 years ago

hmmmmmm nice idea…………… carryOn on spring..