三种方式在spring容器中为bean实现初始化方法和销毁方法

  容器管理bean的生命周期,我们可以自定义初始化和销毁方法;容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法。

  下面演示三种bean的 初始化和销毁方法。

  第一种,利用@Bean注解中自带的 initMethod()和 destroyMethod()。

  第二种,实现 InitializingBean和 DisposableBean接口中的 afterPropertiesSet()和 destroy()方法。

  第三种,可以使用JSR250,@PostConstruct(在bean创建完成并且属性赋值完成;来执行初始化方法)和 @PreDestroy(在容器销毁bean之前通知我们进行清理工作)

public class Car {
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Car() {
        System.out.println("Car Constructor.....");
    }
    public void  init()
    {
        System.out.println("Car init .....");
    }
    public void destory()
    {
        System.out.println("Car destory .....");
    }
}
public class Cat implements InitializingBean, DisposableBean {
    public Cat() {
        System.out.println("Cat 构造方法 。。。。。。");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("Cat 销毁方法");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Cat 初始化方法 。。。。。。");
    }
}@Configuration
 
 
public class Dog {

public Dog() {
System.out.println("dog construct......");
}

//对象创建并赋值之后调用
@PostConstruct
public void init()
{
System.out.println("dog init @PostConstruct .....");
}
@PreDestroy
public void destory()
{
System.out.println("dog destory @PreDestroy .....");
}
@Import(Dog.class)
@Configuration
public class Config1 {

    @Bean(initMethod = "init",destroyMethod = "destory")
    public Car car (){
        return new Car();
    }
    @Bean
    public Cat cat(){
        return new  Cat();
    }
}
public class App {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(Config1.class);
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String n:beanDefinitionNames)
        {
            System.out.println(n);
        }
        context.close();
    }
}

运行结果:

dog construct......
dog init @PostConstruct .....
13:41:29.489 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'car'
Car Constructor.....
Car init .....
13:41:29.522 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'cat'
Cat 构造方法 。。。。。。
Cat 初始化方法 。。。。。。
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
config1
com.yuan.beancycle.model.Dog
car
cat
13:41:29.577 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@544a2ea6, started on Mon Aug 05 13:41:29 CST 2019
Disconnected from the target VM, address: '127.0.0.1:64811', transport: 'socket'
Cat 销毁方法
Car destory .....
dog destory  @PreDestroy .....

猜你喜欢

转载自www.cnblogs.com/mingyuan1031/p/11302488.html