Spring Bean's life
When you work directly in Java, you can do anything you like with your objects and do not always need to rely on the container lifecycle.
Preface:
Bean corresponding instantiated and not immediately after the start Ioc container, when the container only has all Ioc objectsBeanDefinition(Bean对象在Spring中的描述,包含该Bean在容器中实例化所需的信息,通过解析xml/javaconfig后所得)
Spring Bean Life Cycle Management
Spring bean factory
To manage Beans
the life cycle, by implementing InitializingBean and DisposableBean
These two interfaces only one way we can declare 初始化/关闭
resource in the bean
Of course, throughout the life cycle also provides some Call the Back Methods , these methods are similar to the Servlet
implementations of the listener
- When
Bean
inpost-initialization
time stage, we can achieveInitializingBean
to implement the interfaceafterPropertiesSet()
- When
Bean
inpre-destruction
time stage, we can implementDisposableBean
interfaces to implementdestroy()
method
Spring Bean's life cycle chart
Code Time
In Rick
class to Spring container, a method for performing sequential order Spring container
Rick.java
public class Rick implements InitializingBean, DisposableBean {
public Rick(){
System.out.println("Rick is in [Rick] - Rick()");
}
public void customInit(){
System.out.println("Rick is in [Rick] - customInit()");
}
@PostConstruct
public void postConstruct(){
System.out.println("Rick is in [@PostConstruct] - postConstruct()");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("Rick is in [InitializingBean] - afterPropertiesSet()");
}
@PreDestroy
public void preDestroy(){
System.out.println("Rick is in [@PreDestroy] - preDestroy()");
}
@Override
public void destroy() throws Exception {
System.out.println("Rick is in [DisposableBean] - destroy()");
}
public void customDestroy(){
System.out.println("Rick is in [Rick] - customDestroy()");
}
}
JoinBeanPostProcess
RickBeanPostProcess.java
public class RickBeanPostProcess implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof Rick) {
System.out.println("Rick is in [BeanPostProcessor] - postProcessBeforeInitialization()");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof Rick) {
System.out.println("Rick is in [BeanPostProcessor] - postProcessAfterInitialization()");
}
return bean;
}
}
Configuration class
RickConfig.java
@Configuration
public class RickConfig {
/**
* 指定自定义初始化方法和自定义销毁方法
* @return Rick
*/
@Bean(initMethod = "customInit",destroyMethod = "customDestroy")
public Rick rick(){
return new Rick();
}
@Bean
public RickBeanPostProcess rickBeanPostProcess(){
return new RickBeanPostProcess();
}
}
Test category
Client.java
public class Client {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(RickConfig.class);
((AnnotationConfigApplicationContext) applicationContext).close();
}
}
The final results were as follows
Rick is in [Rick] - Rick()
Rick is in [BeanPostProcessor] - postProcessBeforeInitialization()
Rick is in [@PostConstruct] - postConstruct()
Rick is in [InitializingBean] - afterPropertiesSet()
Rick is in [Rick] - customInit()
Rick is in [BeanPostProcessor] - postProcessAfterInitializa
Rick is in [@PreDestroy] - preDestroy()
Rick is in [DisposableBean] - destroy()
Rick is in [Rick] - customDestroy()
reference
Spring Framework Documentation