C
C
Cyrus Smith2015-12-02 02:26:23
Java
Cyrus Smith, 2015-12-02 02:26:23

What is the bean initialization process?

Hello everyone,
There was a question about the bean initialization process. I’ll immediately begin to describe on my fingers, otherwise I’m afraid I will describe indistinctly.
So we have the HelloWorld class:

public class HelloWorld {
  private String message;

  // getter and setter go here
  
  public void init() {
    this.setMessage("null");
    System.out.println("Bean is going through init.");
  }
  
  public void destroy() {
      System.out.println("Bean will destroy now.");
  }

}

There is a class with main:
public class MainApp {

  public static void main(String[] args) {
    AbstractApplicationContext context = 
        new ClassPathXmlApplicationContext("/spring/BeanLifeCycle/Beans.xml");

      HelloWorld obj = (HelloWorld) context.getBean("hello");
      System.out.println(obj.getMessage());
      context.registerShutdownHook();
  }

}

And, of course, the bean configuration in Beans.xml:
<?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="hello" 
        class="spring.BeanLifeCycle.HelloWorld"
        init-method="init"
        destroy-method="destroy">
        <property name="message" value="Property value of message"></property>        
    </bean>

</beans>

Question : Why is it displayed not " Property value of message " (value from the bean), but " null " (value from the init method). After all, as I understand it, the bean is initialized first, then its use and the logical end - destruction. I expected that the message variable would first be equal to " null " and then become the value from the bean. But with these expectations, as always...
Where did I go wrong?
Вывод с консоли:
Pro 02, 2015 12:23:14 DOP. org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org[email protected]2b71fc7e: startup date [Wed Dec 02 00:23:14 CET 2015]; root of context hierarchy
Pro 02, 2015 12:23:14 DOP. org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring/BeanLifeCycle/Beans.xml]
Bean is going through init.
Pro 02, 2015 12:23:14 DOP. org.springframework.context.support.ClassPathXmlApplicationContext doClose
INFO: Closing org[email protected]2b71fc7e: startup date [Wed Dec 02 00:23:14 CET 2015]; root of context hierarchy
null
Bean will destroy now.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Ivanov, 2015-12-02
@CSmith

Метод, указанный в init-method вызывается сразу после создания экземпляра. Тем самым Вы заменяете значение message на "null".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question