• SpringBean


    目录

    回顾:Servlet的生命周期

    一、SpringBean的生命周期原理

    二、Bean的单例和多例模式的区别


    回顾:Servlet的生命周期

    Servlet的生命周期:

            初始化:init ——>Tomcat启动,Servlet对象就创建/初始化了

            服务:service——>浏览器发送请求,对应的Servlet进行处理调用

            销毁:destroy——>Tomcat停止
     

    一、SpringBean的生命周期原理

    初始化过程图

    1、加载Spring Bean的三种方式

    通过XML、Java annotation(注解)以及Java Configuration(配置类)

    等方式加载Spring Bean

    2、BeanDefinitionReader

    解析Bean的定义。在Spring容器启动过程中,

    会将Bean解析成Spring内部的BeanDefinition结构

    理解为:将spring.xml中的标签转换成BeanDefinition结构

    有点类似于XML解析

    3、BeanDefinition

    包含了很多属性和方法。例如:id、class(类名)、

    scope、ref(依赖的bean)等等。其实就是将bean(例如)的定义信息

    存储到这个对应BeanDefinition相应的属性中

    4、BeanFactoryPostProcessor

    是Spring容器功能的扩展接口。

    (1)

    BeanFactoryPostProcessor在spring容器加载完BeanDefinition之后,

    在bean实例化之前执行的

    (2)

    对bean元数据(BeanDefinition)进行加工处理,也就是BeanDefinition

    属性填充、修改等操作

    5、BeanFactory

    bean工厂。它按照我们的要求生产我们需要的各种各样的bean。

    BeanFactory -> List

    BeanDefinition(id/class/scope/init-method)

    foreach(BeanDefinition bean : List){

       //根据class属性反射机制实例化对象

       //反射赋值设置属性

     6、Aware感知接口

    在实际开发中,经常需要用到Spring容器本身的功能资源

    例如:BeanNameAware、ApplicationContextAware等等

    BeanDefinition 实现了 BeanNameAware、ApplicationContextAware

    7、BeanPostProcessor 

    后置处理器。在Bean对象实例化和引入注入完毕后,在显示调用初始化方法的前后添加自定义的逻辑。(类似于AOP的绕环通知)

    二、Bean的单例和多例模式的区别

    1、区别

    单例时,容器销毁instanceFactory对象也销毁;

            单例模式下Javabean的生命周期:容器生对象生,容器死对象死

            

    补充:当配置文件中没有scope=“”时默认就是单例,当scope=“prototype”中填写的是

    prototype就是原型的模式(多例模式)但是Spring容器中默认的是单例singleton

    多例时,容器销毁对象不一定销毁;

            多例模式下Javabean的生命周期:使用时对象生,死亡跟着JVM垃圾回收站机制走

            bean的初始化时间点,除了与bean管理模式(单例/多例)有关,还跟beanfactor的子类有关
     

    2、代码论证

    InstanceFactory :

    为了印证BeanPostProcessor 初始化Javabean

    1. package com.zjy.beanLife;
    2. /**
    3. * 为了印证BeanPostProcessor 初始化Javabean
    4. * @ 朱佳音
    5. *
    6. */
    7. public class InstanceFactory {
    8. public void init() {
    9. System.out.println("初始化方法");
    10. }
    11. public void destroy() {
    12. System.out.println("销毁方法");
    13. }
    14. public void service() {
    15. System.out.println("业务方法");
    16. }
    17. }

    ParamAction :

    为了印证单例、多例模式的区别

    1. package com.zjy.beanLife;
    2. import java.util.List;
    3. /**
    4. * 为了印证单例、多例模式的区别
    5. * 小朱
    6. *
    7. */
    8. public class ParamAction {
    9. private int age;
    10. private String name;
    11. private List hobby;
    12. private int num = 1;
    13. // private UserBiz userBiz = new UserBizImpl1();
    14. public ParamAction() {
    15. super();
    16. }
    17. public ParamAction(int age, String name, List hobby) {
    18. super();
    19. this.age = age;
    20. this.name = name;
    21. this.hobby = hobby;
    22. }
    23. public void execute() {
    24. // userBiz.upload();
    25. // userBiz = new UserBizImpl2();
    26. System.out.println("this.num=" + this.num++);
    27. System.out.println(this.name);
    28. System.out.println(this.age);
    29. System.out.println(this.hobby);
    30. }
    31. }

     spring-context.xml:

    1. <bean id="paramAction" class="com.zking.beanLife.ParamAction">
    2. <constructor-arg name="name" value="三丰">constructor-arg>
    3. <constructor-arg name="age" value="21">constructor-arg>
    4. <constructor-arg name="hobby">
    5. <list>
    6. <value>抽烟value>
    7. <value>烫头value>
    8. <value>大保健value>
    9. list>
    10. constructor-arg>
    11. bean>
    12. <bean id="instanceFactory" class="com.zking.beanLife.InstanceFactory"
    13. scope="prototype" init-method="init" destroy-method="destroy">bean>

  • 相关阅读:
    Word不计算封面、目录页数将正文页码修改为第几页共几页的格式
    js调用android 方法
    YOLOv7独家改进:Multi-Dconv Head Transposed Attention注意力,效果优于MHSA| CVPR2022
    Java“牵手”淘宝店铺商品列表页数据采集+淘宝店铺商品上传接口,淘宝API接口申请指南
    CSS实现背景图片模糊——毛玻璃效果 | 浅谈CSS属性 filter、backdrop-filter
    Spring入门第一讲——Spring框架的快速入门
    CSS3属性详解(一)文本 盒模型中的 box-ssize 属性 处理兼容性问题:私有前缀 边框 背景属性 渐变 前端开发入门笔记(七)
    什么是Token?一文带你深入理解Token
    前端研习录(27)——JavaScript document方法讲解及示例分析
    扩散模型Diffusion轻松入门
  • 原文地址:https://blog.csdn.net/m0_67477525/article/details/126234540