• bean的生命周期


    目录

    一:bean的生命周期原理

    1.图解

    2.概括

    3. 代码:

    二:单例多例的理论

    1.运行效果

    2.图解 

    ​3. 单多的区别

    4.代码


    一:bean的生命周期原理

    1.图解

    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相应的属性中

    3. 代码:

    1. package zking.com.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. @Override
    32. public String toString() {
    33. return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
    34. }
    35. public Person() {
    36. this.init();
    37. this.name = "zs";
    38. this.age = 20;
    39. this.sex ="未知";
    40. }
    41. public void init() {
    42. }
    43. }

    二:单例多例的理论

    1.运行效果

    2.图解 

    3. 单多的区别

    单例模式下JAVAbean的生命周期

            容器生对象生,容器死对象死

    多例模式下JAVAbean的生命周期

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

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

    4.代码

    ParamAction

    1. package zking.com.beanlife;
    2. import java.util.List;
    3. /**
    4. * 印证单例模式的区别
    5. * @author zjjt
    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. }

    InstanceFactory: 

    1. package zking.com.beanlife;
    2. /**
    3. * 为了印证BeanPostProcessor初始化javabean
    4. * @author zjjt
    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. }

    demo2

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

  • 相关阅读:
    JDBC教程
    【Rust日报】2022-12-05 探索 docker 的 WASM 技术预览
    英伟达系列显卡大解析B100、H200、L40S、A100、A800、H100、H800、V100如何选择,含架构技术和性能对比带你解决疑惑
    IEDA refactor的用法
    Python---数据序列中的公共方法
    Golang-Gin Response 统一返回restful格式的数据
    论文阅读-基于低秩分解的网络异常检测综述
    [附源码]java毕业设计基于web的球类体育馆预定系统
    基于ssm操作系统课程网站
    Thread类中run和start的区别
  • 原文地址:https://blog.csdn.net/m0_67864917/article/details/126234266