Question - Explain inner beans in Spring.
Answer -
A bean can be declared as an inner bean only when it is used as a property of another bean. For defining a bean, the Spring’s XML based configuration metadata provides the use of
element inside the or . Inner beans are always anonymous and they are always scoped as prototypes. For example, let’s say we have one Student class having reference of Person class. Here we will be creating only one instance of Person class and use it inside Student.
Here’s a Student class followed by bean configuration file:
Student.java
- public class Student
- {
- private Person person;
- //Setters and Getters
- }
- public class Person
- {
- private String name;
- private String address;
- //Setters and Getters
- }
studentbean.xml