Spring初始化和銷毀的實現(xiàn)方法
這篇文章主要介紹了Spring初始化和銷毀的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
一 指定初始化和銷毀方法
通過@Bean指定init-method和destroy-method;
@Bean(initMethod="init",destroyMethod="detory")
public Car car(){
return new Car();
}
二 通過讓Bean實現(xiàn)InitializingBean(定義初始化邏輯)
@Component
public class Cat implements InitializingBean,DisposableBean {
public Cat(){
System.out.println("cat constructor...");
}
@Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("cat...destroy...");
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("cat...afterPropertiesSet...");
}
}
三 可以使用JSR250
@PostConstruct:在bean創(chuàng)建完成并且屬性賦值完成;來執(zhí)行初始化方法
@PreDestroy:在容器銷毀bean之前通知我們進行清理工作
@Component
public class Dog implements ApplicationContextAware {
//@Autowired
private ApplicationContext applicationContext;
public Dog(){
System.out.println("dog constructor...");
}
//對象創(chuàng)建并賦值之后調(diào)用
@PostConstruct
public void init(){
System.out.println("Dog....@PostConstruct...");
}
//容器移除對象之前
@PreDestroy
public void detory(){
System.out.println("Dog....@PreDestroy...");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO Auto-generated method stub
this.applicationContext = applicationContext;
}
}
四 可以使用BeanPostProcessor
/**
* 后置處理器:初始化前后進行處理工作
* 將后置處理器加入到容器中
* 在bean初始化前后進行一些處理工作;
* postProcessBeforeInitialization:在初始化之前工作
* postProcessAfterInitialization:在初始化之后工作
*/
@Component
public class MyBeanPostProcessor implements BeanPostProcessor,Ordered {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessBeforeInitialization..."+beanName+"=>"+bean);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
// TODO Auto-generated method stub
System.out.println("postProcessAfterInitialization..."+beanName+"=>"+bean);
return bean;
}
@Override
public int getOrder() {
return 2;
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring boot整合RabbitMQ實例詳解(Fanout模式)
這篇文章主要介紹了spring boot整合RabbitMQ的實例講解(Fanout模式),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-04-04
關(guān)于java開發(fā)的性能問題總結(jié)(必看)
下面小編就為大家?guī)硪黄P(guān)于java開發(fā)的性能問題總結(jié)(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
SpringCloud使用Ribbon實現(xiàn)負載均衡的流程步驟
在微服務(wù)架構(gòu)中,負載均衡是一項關(guān)鍵的技術(shù),它可以確保各個服務(wù)節(jié)點間的負載分布均勻,提高整個系統(tǒng)的穩(wěn)定性和性能,Spring Cloud 中的 Ribbon 就是一種負載均衡的解決方案,本文將深入探討 Ribbon 的原理和在微服務(wù)中的應(yīng)用,需要的朋友可以參考下2024-02-02
為什么Spring和IDEA都不推薦使用 @Autowired 注解
本文主要介紹了為什么Spring和IDEA都不推薦使用 @Autowired 注解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
詳解SpringBoot+Thymeleaf 基于HTML5的現(xiàn)代模板引擎
本篇文章主要介紹了SpringBoot+Thymeleaf 基于HTML5的現(xiàn)代模板引擎,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10

