• Spring Bean 生命周期


    Spring Bean 生命周期

    一、spring的bean创建和销毁

    1、spring只帮我们管理单例bean。

    2、spring容器默认是使用单例模式,容器启动时创建所有单例bean,容器关闭时销毁bean

    3、对于 prototype 的 bean 多例模式,何时调用getBean对象时创建bean对象,之后则不会再管理后续的生命周期(不会调用destory方法)。

    二、注意点

    1、BeanPostProcessor 后置处理器不是单独针对某一个bean生效,而是针对IOC容器中所有bean都会执行

    三、生命周期图

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GYOK0gUv-1660437296602)(D:\my_software\Typora\localpicture\image-20220813115036097.png)]

    Person 类型

    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.BeanFactoryAware;
    import org.springframework.beans.factory.BeanNameAware;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    
    public class Person implements BeanNameAware,BeanFactoryAware,
            InitializingBean, DisposableBean, ApplicationContextAware {
    
        private String name;
        private String address;
        private String phone;
    
        private BeanFactory beanFactory;
        private String beanName;
    
        public Person() {
            System.out.println("【构造器】调用Person的构造器实例化=======>>>");
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            System.out.println("【注入属性】注入属性name");
            this.name = name;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            System.out.println("【注入属性】注入属性address");
            this.address = address;
        }
    
        public String getPhone() {
            return phone;
        }
    
        public void setPhone(String phone) {
            System.out.println("【注入属性】注入属性phone");
            this.phone = phone;
        }
    
        @Override
        public String toString() {
            return "Person [address=" + address + ", name=" + name + ", phone="
                    + phone + "]";
        }
    
        // 这是BeanNameAware接口方法
        @Override
        public void setBeanName(String arg0) {
            System.out.println("【BeanNameAware接口】调用BeanNameAware.setBeanName()");
            this.beanName = arg0;
        }
    
        // 这是BeanFactoryAware接口方法
        @Override
        public void setBeanFactory(BeanFactory arg0) throws BeansException {
            System.out
                    .println("【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()");
            this.beanFactory = arg0;
        }
    
        // 这是InitializingBean接口方法
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out
                    .println("【InitializingBean接口】调用InitializingBean.afterPropertiesSet()");
        }
    
        // 这是 DisposableBean 接口方法
        @Override
        public void destroy() throws Exception {
            System.out.println("【DisposableBean接口】DisposableBean.destroy()");
        }
    
        // 通过的init-method属性指定的初始化方法
        public void myInit() {
            System.out.println("【init-method】调用的init-method属性指定的初始化方法");
        }
    
        // 通过的destroy-method属性指定的初始化方法
        public void myDestroy() {
            System.out.println("【destroy-method】调用的destroy-method属性指定的初始化方法");
        }
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            System.out.println("【ApplicationContext 接口】调用ApplicationContext.setApplicationContext()");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99

    MyBeanPostProcessor 后置处理器

    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    
    public class MyBeanPostProcessor implements BeanPostProcessor {
    
        public MyBeanPostProcessor() {
            super();
            System.out.println("这是 BeanPostProcessor 实现类构造器!!=======>>>");
            // TODO Auto-generated constructor stub
        }
    
        @Override
        public Object postProcessAfterInitialization(Object arg0, String arg1)
                throws BeansException {
            System.out
                    .println("【BeanPostProcessor 接口】 调用BeanPostProcessor.postProcessAfterInitialization()方法 对属性进行更改!");
            return arg0;
        }
    
        @Override
        public Object postProcessBeforeInitialization(Object arg0, String arg1)
                throws BeansException {
            System.out
                    .println("【BeanPostProcessor 接口】 调用BeanPostProcessor.postProcessBeforeInitialization()方法 对属性进行更改!");
            return arg0;
        }
    }	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    partBeanLife/beans.xml

    
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
           xsi:schemaLocation="
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
    
        <bean id="beanPostProcessor" class="partBeanLife.MyBeanPostProcessor">
        bean>
    
        <bean id="person" class="partBeanLife.Person" init-method="myInit" destroy-method="myDestroy"
              p:name="张三" p:address="广州" p:phone="18979696249" />
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    测试类:

    public class BeanLifeCycle {
        public static void main(String[] args) {
    
            System.out.println("现在开始初始化容器");
    
            ApplicationContext factory = new ClassPathXmlApplicationContext("partBeanLife/beans.xml");
            System.out.println("容器初始化成功");
            //得到Person,并使用
            Person person = factory.getBean("person",Person.class);
            System.out.println(person);
    
            System.out.println("现在开始关闭容器!");
            ((ClassPathXmlApplicationContext)factory).registerShutdownHook();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    输出结果:

    现在开始初始化容器
    这是 BeanPostProcessor 实现类构造器!!=======>>>
    【构造器】调用Person的构造器实例化=======>>>
    【注入属性】注入属性address
    【注入属性】注入属性name
    【注入属性】注入属性phone
    【BeanNameAware接口】调用BeanNameAware.setBeanName()
    【BeanFactoryAware接口】调用BeanFactoryAware.setBeanFactory()
    【ApplicationContext 接口】调用ApplicationContext.setApplicationContext()
    【BeanPostProcessor 接口】 调用BeanPostProcessor.postProcessBeforeInitialization()方法 对属性进行更改!
    【InitializingBean接口】调用InitializingBean.afterPropertiesSet()
    【init-method】调用的init-method属性指定的初始化方法
    【BeanPostProcessor 接口】 调用BeanPostProcessor.postProcessAfterInitialization()方法 对属性进行更改!
    容器初始化成功
    Person [address=广州, name=张三, phone=18979696249]
    现在开始关闭容器!
    【DisposableBean接口】DisposableBean.destroy()
    【destroy-method】调用的destroy-method属性指定的初始化方法
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    [Spring Bean的生命周期(非常详细)]:https://www.cnblogs.com/zrtqsk/p/3735273.html

  • 相关阅读:
    创建数据库报错--MySQL server is running with the --super-read-only option
    LeetCode题解——36.二叉搜索树与双向链表(中序遍历)
    SSH Remote Linux Servers By Public Key Authentication
    新一代分布式实时流处理引擎Flink入门实战操作篇
    iNFTnews | NFT或将彻底改变音乐产业
    树莓派4b安装Pytorch1.11
    Java-面向对象的特征之一:封装
    2022Nginx入门教程,图文超详细
    Django 入门学习总结8-管理页面的生成
    win10 查看已连接过的wifi的密码
  • 原文地址:https://blog.csdn.net/jue6628/article/details/126328110