Inner Bean in Spring Example
When a bean is used for only one particular property, we can declare them as inner beans.

Directory Structure
structure

employee.java

package com.javainfinite;

public class employee {
    
    private String ename;
    private int id;

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
 
}

employeedependent.java

package com.javainfinite;

public class employeedependent {
    
    employee employee;
    employee employee1;

    public employee getEmployee1() {
        return employee1;
    }

    public void setEmployee1(employee employee1) {
        this.employee1 = employee1;
    }

    public employee getEmployee() {
        return employee;
    }

    public void setEmployee(employee employee) {
        this.employee = employee;
    }
    
    public void display()
    {
        System.out.println("Employee Name: "+employee.getEname()+" ID: "+employee.getId());
        System.out.println("Employee Name: "+employee1.getEname()+" ID: "+employee1.getId());
    }
}

operational.java

package com.javainfinite;

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


public class operational {
    
    public static void main(String args[])
    {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        employeedependent emp=(employeedependent) ac.getBean("employeedependent");
        emp.display();
    }
    
}

applicationContext.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


<bean id="employeedependent" class="com.javainfinite.employeedependent">
    <property name="employee">
        <bean class="com.javainfinite.employee">
            <property name="ename" value="Alpha"/>
            <property name="id" value="001"/>
        </bean>
    </property>
    <property name="employee1" ref="employee2"/>
</bean>

<bean id="employee2" class="com.javainfinite.employee">
    <property name="ename" value="Beta"/>
    <property name="id" value="002"/>
</bean>

</beans>

Output
op1

 

 

 

By Sri

Leave a Reply

Your email address will not be published. Required fields are marked *