• spring03


    目录

    一、bean的生命周期原理

    1.1Spring Bean的生命周期

    1. 2 小结

    二、单例 多例理论讲解

    1、多例模式

     2、单例模式

     三、单例多例的代码论证

    一、bean的生命周期原理

    按顺时针看 

    1.1Spring 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.cdl.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. }

    此时结果:

     进行修改:

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

    例如:

    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. 2 小结


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

    二、单例 多例理论讲解

    1、多例模式

    在配置文件的bean中可以配置

    scope="prototype"
    

     Demo1

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

    结果:

     2、单例模式

    在配置文件的bean中可以配置

    scope="singLeton"
    
    1. package com.cdl.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. Person p1=Person.newInstance();
    13. Person p2=Person.newInstance();
    14. Person p3=Person.newInstance();
    15. Person p4=Person.newInstance();
    16. System.out.println(p1);
    17. System.out.println(p2);
    18. System.out.println(p3);
    19. System.out.println(p4);
    20. }
    21. }
    22. class Person{
    23. private Person() {
    24. }
    25. private final static Person p=new Person();
    26. public static Person newInstance() {
    27. return p;
    28. }
    29. }

    运行:

     

     因为多例模式存在弊端

     三、单例多例的代码论证

    InstanceFactory

    1. package com.cdl.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.cdl.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.cdl.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. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:aop="http://www.springframework.org/schema/aop"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    6. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    8. <bean class="com.cdl.biz.impl.UserBizImpl1" id="userBiz">bean>
    9. <bean class="com.cdl.biz.impl.UserBizImpl1" id="userBiz1">bean>
    10. <bean class="com.cdl.web.UserAction" id="userAction">
    11. <property name="userBiz" ref="userBiz1">property>
    12. <property name="age" value="22">property>
    13. <property name="name" value="cdl">property>
    14. <property name="hobby">
    15. <list>
    16. <value>篮球value>
    17. <value>足球value>
    18. <value>唱歌value>
    19. list>
    20. property>
    21. bean>
    22. <bean class="com.cdl.web.OrderAction" id="orderAction">
    23. <property name="userBiz" ref="userBiz1">property>
    24. <constructor-arg name="name" value="cdl">constructor-arg>
    25. <constructor-arg name="age" value="23">constructor-arg>
    26. <constructor-arg name="hobby">
    27. <list>
    28. <value>篮球1value>
    29. <value>足球1value>
    30. <value>唱歌1value>
    31. list>
    32. constructor-arg>
    33. bean>
    34. <bean class="com.cdl.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
    35. <bean class="com.cdl.aop.advice.MyMethodBeforeAdvice" id="myBefore">bean>
    36. <bean class="com.cdl.aop.advice.MyAfterReturningAdvice" id="myAfter">bean>
    37. <bean class="com.cdl.aop.advice.MyMethodInterceptor" id="myMethod">bean>
    38. <bean class="com.cdl.aop.advice.MyThrowsAdvice" id="myThrows">bean>
    39. <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="myafterPlus">
    40. <property name="advice" ref="myAfter">property>
    41. <property name="pattern" value=".*buy">property>
    42. bean>
    43. <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
    44. <property name="target" ref="bookBiz">property>
    45. <property name="proxyInterfaces">
    46. <list>
    47. <value>com.cdl.aop.biz.BookBizvalue>
    48. list>
    49. property>
    50. <property name="interceptorNames">
    51. <list>
    52. <value>myBeforevalue>
    53. <value>myMethodvalue>
    54. <value>myThrowsvalue>
    55. <value>myafterPlusvalue>
    56. list>
    57. property>
    58. bean>
    59. <bean id="paramAction" class="com.cdl.beanLife.ParamAction">
    60. <constructor-arg name="name" value="三丰">constructor-arg>
    61. <constructor-arg name="age" value="21">constructor-arg>
    62. <constructor-arg name="hobby">
    63. <list>
    64. <value>抽烟value>
    65. <value>烫头value>
    66. <value>大保健value>
    67. list>
    68. constructor-arg>
    69. bean>
    70. <bean id="instanceFactory" class="com.cdl.beanLife.InstanceFactory"
    71. scope="prototype" init-method="init" destroy-method="destroy">
    72. bean>
    73. beans>

    运行test1 默认的单例

     在配置文件 bean的开始标签中添加 变为多例模式

    scope="prototype"

    再运行test1

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

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

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

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

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

     

  • 相关阅读:
    Flink处理函数(一)
    JS-数组去重
    VSCode任务tasks.json中的问题匹配器problemMatcher的问题匹配模式ProblemPattern详解
    java面向对象的三大特征【继承、封装、多态】
    SAP通过应用实例分析BOM组件中“Asm“勾选问题
    如何在 uwsgi 配置中传递自定义参数?
    流畅的Python读书笔记-第九章-符合Python风格的对象
    集成测试规范
    非线性参数的精英学习灰狼优化算法-附代码
    数据持久化技术(Python)的使用
  • 原文地址:https://blog.csdn.net/weixin_62735525/article/details/126233091