I am new in spring.
Please just guide when i should go for inner bean 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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With