• bean的生命周期


    目录

    一、bean的生命周期

    二、单例多例理论讲解

    三、单例多例的代码论证


    一、bean的生命周期

    对比之前

    Servlet的生命周期

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

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

            销毁:destroy——>Tomcat停止

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

    这些是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.ycx.beanLife;
    2. public class Demo1 {
    3. public static void main(String[] args) {
    4. Person p=new Person();
    5. System.out.println(p.getSex());
    6. }
    7. }
    8. class Person{
    9. private String name;
    10. private int age;
    11. private String sex;
    12. public String getName() {
    13. return name;
    14. }
    15. public void setName(String name) {
    16. this.name = name;
    17. }
    18. public int getAge() {
    19. return age;
    20. }
    21. public void setAge(int age) {
    22. this.age = age;
    23. }
    24. public String getSex() {
    25. return sex;
    26. }
    27. public void setSex(String sex) {
    28. this.sex = sex;
    29. }
    30. public Person() {
    31. this.init();
    32. this.name="杨总";
    33. this.age=18;
    34. this.sex="你猜";
    35. }
    36. public void init() {
    37. }
    38. @Override
    39. public String toString() {
    40. return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
    41. }
    42. }

     

    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标签转成beandifinition对象

    2、通过BeanFactoryPostProcessor可以在初始化之前修改属性值

    3、BeanFactory进行bean实例化,即生产Javabean

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

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

     

    二、单例多例理论讲解

    Spring的单例模式优点:

    1. package com.ycx.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="杨总";
    39. this.age=18;
    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. }

     一个类只创建一个Spring上下文

    1. package com.ycx.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"

          

     

    三、单例多例的代码论证

    InstanceFactory :

    为了印证BeanPostProcessor 初始化Javabean

    1. package com.ycx.beanLife;
    2. /**
    3. * 为了印证BeanPostProcessor 初始化Javabean
    4. * @author 杨总
    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.ycx.beanLife;
    2. import java.util.List;
    3. /**
    4. * 为了印证单例、多例模式的区别
    5. * @author 杨总
    6. *
    7. */
    8. public class ParamAction {
    9. private int age;
    10. private String name;
    11. private List<String> 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<String> 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. }

     

    Demo2 :

    (各种测试)

    1. package com.ycx.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.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>

     多例:

     

      单例(默认):

     

     单例模式下Javabean的生命周期:

            容器生则对象生,容器死则对象死。

     多例模式下Javabean的生命周期:

            使用时对象生,死亡跟着jvm垃圾回收机制走。

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

  • 相关阅读:
    温控采集器对接-java版-modbus4j
    fastdds库架构
    Python性能测试框架:Locust实战教程
    Java面试题200+大全(合适各级Java人员)
    【Vue】provider/inject 祖孙传值
    MapReduce切片与文件输入格式
    Spring Boot——日志文件
    【CodeForces】CF1700D River Locks
    音视频技术在手机上的应用与挑战
    PADS(二)更多使用和实战总结
  • 原文地址:https://blog.csdn.net/weixin_65808248/article/details/126232380