Spring inner bean examples

  • Facebook
  • Google
  • LinkedIn
  • Twitter

when a bean used only as a property of another bean then it can be declared as an inner bean.

Here are the some points of Spring inner beans which need to remember

  • When a bean does not use in other beans, then it can be declared as an inner bean.
  • Inner bean does not have an id which is associated with it but you can provide id to inner bean and it is correct, inner bean will not be shared outside of its surrounding bean. Spring ignore the value of the id attribute of an inner bean.
  • The scope of an inner bean is always prototype and container will ignore the value of the scope attribute of an inner bean.

Spring inner bean example


package com.javatutsworld;

public class Fruits {
	private String fruitsName;
	private int qty;

	public String getFruitsName() {
		return fruitsName;

	public void setFruitsName(String fruitsName) {
		this.fruitsName = fruitsName;

	public int getQty() {
		return qty;

	public void setQty(int qty) {
		this.qty = qty;


package com.javatutsworld;

public class FruitsDisplay {
	  Fruits fruits1;
	  Fruits fruits2;

	  public Fruits getFruits1() {
		return fruits1;

	  public void setFruits1(Fruits fruits1) {
		this.fruits1 = fruits1;

	  public Fruits getFruits2() {
		return fruits2;

	  public void setFruits2(Fruits fruits2) {
		this.fruits2 = fruits2;

	  public void display(){
	    System.out.println("Fruits Name: "+fruits1.getFruitsName()+":"+" Fruits Quantity: "+fruits1.getQty()+"kg");
	    System.out.println("Fruits Name: "+fruits2.getFruitsName()+":"+" Fruits Quantity: "+fruits2.getQty()+"kg");



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

	<bean id="fruitsDisplay" class="com.javatutsworld.FruitsDisplay">
		<property name="fruits1">
			<bean class="com.javatutsworld.Fruits">
				<property name="fruitsName" value="Apple" />
				<property name="qty" value="5" />
		<property name="fruits2" ref="fruitsDisplay2" />

	<bean id="fruitsDisplay2" class="com.javatutsworld.Fruits">
		<property name="fruitsName" value="Orange" />
		<property name="qty" value="10" />



package com.javatutsworld;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {
	public static void main(String[] args) {
		ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");	
		FruitsDisplay fruitsdis= (FruitsDisplay)context.getBean("fruitsDisplay");


Nov 7, 2015 6:23:52 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2808b3: startup date [Sat Nov 07 06:23:52 EST 2015]; root of context hierarchy
Nov 7, 2015 6:23:52 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Nov 7, 2015 6:23:53 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@56f631: defining beans [fruitsDisplay,fruitsDisplay2]; root of factory hierarchy
Fruits Name: Apple: Fruits Quantity: 5kg
Fruits Name: Orange: Fruits Quantity: 10kg