• Spring--bean的生命周期


    目录

    一、bean的生命周期

    二、bean的单例与多例模式

    Spring的单例模式优点⬇⬇⬇

     单例模式弊端⬇⬇⬇

    ​​​​​​​​单例多例的代码论证

    InstanceFactory 

    ParamAction 

    Demo2测试


    一、bean的生命周期

    对比Servlet的生命周期⬇⬇⬇

    Servlet的生命周期

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

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

            销毁:destroy——>Tomcat停止

    Spring是管理项目中所有的Javabean对象

     Spring Bean的生命周期⬇⬇⬇

    1)通过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相应的属性中

    例如:

    -----> BeanDefinition(id/class/scope)

    4)BeanFactoryPostProcessor:是Spring容器功能的扩展接口。

    注意:

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

    在bean实例化之前执行的

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

    属性填充、修改等操作

    1. package com.xiaokun.beanLife;
    2. public class Demo1 {
    3. public static void main(String[] args) {
    4. Person p=new Person();
    5. p.setSex("男");
    6. System.out.println(p.getSex());
    7. }
    8. }
    9. class Person{
    10. private String name;
    11. private int age;
    12. private String sex;
    13. public String getName() {
    14. return name;
    15. }
    16. public void setName(String name) {
    17. this.name = name;
    18. }
    19. public int getAge() {
    20. return age;
    21. }
    22. public void setAge(int age) {
    23. this.age = age;
    24. }
    25. public String getSex() {
    26. return sex;
    27. }
    28. public void setSex(String sex) {
    29. this.sex = sex;
    30. }
    31. public Person() {
    32. this.init();
    33. this.name="zs";
    34. this.age=20;
    35. this.sex="未知";
    36. }
    37. public void init() {
    38. }
    39. @Override
    40. public String toString() {
    41. return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
    42. }
    43. }

    5)BeanFactory:bean工厂;它按照我们的要求生产我们需要的各种各样的bean

    1. 例如:
    2. BeanFactory -> List
    3. BeanDefinition(id/class/scope/init-method)
    4. "com.zking.spring02.biz.BookBizImpl"/>
    5. foreach(BeanDefinition bean : List){
    6.    //根据class属性反射机制实例化对象
    7.    //反射赋值设置属性
    8. }

    6)Aware感知接口:在实际开发中,经常需要用到Spring容器本身的功能资源

    例如:BeanNameAware、ApplicationContextAware等等

    BeanDefinition 实现了 BeanNameAware、ApplicationContextAware

    7)BeanPostProcessor:后置处理器;在Bean对象实例化和引入注入完毕后,

    在显示调用初始化方法的前后添加自定义的逻辑(类似于AOP的绕环通知)

     

    ​​​​​​​

    前提条件:如果检测到Bean对象实现了BeanPostProcessor后置处理器才会执行

    Before和After方法

    BeanPostProcessor

    1、Before

    2、调用初始化Bean(InitializingBean和init-method,Bean的初始化才算完成)

    3、After,完成了Bean的创建工作

    8)destory:销毁

    小结:通过三种方式(配置文件、注解、配置类)将bean标签转成beandifinition对象
               通过BeanFactoryPostPricessor可以在初始化之前修改属性
               ③BeanFactory进行bean实例化,就是生产javabean
               ④Aware感知接口,能够在拿到Spring上下文中内部的资源对象
               ⑤BeanPostProcessor后置处理器,相当于环绕通知

    二、bean的单例与多例模式

    Spring的单例模式优点⬇⬇⬇

    scope="singLeton"
    
    1. package com.xiaokun.beanLife;
    2. public class Demo1 {
    3. public static void main(String[] args) {
    4. // Person p1=new Person();
    5. // Person p2=new Person();
    6. // Person p3=new Person();
    7. // Person p4=new Person();
    8. Person p1=Person.newInstance();
    9. Person p2=Person.newInstance();
    10. Person p3=Person.newInstance();
    11. Person p4=Person.newInstance();
    12. System.out.println(p1);
    13. System.out.println(p2);
    14. System.out.println(p3);
    15. System.out.println(p4);
    16. }
    17. }
    18. class Person{
    19. private Person() {
    20. }
    21. private final static Person p=new Person();
    22. public static Person newInstance() {
    23. return p;
    24. }
    25. }

    控制台运行效果:

     单例模式弊端⬇⬇⬇:

    解决措施:假如某实现类有个变量,可在配置内设置多例 scope="prototype"

     多例模式👀👀👀👀

    scope="prototype"
    1. package com.xiaokun.beanLife;
    2. public class Demo1 {
    3. public static void main(String[] args) {
    4. Person p1=new Person();
    5. Person p2=new Person();
    6. Person p3=new Person();
    7. Person p4=new Person();
    8. System.out.println(p1);
    9. System.out.println(p2);
    10. System.out.println(p3);
    11. System.out.println(p4);
    12. }
    13. }
    14. class Person{
    15. private String name;
    16. private int age;
    17. private String sex;
    18. public String getName() {
    19. return name;
    20. }
    21. public void setName(String name) {
    22. this.name = name;
    23. }
    24. public int getAge() {
    25. return age;
    26. }
    27. public void setAge(int age) {
    28. this.age = age;
    29. }
    30. public String getSex() {
    31. return sex;
    32. }
    33. public void setSex(String sex) {
    34. this.sex = sex;
    35. }
    36. public Person() {
    37. this.init();
    38. this.name="zs";
    39. this.age=20;
    40. this.sex="未知";
    41. }
    42. public void init() {
    43. }
    44. // @Override
    45. // public String toString() {
    46. // return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
    47. // }
    48. //
    49. }

    控制台运行效果: 

    ​​​​​​​
    三、单例多例的代码论证

    为了印证BeanPostProcessor 初始化Javabean

    InstanceFactory :

    1. package com.xiaokun.beanLife;
    2. public class InstanceFactory {
    3. public void init() {
    4. System.out.println("初始化方法");
    5. }
    6. public void destroy() {
    7. System.out.println("销毁方法");
    8. }
    9. public void service() {
    10. System.out.println("业务方法");
    11. }
    12. }

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

    ParamAction :

    1. package com.xiaokun.beanLife;
    2. import java.util.List;
    3. public class ParamAction {
    4. private int age;
    5. private String name;
    6. private List hobby;
    7. private int num = 1;
    8. // private UserBiz userBiz = new UserBizImpl1();
    9. public ParamAction() {
    10. super();
    11. }
    12. public ParamAction(int age, String name, List hobby) {
    13. super();
    14. this.age = age;
    15. this.name = name;
    16. this.hobby = hobby;
    17. }
    18. public void execute() {
    19. // userBiz.upload();
    20. // userBiz = new UserBizImpl2();
    21. System.out.println("this.num=" + this.num++);
    22. System.out.println(this.name);
    23. System.out.println(this.age);
    24. System.out.println(this.hobby);
    25. }
    26. }

    Demo2测试:

    1. package com.xiaokun.beanLife;
    2. import org.junit.Test;
    3. import org.springframework.beans.factory.BeanFactory;
    4. import org.springframework.beans.factory.xml.XmlBeanFactory;
    5. import org.springframework.context.ApplicationContext;
    6. import org.springframework.context.support.ClassPathXmlApplicationContext;
    7. import org.springframework.core.io.ClassPathResource;
    8. import org.springframework.core.io.Resource;
    9. /*
    10. * spring bean的生命週期
    11. * spring bean的單例多例
    12. */
    13. public class Demo2 {
    14. // 体现单例与多例的区别
    15. @Test
    16. public void test1() {
    17. ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
    18. // ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
    19. ParamAction p1 = (ParamAction) applicationContext.getBean("paramAction");
    20. ParamAction p2 = (ParamAction) applicationContext.getBean("paramAction");
    21. // System.out.println(p1==p2);
    22. p1.execute();
    23. p2.execute();
    24. // 单例时,容器销毁instanceFactory对象也销毁;多例时,容器销毁对象不一定销毁;
    25. applicationContext.close();
    26. }
    27. // 体现单例与多例的初始化的时间点 instanceFactory
    28. @Test
    29. public void test2() {
    30. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
    31. }
    32. // BeanFactory会初始化bean对象,但会根据不同的实现子类采取不同的初始化方式
    33. // 默认情况下bean的初始化,单例模式立马会执行,但是此时XmlBeanFactory作为子类,单例模式下容器创建,bean依赖没有初始化,只有要获取使用bean对象才进行初始化
    34. @Test
    35. public void test3() {
    36. // ClassPathXmlApplicationContext applicationContext = new
    37. // ClassPathXmlApplicationContext("/spring-context.xml");
    38. Resource resource = new ClassPathResource("/spring-context.xml");
    39. BeanFactory beanFactory = new XmlBeanFactory(resource);
    40. // InstanceFactory i1 = (InstanceFactory) beanFactory.getBean("instanceFactory");
    41. }
    42. }

     spring-context.xml:

    1. <bean id="paramAction" class="com.xiaokun.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.xiaokun.beanLife.InstanceFactory"
    13. scope="prototype" init-method="init" destroy-method="destroy">bean>

     Demo2-test1运行效果👀

    ​​​​​​​​​​​​​​​​​​​​​ 

     

    test3运行效果

     小结

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

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

    bean的初始化时间点,除了与bean管理模式有关,还跟Beanfactory的子类有关
     

  • 相关阅读:
    04 src和href的区别?link标签和script标签的区别?如何提高权重?DOM和BOM区别
    网络安全工作的随想
    centos7篇---centos7中安装mongodb
    【论文解读】CP-SLAM: Collaborative Neural Point-based SLAM System_神经点云协同SLAM系统(上)
    Linux软件管理RPM的使用
    Java.lang.Class类 toString()方法有什么功能呢?
    Mac图像编辑器Pixelmator Pro
    2309C++测试miniz简单压缩
    MySQL 8.0.34安装教程
    Qt第二十一章:Qt Designer 之 布局
  • 原文地址:https://blog.csdn.net/weixin_67450855/article/details/126238950