Explain inner beans with example

Spring  allows Inner Beans declaration. We can declare beans inside a bean. But, it has few restriction. Inner Beans are like annonymous beans, they are created and used on the fly. these beans cannot be used outside the enclosing beans. So, it is wise to avoid declaring the ‘ID‘ or ‘SCOPE‘ attributes for them. Because, these values will be ignored by the container. Only the enclosing beans can access them.

Employee.java

package com.thecafetechno.innerbean

public class Employee {

	private String empId;
	private String name;
	private Address address;

	public Employee(){
	}

	public String getEmpId() {
		return empId;
	}

	public void setEmpId(String empId) {
		this.empId = empId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}
}

Address.java

package com.thecafetechno.innerbean

public class Address {

	private String street;
	private String city;
	private String pincode;

	public Address(){

	}

	public String getStreet() {
		return street;
	}

	public void setStreet(String street) {
		this.street = street;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getPincode() {
		return pincode;
	}

	public void setPincode(String pincode) {
		this.pincode = pincode;
	}

}

ConstructorInjection.java

package com.thecafetechno.innerbean

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

public class ConstructorInjection {

	public static void main(String args[]){

		Resource xmlResource = new FileSystemResource("application-Context.xml");
		BeanFactory factory = new XmlBeanFactory(xmlResource);

		Employee employee = (Employee)factory.getBean("employeeBean");

		Address address = employee.getAddress();
		
		System.out.println(employee.getName());
		System.out.println(employee.getEmpId());
		System.out.println(address.getCity());
		System.out.println(address.getStreet());
		System.out.println(address.getPincode());
	}
}

application-Context.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
   
 <bean id="employeeBean" class="com.thecafetechno.innerbean.Employee">
        <property name="name" value="karan"/>
        <property name="empId"  value="1911"/>
        <property name="address">
            <bean class="com.thecafetechno.innerbean">
                <property name="street">
                    <value>Java corner</value>
                </property>
                <property name="city">
                    <value>noida</value>
                </property>
                <property name="pincode">
                    <value>201301</value>
                </property>
            </bean>
        </property>
    </bean>
</beans>