bean初始化和销毁方法的几种实现

第一种方法,继承InitializingBean和DisposableBean方法
继承前者实现afterPropertiesSet方法,继承后者实现destroy方法,
代码示例:
定义一个实体类:

public class Info {
}

定义另一个实体类实现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===================

我们发现继承InitializingBean接口实现的afterPropertiesSet方法在属性设置之后执行,继承DisposableBean接口实现destroy方法在对象销毁之后执行。

bean初始化和销毁方法的几种实现_第1张图片
InitializingBean接口

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.
这个接口的实现在所有属性被BeanFactory设置之后才被执行:比如说。执行自定义的初始化或者检查虽有必要的属性是否被设置。

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.
实现InitializingBean的替代方法是指定一个自定义的init方法,例如在一个XML bean定义中。 有关所有bean生命周期的方法,请参阅BeanFactory 帮助文档

bean初始化和销毁方法的几种实现_第2张图片
DisposableBean接口

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.
这个接口的实现在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.
实现DisposableBean的替代方法是指定一个自定义的destroy方法,例如在XML bean定义。 有关所有bean生命周期方法,请参阅BeanFactory 帮组文档。

第二种方式使用@Bean注解的参数
定义一个实例类,并在实例类中定义初始化方法和销毁方法

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();
    }
}

执行结果:

=== 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.........

你可能感兴趣的:(bean初始化和销毁方法的几种实现)