Spring Bean Lifecycle: Definition, Initialization, Invocation, and Destruction
This article explains the complete Spring Bean lifecycle—including definition via XML, various initialization methods, three ways to obtain and use a bean, and bean destruction techniques—illustrated with Java code snippets and a diagram for clear understanding.
First, a brief overview (the following serves as a reference template for answering interview questions):
1. Instantiating a Bean – essentially a new operation.
2. Configuring the Bean according to the Spring context – i.e., IoC injection.
3. If the Bean implements BeanNameAware, Spring calls setBeanName(String) with the Bean's id from the configuration file.
4. If the Bean implements BeanFactoryAware, Spring calls setBeanFactory(BeanFactory), passing the BeanFactory itself (useful for obtaining other Beans).
5. If the Bean implements ApplicationContextAware, Spring calls setApplicationContext(ApplicationContext), providing the full application context.
6. If the Bean is associated with a BeanPostProcessor, Spring invokes postProcessBeforeInitialization(Object, String) before the Bean's initialization completes.
7. If the Bean definition includes an init-method attribute, Spring automatically calls that method after property injection.
8. After initialization, if a BeanPostProcessor is present, Spring calls postProcessAfterInitialization(Object, String).
Note: After the above steps, the Bean is ready for use. By default it is a singleton, so repeated look‑ups return the same instance unless the scope is changed in the configuration.
9. When the Bean is no longer needed, Spring enters the destruction phase; if the Bean implements DisposableBean, its destroy() method is invoked.
10. Finally, if a destroy-method attribute is defined, Spring calls that method automatically.
1. Bean Definition
Spring typically defines Beans in an XML configuration file, for example:
<?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-2.0.xsd">
<bean id="HelloWorld" class="com.pqf.beans.HelloWorld">
<property name="msg">
<value>HelloWorld</value>
</property>
</bean>
</beans>This configuration defines a Bean with the identifier HelloWorld. Multiple Beans can be defined in the same file.
2. Bean Initialization
There are two common ways to initialize a Bean.
1) Using the init-method attribute in the configuration file
Define an initialization method in the Bean class, e.g. init():
public class HelloWorld {
public String msg = null;
public Date date = null;
public void init() {
msg = "HelloWorld";
date = new Date();
}
// ... other members ...
}Then specify init-method="init" on the <bean> element.
2) Implementing org.springframework.beans.factory.InitializingBean
Implement the interface and provide afterPropertiesSet():
public class HelloWorld implements InitializingBean {
public String msg = null;
public Date date = null;
public void afterPropertiesSet() {
msg = "Hello to the world!";
date = new Date();
}
// ... other members ...
}Spring will invoke afterPropertiesSet() after all properties have been set, eliminating the need for an init-method attribute.
3. Bean Invocation
Beans can be obtained and used in three ways:
1) Using BeanWrapper
HelloWorld hw = new HelloWorld();
BeanWrapper bw = new BeanWrapperImpl(hw);
bw.setPropertyValue("msg", "HelloWorld");
System.out.println(bw.getPropertyValue("msg"));2) Using BeanFactory
InputStream is = new FileInputStream("config.xml");
XmlBeanFactory factory = new XmlBeanFactory(is);
HelloWorld hw = (HelloWorld) factory.getBean("HelloWorld");
System.out.println(hw.getMsg());3) Using ApplicationContext
ApplicationContext ctx = new FileSystemXmlApplicationContext("config.xml");
HelloWorld hw = (HelloWorld) ctx.getBean("HelloWorld");
System.out.println(hw.getMsg());4. Bean Destruction
1) Using the destroy-method attribute in the configuration file
Define a cleanup method in the Bean class and reference it with destroy-method. Spring will call it automatically when the Bean is destroyed.
2) Implementing org.springframework.beans.factory.DisposableBean
If the Bean implements DisposableBean, Spring invokes its destroy() method during the destruction phase, so the Bean must provide that method.
Diagram:
Selected common interview questions and technical points help developers fill knowledge gaps.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
