Surace Surace - 1 year ago 180
Java Question

Spring bean order of execution

I have the following classes, main class and bean configuration file . The result of the program is also given. My question is that I am not getting the idea how this output is obtained? I am learning spring and I am unable to understand the order in which this program gave an output. I thought in the third line of output there must be "constructing classB object" but the result shows "there is constructing classD object".


public abstract class ClassA {
private String text;

public ClassA(){
System.out.println("- new -");

public String getText() {
return text;

public void setText(String text) {
this.text = text;



public class ClassB extends ClassA {
public void settings(){
System.out.println("Constructing ClassB object");


public class ClassC {
private List<ClassA> items = new ArrayList<>();

public List<ClassA> getItems(){
return items;

public void setItems(List<ClassA> items) {
this.items = items;

public void print(){
String s = "This object contains: ";
for(ClassA item : items){
s+= item.getText();


public class ClassD extends ClassA {
public void settings(){
System.out.println("Constructing ClassD Object");

Main method is

public class Question1Application {

public static void main(String[] args) {
//, args);
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("springconfig.xml");
ClassB classB = context.getBean("objectB",ClassB.class);
ClassC classC = context.getBean("objectF",ClassC.class);

Bean config file is

?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<bean id="objectA" class="com.example.ClassA" abstract="true">
<property name="text" value="Things!"></property>

<bean id="objectB" class="com.example.ClassB" scope="prototype" parent="objectA"

<bean id="objectC" class="com.example.ClassD" scope="prototype" parent="objectA"

<bean id="objectD" class="com.example.ClassD" lazy-init="true" parent="objectA"

<bean id="objectE" class="com.example.ClassD" parent="objectA" init-method="settings"></bean>

<bean id="objectF" class="com.example.ClassC" init-method="print">
<property name="items">
<ref bean="objectB"></ref>
<bean class="com.example.ClassB">
<property name="text" value="lot of things!"></property>

The output of the program is :

constructing classD object
constructing classB object
This object contains : lots of things!
constructing classB object

Answer Source

The example well demonstrates the difference between the singleton and prototype scopes.

Instances of the first one will have been created immediately the context is started up and an IOC controller should support their entire lifecycle.

In contrast to the singletons, for the prototype scope, a container will create an instance by our demand. One request - a new well-configured object from a context is created. A container doesn't take care of such type of beans stopping thinking about them after giving away.

To put it simply, let's go through each statement:

  1. PREPARE - context startup - the beginning of singleton initialisations
  2. constructing classD object - it is a singleton
  3. constructing classB object - the objectF is a singleton, there is a need to create a ClassB instance to initialise it
  4. BEGIN - all singletons are bootstrapped
  5. constructing classB object - due to context.getBean("objectB",ClassB.class);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download