目录
1)通过XML、Java annotation(注解)以及Java Configuration(配置类)
等方式加载Spring Bean
2)BeanDefinitionReader:解析Bean的定义。在Spring容器启动过程中,
会将Bean解析成Spring内部的BeanDefinition结构;
理解为:将spring.xml中的
有点类似于XML解析
3)BeanDefinition:包含了很多属性和方法。例如:id、class(类名)、
scope、ref(依赖的bean)等等。其实就是将bean(例如
存储到这个对应BeanDefinition相应的属性中
例如:
4)BeanFactoryPostProcessor:是Spring容器功能的扩展接口。
注意:
①BeanFactoryPostProcessor在spring容器加载完BeanDefinition之后,
在bean实例化之前执行的
②对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
①Before
②调用初始化Bean(InitializingBean和init-method,Bean的初始化才算完成)
③After
完成了Bean的创建工作
8)destory:销毁

1.通过三种方式(配置文件、注解、配置类)将bean标签转成beandifinition对象
2.通过BeanFactoryPostProcessor可以在初始化之前修改属性值
3.BeanFactory进行bean实例化,其实就是生产javabean
4.Aware感知接口,能够在拿到Spring上下文中内部的资源对象
5.BeanPostProcessor后置处理器,相对于环绕通知
Spring的javaBean是分为两种的,一种是单例,一种是多例;
我们在Spring配置文件中也是可以配置的:
scope="singleton" ---> 代表单例
scope="prototype" ---> 代表多例
不配置的话默认单例
-
- <bean class="com.zking.biz.impl.UserBizImlp1" id="userBiz">bean>
-
- <bean class="com.zking.biz.impl.UserBizImlp1" id="userBiz" scope="singleton">bean>
-
- <bean class="com.zking.biz.impl.UserBizImlp1" id="userBiz" scope="prototype">bean>
普通代码
- package com.zking.beanLife;
-
- public class Demo1 {
- public static void main(String[] args) {
- Person p1 = new Person();
- Person p2 = new Person();
- Person p3 = new Person();
- Person p4 = new Person();
- System.out.println(p1);
- System.out.println(p2);
- System.out.println(p3);
- System.out.println(p4);
- }
-
- }
-
- class Person{
-
- }
运行

一个类如果被使用了一百次会创建多少个对象?如果这个项目一共有一万个类呢?
1百万个
这样极大的占用了我们的内存空间
用单例模式
- package com.zking.beanLife;
-
- public class Demo1 {
- public static void main(String[] args) {
- Person p1 = Person.newInstance();
- Person p2 = Person.newInstance();
- Person p3 = Person.newInstance();
- Person p4 = Person.newInstance();
- System.out.println(p1);
- System.out.println(p2);
- System.out.println(p3);
- System.out.println(p4);
- }
-
- }
-
- class Person{
- private Person() {
-
- }
- private final static Person p = new Person();
- public static Person newInstance() {
- return p;
- }
- }
运行

一个类在100个地方使用,使用的但是同一个对象,这个项目一共有1w个类,请问Spring上下文
要创建多少个对象?
1w个
结论:
单例模式的优点:就是极大的节省了空间,省略了很多的没必要的对象创建。
所以Spring默认使用单例模式;
问题·:为什么spring框架作者要提供原型/多例和javabean管理模式进行选择?
原因:单例模式也有弊端
弊端:变量会被污染
- package com.zking.beanLife;
-
- /**
- * 为了印证BeanPostProcessor 初始化javabean
- * @author yzq
- *
- * 2022年8月9日上午10:12:25
- */
- public class InstanceFactory {
- public void init() {
- System.out.println("初始化方法");
- }
-
- public void destroy() {
- System.out.println("销毁方法");
- }
-
- public void service() {
- System.out.println("业务方法");
- }
- }
- package com.zking.beanLife;
-
- import java.util.List;
-
- /**
- * 印证单例多例模式的区别
- * @author yzq
- *
- * 2022年8月9日上午10:13:56
- */
- 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);
- }
- }
- package com.zking.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");
- // 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");
-
- }
-
- }
- <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 id="paramAction" class="com.zking.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.zking.beanLife.InstanceFactory"
- scope="prototype" init-method="init" destroy-method="destroy">bean>
- beans>

由此我们可以发现他们的num值已经不一样了就当于被污染了
我们改成多例模式在了运行一次

运行

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