• Bean的生命周期


    目录

    一,bean的初始化

    (1)Spring的IOC和AOP:

    (2)Spring Bean的生命周期:

    二,单例模式与多例模式的区别

    区别

     《代码演示》


    前言 

    回顾:Servlet的生命

    初始化:init-----》Tomcat启动,servlet对象就创建/初始化了

    服务:servlet----》浏览器发送请求,对应的servlet进行处理调用

    销毁:destroy----》Tomcat停止

    一,bean的初始化

    如图

    (1)Spring的IOC和AOP:

                    //初始化Spring上下文容器(IOC)

                                 ioc是一个容器,Spring是管理所有的Javabean对象;

                                    这些Javabean对象什么时候生,什么时间提供服务,什么时候销毁

    初始化Spring容器的时候,是通过

    ApplicationContext ac =  new ClassXmlPathApplicationContext("spring.xml");对象始化的对应的就是图中xml

    (2)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

    属性填充、修改等操作

    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对象实现了BeanPostProcessor后置处理器才会执行

    Before和After方法

    BeanPostProcessor

    1)Before

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

    3)After

    完成了Bean的创建工作

    8)destory:销毁

    简单总结

            1.通过三种方式(配置文件,注解,配置类)将bean便签转成BeanDefinition对象

    ·       2.通过BeanFactoryPostProcessor可以在初始化之前修啊属性值

            3.BeanFactory进行bean实例化,说白了就是生产Javabean

            4.Aware感知接口,能够在拿到Spring上下文中内部的资源对象

            5.BeanPostProcessor后置处理器,相当于环绕通知

    《代码测试》

    在正式初始化之前会调用init方法,而我调用的this.init();方法会有专门的人员来写,就是BeanFactoryPostProcessor

    《提取代码》

    1. public Person() {
    2. this.init();
    3. this.name="hhh";
    4. this.sex="nan";
    5. this.age=66;
    6. }
    7. private void init() {
    8. // TODO Auto-generated method stub
    9. }

    《整体代码》 

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

    二,单例模式与多例模式的区别

    区别

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

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

     ​​​​​​​       优点:内存使用少

            弊端:存在变量污染

            

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

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

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

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

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

            优点:变量相对独立

    ​​​​​​​        弊端:内存占比较多

     《代码演示》

    1. package com.zking.beanLife;
    2. import java.util.List;
    3. /**
    4. * 验证单例多例模式的区别
    5. * @author dxy
    6. *
    7. */
    8. public class ParamAction {
    9. private int age;
    10. private String name;
    11. private List<String> hobby;
    12. //当初始化值是1
    13. private int num = 1;
    14. // private UserBiz userBiz = new UserBizImpl1();
    15. public ParamAction() {
    16. super();
    17. }
    18. public ParamAction(int age, String name, List<String> hobby) {
    19. super();
    20. this.age = age;
    21. this.name = name;
    22. this.hobby = hobby;
    23. }
    24. public void execute() {
    25. // userBiz.upload();
    26. // userBiz = new UserBizImpl2();
    27. System.out.println("this.num=" + this.num++);
    28. System.out.println(this.name);
    29. System.out.println(this.age);
    30. System.out.println(this.hobby);
    31. }
    32. }

     num的值不一样,是由一个类创建了两个对象,但是两个对象是同一个对象,这就是单例

    1. package com.zking.beanLife;
    2. /**
    3. * 为了验证BeanPostrocessor 初始化Javabean
    4. * @author dxy
    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. }
    1. package com.zking.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. InstanceFactory p1 = (InstanceFactory) applicationContext.getBean("instanceFactory");
    23. InstanceFactory p2 = (InstanceFactory) applicationContext.getBean("instanceFactory");
    24. // p1.execute();
    25. // p2.execute();
    26. // 单例时,容器销毁instanceFactory对象也销毁;多例时,容器销毁对象不一定销毁;
    27. applicationContext.close();
    28. }
    29. // 体现单例与多例的初始化的时间点 instanceFactory
    30. @Test
    31. public void test2() {
    32. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
    33. }
    34. // BeanFactory会初始化bean对象,但会根据不同的实现子类采取不同的初始化方式
    35. // 默认情况下bean的初始化,单例模式立马会执行,但是此时XmlBeanFactory作为子类,单例模式下容器创建,bean依赖没有初始化,只有要获取使用bean对象才进行初始化
    36. @Test
    37. public void test3() {
    38. // ClassPathXmlApplicationContext applicationContext = new
    39. // ClassPathXmlApplicationContext("/spring-context.xml");
    40. Resource resource = new ClassPathResource("/spring-context.xml");
    41. BeanFactory beanFactory = new XmlBeanFactory(resource);
    42. // InstanceFactory i1 = (InstanceFactory) beanFactory.getBean("instanceFactory");
    43. }
    44. }

    配置文件

    1. "1.0" encoding="UTF-8"?>
    2. <beans default-autowire="byName" xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:aop="http://www.springframework.org/schema/aop"
    5. xmlns:context="http://www.springframework.org/schema/context"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    9. <bean id="paramAction" class="com.zking.beanLife.ParamAction" scope="poprototype">
    10. <constructor-arg name="name" value="三丰">constructor-arg>
    11. <constructor-arg name="age" value="21">constructor-arg>
    12. <constructor-arg name="hobby">
    13. <list>
    14. <value>抽烟value>
    15. <value>烫头value>
    16. <value>大保健value>
    17. list>
    18. constructor-arg>
    19. bean>
    20. <bean id="instanceFactory" class="com.zking.beanLife.InstanceFactory"
    21. scope="prototype" init-method="init" destroy-method="destroy">bean>
    22. beans>

    效果图

     当在配置文件中加上scope=“”里面的值换成prototype多例模式,可以看到效果图,num值就变得不一样了

  • 相关阅读:
    FPGA project : beep
    【自学笔记】网络安全——黑客技术
    青岛大学数据结构与算法——第5章
    (提供数据集下载)基于大语言模型LangChain与ChatGLM3-6B本地知识库调优:数据集优化、参数调整、Prompt提示词优化实战
    MySQL如何导入大量数据?
    手把手教你君正X2000开发板的OpenHarmony环境搭建
    F. Rats Rats(二分 or 预处理)[UTPC Contest 09-02-22 Div. 2 (Beginner)]
    CentOS 7安装MySQL及初始化操作教程
    Java标识符指什么呢?
    2022杭电多校9 Matryoshka Doll
  • 原文地址:https://blog.csdn.net/weixin_66202611/article/details/126231311