Recent Posts

Saturday, 8 June 2019

Nested Beans

Nested Beans
     Declaring a bean configuration in another bean configuration is called as Inner Beans or Nested Beans. In Bean class, if we declare any property of User defined class type then we have to use <bean> tag for the user defined class as value to the respective property under <property> tag.
package com.ashok.spring.core.bean.nested.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Account {
   private String accNo;
   private String accName;
   private String accType;
   private long balance;

   public String getAccNo() {
      return accNo;
   }

   public void setAccNo(String accNo) {
      this.accNo = accNo;
   }

   public String getAccName() {
      return accName;
   }

   public void setAccName(String accName) {
      this.accName = accName;
   }

   public String getAccType() {
      return accType;
   }

   public void setAccType(String accType) {
      this.accType = accType;
   }

   public long getBalance() {
      return balance;
   }

   public void setBalance(long balance) {
      this.balance = balance;
   }

   @Override
   public String toString() {
      return "Account [accNo=" + accNo + ", accName=" + accName + ", accType=" + accType + ", balance=" + balance
            + "]";
   }
}
package com.ashok.spring.core.bean.nested.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Employee {
   private String empName;
   private String empId;
   private String empAddress;
   private Account account;

   public String getEmpName() {
      return empName;
   }

   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpId() {
      return empId;
   }

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

   public String getEmpAddress() {
      return empAddress;
   }

   public void setEmpAddress(String empAddress) {
      this.empAddress = empAddress;
   }
   
   public Account getAccount() {
      return account;
   }

   public void setAccount(Account account) {
      this.account = account;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", empAddress=" + empAddress + ", account="
            + account + "]";
   }
}
<?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-3.0.xsd">
    <bean id="emp"
        class="com.ashok.spring.core.bean.nested.beans.Employee"
        scope="prototype">
        <property name="empName" value="Ashok Kumar" />
        <property name="empId" value="Emp0087" />
        <property name="empAddress" value="Bhimavaram" />
        <property name="account">
            <bean id="acc"
                class="com.ashok.spring.core.bean.nested.beans.Account">
                <property name="accNo" value="123456789" />
                <property name="accName" value="M V Ashok Kumar" />
                <property name="accType" value="Savings" />
                <property name="balance" value="2500" />
            </bean>
        </property>
    </bean>
</beans>
package com.ashok.spring.core.bean.nested.test;

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

import com.ashok.spring.core.bean.nested.beans.Employee;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class TestSpringApplication {
   @SuppressWarnings("resource")
   public static void main(String[] args) {
      String configFile = "/com/ashok/spring/core/bean/nested/config/applicationContext.xml";
      ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
      Employee emp = (Employee) context.getBean("emp");
      System.out.println(emp);
   }
}
Output
Employee [empName=Ashok Kumar, empId=Emp0087, empAddress=Bhimavaram, account=Account [accNo=123456789, accName=M V Ashok Kumar, accType=Savings, balance=2500]]

Next Tutorial : Bean Post Processor

Previous Tutorial : Bean Inheritance

No comments:

Post a Comment