Inner bean is a bean defined inside another bean, it can be seen as an inner class. In another word, the inner bean is a bean defined within the scope of another bean. In this case the inner bean can only be use by the outer bean. No other bean in the Spring context can refer to that bean.
So, if you sure that a bean is only use within a single bean it is a good idea to use an inner bean. Inner bean can be injected through setter injection or constructor injection.
Here is an example of Spring configuration for an inner bean injection:
<?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.xsd">
<bean id="racer" class="org.kodejava.spring.core.Racer">
<property name="car">
<bean class="org.kodejava.spring.core.Car">
<property name="maker" value="Ferrari" />
<property name="year" value="2021" />
</bean>
</property>
</bean>
</beans>
In this configuration we use a setter injection. So we use the property
element. Instead of using a ref
attribute for referring to another bean we define the bean using the bean
element inside the property
element. And then we create the Car
bean and sets its properties.
If you want to use a constructor injection you can inject the Car
bean into the Racer
bean by defining a bean inside the constructor-arg
element in the Racer
bean.
Below is our Racer
and Car
classes.
package org.kodejava.spring.core;
public class Racer {
private Car car;
public Racer() {
}
public Racer(Car car) {
this.car = car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "Racer{" +
"car=" + car +
'}';
}
}
package org.kodejava.spring.core;
public class Car {
private String maker;
private int year;
public void setMaker(String maker) {
this.maker = maker;
}
public void setYear(int year) {
this.year = year;
}
@Override
public String toString() {
return "Car{" +
"maker='" + maker + "'" +
", year=" + year +
'}';
}
}
Let’s create our Demo
class to run the program:
package org.kodejava.spring.core;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo {
public static void main(String[] args) {
var context = new ClassPathXmlApplicationContext("inner-bean.xml");
Racer racer = (Racer) context.getBean("racer");
System.out.println("Racer = " + racer);
context.close();
}
}
Here is the output of our program:
Racer = Racer{car=Car{maker='Ferrari', year=2021}}
Maven Dependencies
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.23</version>
</dependency>
</dependencies>
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024