Surace Surace - 23 days ago 6
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".

ClassA

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;
}

}


ClassB

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


ClassC

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();
}
System.out.println(s);
}
}


ClassD

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) {
//SpringApplication.run(Question1Application.class, args);
System.out.println("PREPARE");
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("springconfig.xml");
System.out.println("BEGIN");
ClassB classB = context.getBean("objectB",ClassB.class);
ClassC classC = context.getBean("objectF",ClassC.class);
System.out.println("END");
context.close();
}
}


Bean config file is

?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="objectA" class="com.example.ClassA" abstract="true">
<property name="text" value="Things!"></property>
</bean>

<bean id="objectB" class="com.example.ClassB" scope="prototype" parent="objectA"
init-method="settings">
</bean>

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

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

<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">
<list>
<ref bean="objectB"></ref>
<bean class="com.example.ClassB">
<property name="text" value="lot of things!"></property>
</bean>
</list>
</property>
</bean>




The output of the program is :

PREPARE
-new-
constructing classD object
-new-
constructing classB object
-new-
This object contains : lots of things!
BEGIN
-new-
constructing classB object
END

Answer

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);
Comments