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

Fruits.java

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;
	}
}

FruitsDisplay.java

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");
	  }

}

applicationContext.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:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	http://www.springframework.org/schema/tx 
    	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<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" />
			</bean>
		</property>
		<property name="fruits2" ref="fruitsDisplay2" />
	</bean>

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

</beans>

MainTest.ava

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");
		fruitsdis.display();
		
	}
}

Output

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
      Advertisements