• Spring 之 Bean 的生命周期


      Bean的生命周期主要包括:创建---->初始化---->销毁 这几个过程,Spring中有很多管理Bean的生命周期的方式,下面我们就列举几种常用的.

    一、通过@Bean注解指定

      1、自定义Bean

    public class Person {
        private String name;
        private int age;
    
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
            System.out.println("创建Person对象....");
        }
    
        public void init() {
            System.out.println("Person的初始化....");
        }
    
        public void destory() {
            System.out.println("Person的销毁....");
        }
    }

      2、配置类

    // 标记这是一个Spring配置类
    @Configuration
    public class SpringConfiguration {
        // 指定初始化方法和销毁方法,init和destory的是Bean里面的方法名
        @Bean(initMethod = "init",destroyMethod = "destory")
        // 通过@Scope注解来指定创建的Bean是单例的还是多例的
        // @Scope("prototype")
        public Person person(){
            return new Person("xiaomaomao",22);
        }
    }
    

      3、测试类

    public class SpringDemo {
    
        @Test
        public void springTest01() throws Exception {
            // 单例的情况下,容器启动完成就会调用构造方法创建Bean的实例并进行初始化工作
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
            // 多例的情况下,容器启动完成时并不会创建Bean对象,只有在实际获取的时候才去调用构造方法创建Bean的实例,并初始化Bean
            Person person = context.getBean("person", Person.class);
            System.out.println(person);
            // 如果是单例的情况,调用close()方法对Bean对象进行销毁;
            // 如果是多例的情况,调用close()方法不会对Bean对象进行销毁,Bean何时销毁由容器自己去决定.
            context.close();
        }
    }

      4、测试结果

    // 容器启动完成时就调用构造方法创建对象,并进行Bean实例的初始化工作
    创建Person对象....
    Person的初始化....
    com.spring01.bean.Person@7ea37dbf
    // 调用close()方法进行Bean实例的销毁
    Person的销毁....
    

      

    二、自定义类实现InitializingBean和Disposable接口

      1、自定义类实现InitializingBean接口,并重写afterPropertiesSet()方法,定义初始化逻辑;实现Disposable接口,并重写destory()方法,定义销毁逻辑.

    public class Tiger implements InitializingBean,DisposableBean {
        // 单实例情况下,此方法在容器启动完成之后调用
        // 多实例情况下,在获取Bean实例的时候调用
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("Tiger initial....");
        }
        // 单实例情况下,此方法在容器调用close()执行销毁方法,
        // 多实例情况下,不能自行销毁,由Spring管理销毁的时机
        @Override
        public void destroy() throws Exception {
            System.out.println("Tiger destory....");
        }
    }
    

      2、配置类

    // 标记这是一个Spring配置类
    @Configuration
    public class SpringConfiguration {
        // 指定初始化方法和销毁方法,init和destory的是Bean里面的方法名
        @Bean(initMethod = "init",destroyMethod = "destory")
        public Person person(){
            return new Person("xiaomaomao",22);
        }
    
        @Bean
        public Tiger tiger(){
            return new Tiger();
        }
    }
    

      3、测试类

    public class SpringDemo {
    
        @Test
        public void springTest01() throws Exception {
            // 单例的情况容器启动完成调用了Person的构造方法创建Person对象,并且初始化Person对象,初始化完成之后调用Tiger的afterPropertiesSet()方法
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
            Person person = context.getBean("person", Person.class);
            // 多例的情况下获取Bean的时候才会调用Tiger的afterPropertiesSet()方法
            Tiger tiger = context.getBean("tiger", Tiger.class);
            // 单例的情况下调用close()方法会调用Lion的destory()方法进行销毁动作
            // 多例的情况下,调用close()方法不会调用Lion的destory()方法进行销毁动作
            context.close();
        }
    }

      4、测试结果

    创建Person对象....
    Person的初始化....
    Tiger initial....
    Tiger destory....
    Person的销毁....
    

      

    三、Spring后置处理器(BeanPostProcessor)

      1、自定义类实现BeanPostProcessor,在postProcessBeforeInitialization()方法中定义初始化之前的逻辑,在postProcessAfterInitialization()方法中定义初始化之后的逻辑

    public class MyBeanPostProcessor implements BeanPostProcessor {
    
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("MyBeanPostProcessor..........postProcessBeforeInitialization");
            return bean;
        }
    
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println("MyBeanPostProcessor..........postProcessAfterInitialization");
            return bean;
        }
    }
    

      2、配置类

    // 标记这是一个Spring配置类
    @Configuration
    public class SpringConfiguration {
        // 指定初始化方法和销毁方法,init和destory的是Bean里面的方法名
        @Bean(initMethod = "init", destroyMethod = "destory")
        public Person person() {
            return new Person("xiaomaomao", 22);
        }
    
        @Bean
        public Tiger tiger() {
            return new Tiger();
        }
    
        @Bean
        public MyBeanPostProcessor myBeanPostProcessor(){
            return new MyBeanPostProcessor();
        }
    }

      3、测试类

    public class SpringDemo {
    
        @Test
        public void springTest01() throws Exception {
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
            Person person = context.getBean("person", Person.class);
            Tiger tiger = context.getBean("tiger", Tiger.class);
            MyBeanPostProcessor myBeanPostProcessor = context.getBean("myBeanPostProcessor", MyBeanPostProcessor.class);
            context.close();
        }
    }

      4、测试结果

    创建Person对象....
    // 初始化之前调用postProcessBeforeInitialization()方法
    MyBeanPostProcessor..........postProcessBeforeInitialization
    Person的初始化....
    // 初始化完成之后调用postProcessAfterInitialization()方法
    MyBeanPostProcessor..........postProcessAfterInitialization
    MyBeanPostProcessor..........postProcessBeforeInitialization
    Tiger initial....
    MyBeanPostProcessor..........postProcessAfterInitialization
    Tiger destory....
    Person的销毁....
    

      

    四、JSR250规范

      @PostConstruct:在bean创建完成并且属性赋值完成时执行初始化方法

      @PreDestory:在容器销毁bean之前通知我们执行清理工作

  • 相关阅读:
    poj2409 Let it Bead
    bzoj1004 Cards
    Burnside引理和polay计数学习小记
    poj3294 --Life Forms
    poj3261 -- Milk Patterns
    Arch linux安装
    各种开源协议介绍
    grub2 rescue模式
    mongoDB-3.x Master Slave Replication
    Unix-like包管理器一览
  • 原文地址:https://www.cnblogs.com/xiaomaomao/p/13558735.html
Copyright © 2020-2023  润新知