Java Spring Bean的生命周期管理詳解
Spring Bean的生命周期管理
一、Spring Bean的生命周期
通過以下方式來指定Bean的初始化和銷毀方法, 當Bean為單例時,Bean歸Spring容器管理,Spring容器關閉,就會調用Bean的銷毀方法 當Bean為多例時,Bean不歸Spring容器管理,Spring容器關閉,不會調用Bean的銷毀方法
二、通過@Bean的參數(initMethod ,destroyMethod)指定Bean的初始化和銷毀方法
1、項目結構
2、Person
public class Person { public Person(){ System.out.println("Person 創建瞭..."); } public void init(){ System.out.println("Person 初始化瞭..."); } public void destroy(){ System.out.println("Person 被銷毀瞭..."); } }
3、Bean註冊配置類(單實例)
import com.dashu.bean.Person; import org.springframework.context.annotation.*; @Configuration public class BeanConfig { @Bean(initMethod = "init",destroyMethod = "destroy") public Person person(){ return new Person(); } }
4、測試類
import com.dashu.bean.Person; import com.dashu.config.BeanConfig; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main { public static void main(String[] args) { //加載配置類獲取容器 AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(BeanConfig.class); //獲取Bean Person bean = annotationConfigApplicationContext.getBean(Person.class); //關閉容器 annotationConfigApplicationContext.close(); } }
5、測試結果
6、Bean註冊配置類(多實例)
import com.dashu.bean.Person; import org.springframework.context.annotation.*; @Configuration public class BeanConfig { @Scope("prototype") @Bean(initMethod = "init",destroyMethod = "destroy") public Person person(){ return new Person(); } }
7、測試結果
三、Bean實現接口InitializingBean, DisposableBean
1、Person
import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class Person implements InitializingBean, DisposableBean { public Person(){ System.out.println("Person 創建瞭..."); } @Override public void afterPropertiesSet() throws Exception { System.out.println("Person 初始化瞭..."); } @Override public void destroy() throws Exception { System.out.println("Person 被銷毀瞭..."); } }
2、Bean註冊配置類
import com.dashu.bean.Person; import org.springframework.context.annotation.*; @Configuration public class BeanConfig { @Bean public Person person(){ return new Person(); } }
3、測試結果
四、通過註解@PostConstruct和@PreDestroy
@PostConstruct:標註在Bean的初始化方法上 @PreDestroy:標註在Bean的銷毀方法上
1、Person
import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; public class Person { public Person(){ System.out.println("Person 創建瞭..."); } @PostConstruct public void init(){ System.out.println("Person 初始化瞭..."); } @PreDestroy public void destroy(){ System.out.println("Person 被銷毀瞭..."); } }
2、測試結果
五、使用接口BeanPostProcessor實現類(後置處理器)
1、項目結構
2、Person
import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; @Component public class Person { public Person(){ System.out.println("Person 創建瞭..."); } @PostConstruct public void init(){ System.out.println("Person 初始化瞭..."); } @PreDestroy public void destroy(){ System.out.println("Person 被銷毀瞭..."); } }
3、Bean註冊配置類
import org.springframework.context.annotation.*; @Configuration @ComponentScan({"com.dashu"}) public class BeanConfig { }
4、BeanPostProcessor實現類(後置處理器)
import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; import org.springframework.lang.Nullable; import org.springframework.stereotype.Component; /** * 後置處理器:初始化前後進行處理工作 */ @Component public class MyBeanPostProcessor implements BeanPostProcessor { /** * 初始化之前工作 * @param bean * @param beanName * @return * @throws BeansException */ @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("初始化之前..."+beanName+"=["+bean+"]"); return bean; } /** * 初始化之後工作 * @param bean * @param beanName * @return * @throws BeansException */ @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("初始化之後..."+beanName+"=["+bean+"]"); return bean; } }
5、測試結果
總結
本篇文章就到這裡瞭,希望能夠給你帶來幫助,也希望您能夠多多關註WalkonNet的更多內容!
推薦閱讀:
- SpringBoot中的Bean的初始化與銷毀順序解析
- Spring源碼解析之BeanPostProcessor知識總結
- Java Spring中Bean的作用域及生命周期
- 使用spring容器在初始化Bean時前和後的操作
- @ComponentScan在spring中無效的原因分析及解決方案