美文网首页Java 杂谈SSM
008Spring自定义初始化和销毁方法

008Spring自定义初始化和销毁方法

作者: 编程界的小学生 | 来源:发表于2018-07-10 11:38 被阅读15次

    1、简介

    Bean的生命周期:

    ​ bean的创建--》初始化--》销毁

    bean的创建:

    ​ 单实例:在容器启动的时候就创建

    ​ 多实例:在每次主动获取bean的时候创建

    初始化:

    ​ 对象创建完成,并赋值完成,调用初始化方法

    销毁:

    ​ 单实例:容器管理的时候进行销毁(调用容器的close方法)。

    ​ 多实例:容器不会管理这个bean,所以说容器不会调用销毁方法,需要纯手动调用。

    2、@Bean initMethod&destroyMethod

    2.1、实体类

    public class Car {
    
        public Car() {
            System.out.println("car constructor...");
        }
    
        public void init() {
            System.out.println("car init ...");
        }
    
        public void destory() {
            System.out.println("car destory...");
        }
    }
    

    2.2、配置类

    @Configuration
    public class MainConfig {
    
        @Bean(initMethod = "init", destroyMethod = "destory")
        public Car car() {
            return new Car();
        }
    }
    

    2.3、测试类

    public class BeanTest {
    
        @Test
        public void test1() {
            ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
            System.out.println("容器创建完成...");
    
            ((AnnotationConfigApplicationContext) applicationContext).close();
        }
    
    }
    

    2.4、测试结果

    car constructor...
    car init ...
    容器创建完成...
    car destory...
    

    3、InitializingBean&DisposableBean

    3.1、实体

    @Component
    public class Cat implements InitializingBean, DisposableBean {
    
        public Cat() {
            System.out.println("Cat constructor...");
        }
    
        @Override
        public void destroy() throws Exception {
            System.out.println("cat destroy");
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("cat afterPropertiesSet");
        }
    }
    

    3.2、配置类

    @Configuration
    @ComponentScan(basePackages = "com.chentongwei.spring.annotation.bean.entity.Cat")
    public class MainConfig {
    
        @Bean(initMethod = "init", destroyMethod = "destory")
        public Car car() {
            return new Car();
        }
    }
    

    3.3、测试类

    同【2.3】

    3.4、测试结果

    Cat constructor...
    cat afterPropertiesSet
    car constructor...
    car init ...
    容器创建完成...
    car destory...
    cat destroy
    

    4、@PostConstruct&@PreDestroy

    4.1、实体

    public class Dog {
    
        public Dog() {
            System.out.println("dog constructor...");
        }
    
        // 对象创建并赋值之后
        @PostConstruct
        public void init() {
            System.out.println("dog postconstructor...");
        }
    
        // 在容器移除对象之前
        @PreDestroy
        public void destroy() {
            System.out.println("dog PreDestroy ...");
        }
    }
    

    4.2、配置类

    @Configuration
    @ComponentScan(basePackages = "com.chentongwei.spring.annotation.bean.entity")
    @Import(Dog.class)
    public class MainConfig {
    
        @Bean(initMethod = "init", destroyMethod = "destory")
        public Car car() {
            return new Car();
        }
    }
    

    4.3、测试类

    同【2.3】

    4.4、测试结果

    Cat constructor...
    cat afterPropertiesSet
    dog constructor...
    dog postconstructor...
    car constructor...
    car init ...
    容器创建完成...
    car destory...
    dog PreDestroy ...
    cat destroy
    

    5、广告

    img
    • 今日头条号:编程界的小学生

    相关文章

      网友评论

        本文标题:008Spring自定义初始化和销毁方法

        本文链接:https://www.haomeiwen.com/subject/lxsfpftx.html