• 【Java第35期】:Bean的生命周期


    作者:有只小猪飞走啦

    博客地址:https://blog.csdn.net/m0_62262008?type=blog

    内容:1,这篇博客要分析Bean生命周期有几个阶段?
    2,每个阶段的效果是什么?
    3,@PostConstruct 和 @PreDestroy 各自的效果是什么?

    在这里插入图片描述

    前言

    本博客内容是小博主在学习之后的总结,如果哪里有错误或者不太完善的都可以私聊我哈,如果哪里你不太理解的也可以来私聊我哦~

    一,Bean的生命周期的5个阶段

    所谓的⽣命周期指的是⼀个对象从诞⽣到销毁的整个⽣命过程,我们把这个过程就叫做⼀个对象的⽣命周期。所以我们来总结一下,Bean的5个生命周期是什么?
    1.实例化 Bean
    2.设置属性
    3.Bean 初始化
    3.1,执行各种通知
    3.2,初始化的前置方法【xml里面定义 init-method|@PostConstruct】
    3.3,初始化方法
    3.4,初始化的后置方法
    4.使⽤ Bean
    5.销毁 Bean
    以上就是Bean的5个生命周期,下面为你们一一解析每个阶段的效果:

    二,每个阶段的效果

    按上面的各个阶段用代码演示其生命周期:

    package com.tom.controller;
    
    import org.springframework.beans.factory.BeanNameAware;
    import org.springframework.stereotype.Component;
    
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    
    @Component
    public class BeanLifeComponent implements BeanNameAware {
        @Override
        public void setBeanName(String s) {
            System.out.println("执行了 Bean Name 通知:"+s);
        }
    
        /**
         * 方法命随意定义
         * xml中init-method指定方法
         */
        public void initMethod(){
            System.out.println("执行了 init method 方法");
        }
    
        @PostConstruct
        public void myPostConstruct(){
            System.out.println("执行了 PostConstruct 方法");
        }
    
        /**
         * 销毁前执行方法
         */
        @PreDestroy
        public void myPreDestroy(){
            System.out.println("执行了PreDestroy方法");
        }
    
        public void use(){
            System.out.println("执行了use 方法");
        }
    }
    
    
    • 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

    xml配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:content="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!--配置存储 Bean 对象的扫描根路径 -->
        <content:component-scan base-package="com.tom"> </content:component-scan>
    
    <!--    <bean id="userConfiguration" class="com.tom.configuration.UserConfiguration"> </bean>-->
    
        <beans>
    
        <bean id="beanLife" class="com.tom.controller.BeanLifeComponent"
              init-method="initMethod"> </bean>
        </beans>
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    调用类:

    import com.tom.controller.*;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class App {
        public static void main(String[] args) {
    
            ClassPathXmlApplicationContext context =
                    new ClassPathXmlApplicationContext("spring-config.xml");
            BeanLifeComponent component =
                    context.getBean("beanLife",BeanLifeComponent.class);
            component.use();
            //关闭容器
            context.destroy();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    结果:
    在这里插入图片描述

    三,@PostConstruct 和 @PreDestroy 各自的效果

    1,当Bean被容器初始化之后,会调用@PostConstruct的注解方法。
    2,当Bean在容器销毁之前,调用被@PreDestroy注解的方法
    如下代码:

     @PostConstruct
        public void myPostConstruct(){
            System.out.println("执行了 PostConstruct 方法");
        }
    
        /**
         * 销毁前执行方法
         */
        @PreDestroy
        public void myPreDestroy(){
            System.out.println("执行了PreDestroy方法");
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    所以,PostConstruct 注释用于在依赖关系注入完成之后需要执行的方法上,以执行任何初始化。此方法必须在将类放入服务之前调用。支持依赖关系注入的所有类都必须支持此注释。即使类没有请求注入任何资源,用PostConstruct 注释的方法也必须被调用。只有一个方法可以用此注释进行注释。

    PreDestroy 用与在依赖注入完成之前的方法前面执行,遵守准则:
    1,该方法不得有任何参数
    2,该方法的返回类型必须为 void;
    3,该方法不得抛出已检查异常;
    4,应用 PostConstruct 的方法可以是 public、protected、package private 或 private;
    5,该方法不能是 static;该方法可以是 final;
    6,该方法只会被执行一次

    以上就是Bean的生命周期的各个阶段,希望对你有帮助!
    关注小猪,小猪带你一起学习!
    请添加图片描述

  • 相关阅读:
    笔试训练2
    emment语法
    SpringAOP总结
    pandas
    科大讯飞2022秋招笔试知识点总结(超详细)
    P1219 [USACO1.5] 八皇后 Checker Challenge
    DDIM代码详细解读(4):分类器classifier的网络设计、训练、推理
    Spring Boot入门项目之外卖
    【ts】Partial<T>
    【教程】应用侧连接华为云IoT平台
  • 原文地址:https://blog.csdn.net/m0_62262008/article/details/128060259