Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Spring Inner bean

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 354
    Comment on it

    Inner beans in spring are those beans which are under the scope of another beans. In spring framework normally to inject a bean, we define it first on container with some id and pass the reference of that bean wherever dependency is needed, using the ref attribute.

    Spring inner bean Example:

    beans.xml

    <bean id="CustomerBean" class="com.evon.example.Customer">
        <property name="person" ref="PersonBean" />
    </bean>
    <bean id="PersonBean" class="com.evon.example.Person">
        <property name="name" value="Sumit" />
        <property name="address" value="address1" />
        <property name="age" value="28" />
    </bean>
    

    But in case where bean is used only by the one bean, then we don't require to define it seperately. We can define inner bean as follows:

    Example code of Spring inner bean

    beans.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:c="http://www.springframework.org/schema/c"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd">
    
      <bean id="college" class="com.evon.example.innerBeanExample.College">
        <property name="collegeName" value="MIT"/> 
        <property name="student">
            <bean class="com.evon.example.innerBeanExample.Student">
                <property name="studentName" value="Sumit"/>
                <property name="age" value="22"/>
                <property name="course" value="MCA"/>
            </bean>
        </property>
      </bean>  
     </beans>
    

    In above configuration dependency is injected through setter, in case of constructor replace the above bean config from following code:

      <bean id="college" class="com.evon.example.innerBeanExample.College">
        <constructor-arg value="MIT"/>
        <constructor-arg>
            <bean class="com.evon.example.innerBeanExample.Student">
                <property name="studentName" value="Sumit"/>
                <property name="age" value="22"/>
                <property name="course" value="MCA"/>
            </bean>
        </constructor-arg>
      </bean>
    

    College.java

    package com.evon.example.innerBeanExample;
    public class College {
        private String collegeName;
        private Student student;
    
        College(String collegeName, Student student){
            this.collegeName=collegeName;
            this.student = student;
        }
        College(Student student){
            this.collegeName="MIT";
            this.student = student;
        }    
        public String getCollegeName() {
            return collegeName;
        }    
        public void setCollegeName(String collegeName) {
            this.collegeName = collegeName;
        }    
        public Student getStudent() {
            return student;
        }    
        public void setStudent(Student student) {
            this.student = student;
        }
    }
    

    Student.java

    package com.evon.example.innerBeanExample;
    public class Student {
        private String studentName;
        private int age;
        private String course;
        public String getStudentName() {
            return studentName;
        }
        public void setStudentName(String studentName) {
            this.studentName = studentName;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getCourse() {
            return course;
        }
        public void setCourse(String course) {
            this.course = course;
        }
    }
    

    Main.java

    package com.evon.example.innerBeanExample;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    import com.evon.example.innerBeanExample.Student;
    public class Main {
        public static void main(String[] args) {
            ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/java/com/evon/example/innerBeanExample/bean.xml");
            College college = (College) context.getBean("college");
            Student student = college.getStudent();
            System.out.println("College name : "+college.getCollegeName());
            System.out.println("Student name : "+student.getStudentName());
            System.out.println("Student age : "+student.getAge());
            System.out.println("Student course : "+student.getCourse());
        }
    }
    

    Output:

    College name : MIT
    Student name : Sumit
    Student age : 22
    Student course : MCA
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: