目录
一、bean的生命周期原理
二、单例多例理论
2.1 理论讲解
2.1.2 单例
2.2.2 多例
2.2 代码论证
根据以上图片进行对bean的生命周期进行讲解,从下往上顺时针方法观看以上图片
根据之前的讲解知道Spring是管理项目中所有的JavaBean对象;
问题1:这些JavaBean对象什么时候生,什么时间提供服务,什么时候销毁?
1)根据图解可知有三种方式加载Spring Bean、初始化BeanDefinition,分别为:xml、Java annotation(注解)以及Java Configuration(配置类)
2)在上一步中会用到一个类:BeanDefinitionReader该类,主要作用为解析Bean的定义。在Spring容器启动过程中,会将Bean解析成Spring内部的BeanDefinition结构;可以理解成为将spring.xml中的
3)BeanDefinition:包含了很多属性和方法。例如:id、class(类名)、scope、ref(依赖的bean)等等。其实就是将bean(例如
可以知道BeanDefinition该类对应于xml中的bean标签。
4)BeanFactoryPostProcessor:是Spring容器功能的扩展接口。
注意:
1)BeanFactoryPostProcessor在spring容器加载完BeanDefinition之后,
在bean实例化之前执行的
2)对bean元数据(BeanDefinition)进行加工处理,也就是BeanDefinition
属性填充、修改等操作
例子:
当去实现一个类的,有没有可能去是实现一个方法,而该方法就是进行去给属性赋值
package com.mgy.beanLife; public class Demo1 { public static void main(String[] args) { Person p=new Person(); System.out.println(p.getSex()); } } class Person{ private String name; private int age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Person() { this.init();//假设该方法是在实例化对象之前进行赋值的方法 this.name="zs"; this.age=20; this.sex="未知"; } public void init() { } public Person(String name, int age, String sex) { super(); this.name = name; this.age = age; this.sex = sex; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]"; } }
5)BeanFactory:bean工厂。它按照我们的要求生产我们需要的各种各样的bean。
实例化bean:将全路径名进行反射实例化;最终进入到Spring上下文
列如:
BeanFactory -> List
BeanDefinition(id/class/scope/init-method)
foreach(BeanDefinition bean : List){
//根据class属性反射机制实例化对象
//反射赋值设置属性
}
6) Aware感知接口:以下图都是感知接口
在实际开发中,经常需要用到Spring容器本身的功能资源
例如:BeanNameAware、ApplicationContextAware等等
BeanDefinition 实现了 BeanNameAware、ApplicationContextAware
结论:Aware接口帮助你拿到内置对象
7)BeanPostProcessor:后置处理器。在Bean对象实例化和引入注入完毕后,在显示调用初始化方法的前后添加自定义的逻辑。(类似于AOP的绕环通知)
简单的说:
1.通过三种方式(配置文件、注解、配置类)将bean标签转成beanbifintion对象
2.通过BeanFactoryPostProcessor可以在初始化之前修改属性值
3.BeanFactory进行bean实例化,说白了就是生成JavaBean
4.Aware感觉接口,能够在拿到Spring上下文中内部的资源对象
5.BeanPostProcessor后置处理器,相当于环绕通知
在bean标签上设置scope="singleton"该属性为单例模式,但bean标签默认为单例模式
· 生命周期:容器生对象生,容器死对象死
在bean标签上设置scope="prototype"该属性为多例模模式
优点:不存在线程安全问题(静态共享资源除外)
缺点:多个实例,内存占用多,GC开销大
生命周期:使用时对象生,死亡跟着jvm垃圾回收机制走
建立以下三个类:
ParamAction:
package com.mgy.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);
}
}
InstanceFactory:
package com.mgy.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("业务方法");
}
}
Demo2:
package com.mgy.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");
}
}
将以下代码加入到spring-context.xml
抽烟
烫头
大保健
第一次test1方法的结果:
因为p1和p2都是用到同一对象,所以p2.execute()被 p1的改变了
切换为多例模式 test1方法的效果为:
因为p1和p2都是用到同一对象,所以看到的东西都是相同的
spring的默认模式是单例模式,然后切换到单例模式看看是不是和第一次的运行结果相同
验证单例的生命周期:
将spring.xml中的instanceFactory改为单例模式。
test1的方法:
@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 p1 = (InstanceFactory) applicationContext.getBean("instanceFactory");
InstanceFactory p2 = (InstanceFactory) applicationContext.getBean("instanceFactory");
// 单例时,容器销毁instanceFactory对象也销毁;多例时,容器销毁对象不一定销毁;
applicationContext.close();
}
效果
多例模式:
死亡不好证明,因为容器关了都还没有告诉你。
体现单例与多例的初始化的时间点 instanceFactory已经在上面展示了
test3:
默认情况下bean的初始化,单例模式立马会执行,但是此时XmlBeanFactory作为子类,单例模式下容器创建,bean依赖没有初始化,只有要获取使用bean对象才进行初始化
单例模式:
因为没有使用,将test3中的i1给解除注销后,才会打印初始化成功
bean的初始化时间点,除了与bean管理模式(单例/多例)有关,还跟BeanFactory的之类有关