Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are inner beans in Spring? [closed]

Tags:

java

spring

I am new in spring.

  1. How to inject inner bean in spring ?
  2. what is main purpose of inner bean in spring

Please just guide when i should go for inner bean in spring

like image 396
dhaval joshi Avatar asked Oct 14 '16 11:10

dhaval joshi


People also ask

What are inner beans in Spring?

As you know Java inner classes are defined within the scope of other classes, similarly, inner beans are beans that are defined within the scope of another bean. Thus, a <bean/> element inside the <property/> or <constructor-arg/> elements is called inner bean and it is shown below.

Does Spring support inner beans?

In Spring framework, whenever a bean is used for only one particular property, it's advise to declare it as an inner bean. And the inner bean is supported both in setter injection ' property ' and constructor injection ' constructor-arg '. See a detail example to demonstrate the use of Spring inner bean.

What is the scope of an inner bean?

Inner beans are always anonymous and they are always created with the outer bean. It is not possible to inject inner beans into collaborating beans other than into the enclosing bean. So an inner bean has no scope and basically can't be used by anything other than the enclosing bean.


1 Answers

In Spring framework, whenever a bean is used for only one particular property, it’s advise to declare it as an inner bean. And the inner bean is supported both in setter injection ‘property‘ and constructor injection ‘constructor-arg‘. Like Inner classes are the classes which are defined inside the scope of another class. Similarly inner beans are the beans which are defined in the scope of another bean.

Injecting Inner Beans

<bean id="outer_bean" class="OuterBean">
      <property name="innerbean">
           <bean  class="InnerBean"/>
      </property>
</bean>

As you can see, instead of using ref attribute of property tag, beans are defined inside property tag. In this case an instance of InnerBean class will be created and wired in to innerbean property of OuterBean class.

We can use inner beans in constructor-arg tag as well like below

<bean id="outer_bean" class="OuterBean">
       <constructor-arg>
            <bean  class="InnerBean"/>
       </ constructor-arg>
</bean>

In this case an instance of InnerBean class will be created and will be passed as an constructor argument of OuterBean class.

Consider this example

Student class

public class Student {
       private String name ;
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       } 
}

Room class

public class Room
{
       private int roomNumber;
       private Student allotedTo;      
              public int getRoomNumber() {
              return roomNumber;
       }
       public void setRoomNumber(int roomNumber) {
              this.roomNumber = roomNumber;
       }
       public Student getAllotedTo() {
              return allotedTo;
       }
       public void setAllotedTo(Student allotedTo) {
              this.allotedTo = allotedTo;
       }
       @Override
       public String toString() {
       return "Room [roomNumber=" + roomNumber + ", allotedTo=" + allotedTo.getName()
                           + "]";
       }      
}

beans entry in 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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
    <bean id="room" class="Room">
              <property name="roomNumber" value="101" />
              <property name="allotedTo">
                     <bean class="Student">
                     <property name="name" value="joe bloggs" />
                     </bean>
              </property>
    </bean>
</beans>

TestInnerBeanDependency class

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestInnerBeanDependency {
       public static void main(String[] args) {
              ApplicationContext context =
                           new ClassPathXmlApplicationContext("beans.xml");
              Room room = (Room)context.getBean("room");             
              System.out.println(room);             
       }
}

Run the TestInnerBeanDependency

Room [roomNumber=101,allotedTo=joe bloggs]

An inner bean definition does not require a defined id or name; if specified, the container does not use such a value as an identifier. The container also ignores the scope flag on creation: Inner beans are always anonymous and they are always created with the outer bean. It is not possible to inject inner beans into collaborating beans other than into the enclosing bean or to access them independently.

I hope it would make you understand the Inner beans. Thanks.

like image 151
Nayan Sharma Avatar answered Oct 13 '22 04:10

Nayan Sharma