目录

1、通过三种方式(配置文件、注解、配类) 将bean标签转为BeanDefinitionReader对象
2、通过BeanFactoryPostProcessor可以在初始化之前修改属性值
3、BeanFactory进行bean实例化,说白了就是生产javabean
4、Aware感知接口,能够在拿到Spring上下文中内部的资源对象
5、BeanPostProcessor后置处理器,相当于环绕通知
假设一个类被使用了100次,而这个项目共有1w个类,那么Spring上下文要创建100w个对象,而单例模式可以让Spring上下文创建的对象只有1w个,因此单例模式可以节省空间
会污染变量
那为什么Spring框架的作者要提供原型/多例模式Javabean管理模式进行选择?
spring-context.xml
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
-
-
- <bean class="com.zsx.biz.impl.UserBizImpl" id="userbiz" scope="singleton">bean>
-
- <bean class="com.zsx.web.UserAction" id="userAction">
- <property name="userbiz" ref="userbiz">property>
- <property name="name" value="zsx">property>
- <property name="age" value="18">property>
- <property name="hobby">
- <list>
- <value>唱value>
- <value>跳value>
- <value>原神value>
- list>
- property>
- bean>
- <bean class="com.zsx.web.OrderAction" id="orderAction">
- <property name="userbiz" ref="userbiz">property>
- <constructor-arg name="name" value="zsx">constructor-arg>
- <constructor-arg name="age" value="18">constructor-arg>
- <constructor-arg name="hobby">
- <list>
- <value>唱value>
- <value>跳value>
- <value>原神value>
- list>
- constructor-arg>
- bean>
-
-
-
-
- <bean class="com.zsx.aop.biz.impl.BookBizImpl" id="bookBiz">bean>
-
- <bean class="com.zsx.aop.advice.MyAdvice" id="myBefore">bean>
-
- <bean class="com.zsx.aop.advice.MyAfterAdvice" id="myAfter">bean>
-
- <bean class="com.zsx.aop.advice.MyInterAdvice" id="myInter">bean>
-
- <bean class="com.zsx.aop.advice.MyThrowsAdvice" id="myThorw">bean>
-
- <bean class="org.springframework.aop.support.RegexpMethodPointcutAdvisor" id="myAfter2">
- <property name="advice" ref="myAfter">property>
- <property name="pattern" value=".*buy">property>
- bean>
-
-
- <bean class="org.springframework.aop.framework.ProxyFactoryBean" id="bookProxy">
-
- <property name="target" ref="bookBiz">property>
-
- <property name="proxyInterfaces">
- <list>
- <value>com.zsx.aop.biz.BookBizvalue>
- list>
- property>
-
- <property name="interceptorNames">
- <list>
- <value>myBeforevalue>
-
- <value>myIntervalue>
- <value>myThorwvalue>
- <value>myAfter2value>
- list>
- property>
- bean>
-
-
- <bean id="paramAction" class="com.zsx.beanLife.ParamAction">
- <constructor-arg name="name" value="三丰">constructor-arg>
- <constructor-arg name="age" value="21">constructor-arg>
- <constructor-arg name="hobby">
- <list>
- <value>抽烟value>
- <value>烫头value>
- <value>大保健value>
- list>
- constructor-arg>
- bean>
-
- <bean id="instanceFactory" class="com.zsx.beanLife.InstanceFactory"
- scope="singleton" init-method="init" destroy-method="destroy">bean>
-
-
-
-
- beans>
InstanceFactory
- package com.zsx.beanLife;
-
- /**
- * 为了印证BeanPostProcessor 初始化javabean
- * @author Administrator
- *
- */
- public class InstanceFactory {
- public void init() {
- System.out.println("初始化方法");
- }
-
- public void destroy() {
- System.out.println("销毁方法");
- }
-
- public void service() {
- System.out.println("业务方法");
- }
- }
ParamAction
- package com.zsx.beanLife;
-
- import java.util.List;
-
-
- /**
- * 印证单例、多例的区别
- * @author Administrator
- *
- */
- public class ParamAction {
- private int age;
- private String name;
- private List
hobby; - private int num = 1;
- // private UserBiz userBiz = new UserBizImpl1();
-
- public ParamAction() {
- super();
- }
-
- public ParamAction(int age, String name, List
hobby) { - super();
- this.age = age;
- this.name = name;
- this.hobby = hobby;
- }
-
- public void execute() {
- // userBiz.upload();
- // userBiz = new UserBizImpl2();
- System.out.println("this.num=" + this.num++);
- System.out.println(this.name);
- System.out.println(this.age);
- System.out.println(this.hobby);
- }
- }
Demo2
- package com.zsx.beanLife;
-
- import org.junit.Test;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.beans.factory.xml.XmlBeanFactory;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.core.io.ClassPathResource;
- import org.springframework.core.io.Resource;
-
- /*
- * spring bean的生命週期
- * spring bean的單例多例
- */
- public class Demo2 {
- // 体现单例与多例的区别
- @Test
- public void test1() {
- ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
- // ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
- //ParamAction p1 = (ParamAction) applicationContext.getBean("paramAction");
- //ParamAction p2 = (ParamAction) applicationContext.getBean("paramAction");
-
- InstanceFactory p1 = (InstanceFactory) applicationContext.getBean("instanceFactory");
- InstanceFactory p2 = (InstanceFactory) applicationContext.getBean("instanceFactory");
- // System.out.println(p1==p2);
- // p1.execute();
- // p2.execute();
-
- // 单例时,容器销毁instanceFactory对象也销毁;多例时,容器销毁对象不一定销毁;
- applicationContext.close();
- }
-
- // 体现单例与多例的初始化的时间点 instanceFactory
- @Test
- public void test2() {
- ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
- }
-
- // BeanFactory会初始化bean对象,但会根据不同的实现子类采取不同的初始化方式
- // 默认情况下bean的初始化,单例模式立马会执行,但是此时XmlBeanFactory作为子类,单例模式下容器创建,bean依赖没有初始化,只有要获取使用bean对象才进行初始化
- @Test
- public void test3() {
- // ClassPathXmlApplicationContext applicationContext = new
- // ClassPathXmlApplicationContext("/spring-context.xml");
-
- Resource resource = new ClassPathResource("/spring-context.xml");
- BeanFactory beanFactory = new XmlBeanFactory(resource);
- //InstanceFactory i1 = (InstanceFactory) beanFactory.getBean("instanceFactory");
-
- }
-
- }
Debug运行Demo2中的text1(这是单例模式下的)

Debug运行Demo2中的text3 这是多例模式下的) 没启用最后一行代码,初始化都不会出来

放出Demo2中的text3中的最后一行代码 然后再运行
- @Test
- public void test3() {
- // ClassPathXmlApplicationContext applicationContext = new
- // ClassPathXmlApplicationContext("/spring-context.xml");
-
- Resource resource = new ClassPathResource("/spring-context.xml");
- BeanFactory beanFactory = new XmlBeanFactory(resource);
- InstanceFactory i1 = (InstanceFactory) beanFactory.getBean("instanceFactory");
-
- }
放出后会初始化方法,但最终不会摧毁方法

单例模式下javabean的生命周期
容器生对象生, 容器死对象死
多例模式下javabean的生命周期
使用时对象生,死亡跟着jvm垃圾回收机制走
bean的初始化时间点,除了跟bean管理模式(单例/多例)有关,还跟BeanFactory的子类有关