- A bean, without any id and name attribute declared in spring configuration file is called Inner Bean
- It is used for one particular property only
- It declaration is enclosed in <property> or <constructor-arg> directly.
- We can define an id and name attribute for inner bean, but it will be ignored by IOC container
- Inner bean will be anonymous so that you cannot use it anywhere else
- Inner bean can access by enclosed bean only
Spring IOC container
allows us to declare bean inside the bean which cannot be used to outside the
enclosing beans. It is wise to avoid declaring 'ID' and 'SCOPE' attribute for
them because these attributes are ignored by IOC container.
Spring Inner bean Example:
Steps
- Create a new java project named “SpringTutorial” using eclipse IDE. (Here I used java 7)
- Create new package under project (Right click on project – new – others – package) and give the package name “com.springtutorial.bean.innerBean”
- Add the required spring jars (Right click on project – properties- java build path – click on Libraries tab – Click “Add External Jar” button – select below mentioned jars and press ok)
- commons-logging-1.1.jar
- spring-2.5.4.jar
- spring-core-2.5.6.jar
- Create a parent java class under created package
- package
com.springtutorial.bean.innerBean;
public class Parent {private String homeName;private String homeAddress;private String homeCity;private Child child;public String getHomeName() {return homeName;}public void setHomeName(String homeName) {this.homeName = homeName;}public String getHomeAddress() {return homeAddress;}public void setHomeAddress(String homeAddress) {this.homeAddress = homeAddress;}public String getHomeCity() {return homeCity;}public void setHomeCity(String homeCity) {this.homeCity = homeCity;}public Child getChild() {return child;}public void setChild(Child child) {this.child = child;}}
- Create Child Java Classs
-
package com.springtutorial.bean.innerBean;public class Child {private String childName;private String constructorInjection;public Child(String cInjection) {this.constructorInjection = cInjection;}public String getChildName() {return childName;}public void setChildName(String childName) {this.childName = childName;}public String getConstructorInjection() {return constructorInjection;}}
- Create spring bean configuration file named “innerbean.xml” which loads on defined beans in IOC container.
-
<?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="parentBean" class="com.springtutorial.bean.innerBean.Parent"><property name="homeName" value="Bahuchar Krupa"></property><property name="homeAddress" value="Nagarwara"></property><property name="homeCity" value="Banswara"></property><property name="child"><bean class="com.springtutorial.bean.innerBean.Child"><property name="childName" value="noName"></property><constructor-arg value="passConstructor"/> </bean></property></bean></beans>
- Finally, main method loads the beans from IOC container
-
package com.springtutorial.bean.innerBean;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.support.FileSystemXmlApplicationContext;public class InnerBeanSpringExample {public static void main(String[] args) {BeanFactory beanFactory = new FileSystemXmlApplicationContext("C:/personal/springpractice/springworkspace/SpringTutotial/src/com/springtutorial/bean/innerBean/innerbean.xml");Parent parent = (Parent)beanFactory.getBean("parentBean");System.out.println("Read property from parent: "+parent.getHomeName()+" "+parent.getHomeAddress()+" "+parent.getHomeCity());System.out.println("Read property from child: "+parent.getChild().getChildName());System.out.println("From Constructor: "+parent.getChild().getConstructorInjection());}}
-
Run the main method java class:Read property from parent: Bahuchar Krupa Nagarwara BanswaraRead property from child: noNameFrom Constructor: passConstructor
When we use inner bean?Generally we developer make a mistake while making bean configuration file for example<bean id="holiday" class="Holiday" /><bean id="birthday" class="Birthday" /><bean id="sunday" class="Sunday" ><property name="holiday" ref="holiday"/></bean><bean id="monday" class="Monday" /><bean id="tuesday" class="Tuesday" /><bean id="wednesday" class="Wednesday" /><bean id="thurday" class="Thursday" /><bean id="friday" class="Friday" ><property name="birthday" ref="birthday"/></bean><bean id="saturday" class="Saturday" />If beans holiday and birthday are used by only Sunday and Friday so it should not be accessible from anywhere else. So in that case we can use INNER BEAN
No comments:
Post a Comment