Java examples on org.springframework
- How do I declare a bean in Spring application?
- How do I use InitializingBean and DisposableBean interfaces in Spring?
- How do I create beans through factory method?
- How do I initialize and destroy beans in Spring?
- How do I define bean scoping in Spring?
- How do I defaulting init-method and destroy method in Spring?
- How do I inject a bean through constructors?
- How do I inject collections using map element in Spring?
- How do I inject collections using list element in Spring?
- How do I inject into bean properties?
- How do I wire / inject a null value in Spring?
- How do I inject collections using set element in Spring?
- How do I inject collections properties in Spring?
- How do I wire properties with Spring's p namespace?
- How do I inject collections using props element in Spring?
- How do I define inner bean in Spring?
- How do I call static method using Spring EL?
- How do I inject beans, properties and methods using Spring EL?
- How do I handle or avoid null value in SpEL?
- How do I do math operations using Spring EL?
How do I define inner bean in Spring?
Inner bean is a bean define 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 instead. Inner bean can be injected through setter injection or constructor injection.
Here is an example of Spring configuration for an inner bean injection:
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 you a constructor injection you can inject the Car bean into the Racer bean by defining a bean inside of the constructor-arg element in the Racer bean.
Below is our Racer and Car classes.
package org.kodejava.example.spring.innerbean;
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.example.spring.innerbean;
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.example.spring.innerbean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"InnerBean.xml"});
Racer racer = (Racer) context.getBean("racer");
System.out.println("Racer = " + racer);
}
}
Here is the output of our program:
Racer = Racer{car=Car{maker='Ferrari', year=2012}}