第一種方法,繼承InitializingBean和DisposableBean方法
繼承前者實現(xiàn)afterPropertiesSet方法,繼承后者實現(xiàn)destroy方法,
代碼示例:
定義一個實體類:
public class Info {
}
定義另一個實體類實現(xiàn)InitializingBean和DisposableBean接口:
public class Bus implements InitializingBean,DisposableBean {
private Info info;
public void setInfo(Info info) {
System.out.println("setInfo");
this.info = info;
}
public Info getInfo() {
return info;
}
public Bus(){
System.out.println("bus constr");
}
//InitializingBean的方法
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("==============afterPropertiesSet==========");
}
@Override
public void destroy() throws Exception {
System.out.println("=================destroy===================");
}
}
配置類:
@Configuration
public class AppContext {
@Bean
public Info info(){
return new Info();
}
@Bean
public Bus createBus(){
Bus bus = new Bus();
bus.setInfo(info());
return bus;
}
}
測試類:
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppContext.class);
Bus bus = context.getBean(Bus.class);
System.out.println(bus);
context.close();
}
}
測試結果:
bus constr
setInfo
==============afterPropertiesSet==========
com.zhihao.miao.bean.demo4.Bus@73d4cc9e
=================destroy===================
我們發(fā)現(xiàn)繼承InitializingBean接口實現(xiàn)的afterPropertiesSet方法在屬性設置之后執(zhí)行畜号,繼承DisposableBean接口實現(xiàn)destroy方法在對象銷毀之后執(zhí)行尿这。
Interface to be implemented by beans that need to react once all their properties have been set by a BeanFactory: for example, to perform custom initialization, or merely to check that all mandatory properties have been set.
這個接口的實現(xiàn)在所有屬性被BeanFactory設置之后才被執(zhí)行:比如說簇抵。執(zhí)行自定義的初始化或者檢查雖有必要的屬性是否被設置。
An alternative to implementing InitializingBean is specifying a custom init-method, for example in an XML bean definition. For a list of all bean lifecycle methods, see the BeanFactory javadocs.
實現(xiàn)InitializingBean的替代方法是指定一個自定義的init方法射众,例如在一個XML bean定義中碟摆。 有關所有bean生命周期的方法,請參閱BeanFactory 幫助文檔
Interface to be implemented by beans that want to release resources on destruction. A BeanFactory is supposed to invoke the destroy method if it disposes a cached singleton. An application context is supposed to dispose all of its singletons on close.
這個接口的實現(xiàn)在bean實例被銷毀的時候釋放資源被調用叨橱。BeanFactory支持調用destroy方法處理緩存單列典蜕。
An alternative to implementing DisposableBean is specifying a custom destroy-method, for example in an XML bean definition. For a list of all bean lifecycle methods, see the BeanFactory javadocs.
實現(xiàn)DisposableBean的替代方法是指定一個自定義的destroy方法,例如在XML bean定義罗洗。 有關所有bean生命周期方法愉舔,請參閱BeanFactory 幫組文檔。
第二種方式使用@Bean注解的參數(shù)
定義一個實例類伙菜,并在實例類中定義初始化方法和銷毀方法
public class Bike {
public void init(){
System.out.println("=== bike ===init ====");
}
public void destroy() {
System.out.println("===== bike ===destroy ====");
}
}
定義一個AppContext類轩缤,用@Configuration注解標記一下
@Configuration
public class AppContext {
@Bean(initMethod="init",destroyMethod="destroy")
public Bike createBike(){
return new Bike();
}
}
測試:
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppContext.class);
System.out.println(context.getBean(Bike.class));
context.close();
}
}
執(zhí)行結果:
=== bike ===init ====
com.zhihao.miao.bean.demo5.Bike@68ceda24
===== bike ===destroy ====
第三種使用@PostConstruct和@PreDestroy注解
定義一個實體類:
public class Train {
@PostConstruct
public void initial(){
System.out.println("......initial......");
}
@PreDestroy
public void close(){
System.out.println("......close.........");
}
}
定義一個實體類,配置了@Configuration注解:
@Configuration
public class AppContext {
@Bean
public Train createTrain(){
return new Train();
}
}
測試:
public class Test {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppContext.class);
System.out.println(context.getBean(Train.class));
context.close();
}
}
測試結果:
......initial......
com.zhihao.miao.bean.demo6.Train@4516af24
......close.........