Bean的生命周期

一、bean的生命周期

1、servlet与Bean生命周期对比

前面我们有接触到servlet的生命周期。
初始化init:对应的toTomcat启动,那么servlet的对象就初始化/创建
服务service:浏览器发送请求对应的service进行处理调用
销毁destroy:Tomcat通知

Spring是管理项目中所有的JavaBean对象
bean生命周期图在这里插入图片描述

2、Spirng Bean的生命周期

1)通过XMLJava 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
属性填充、修改等操作

package com.xlb.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="未知";
	}
	//在实例化之前进行加工
	private void init() {
		// TODO Auto-generated method stub
	}
	
}

在这里插入图片描述

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
(感知接口拿到Spring的对象)
如:
在这里插入图片描述

7)BeanPostProcessor:后置处理器。在Bean对象实例化和引入注入完毕后,
在显示调用初始化方法的前后添加自定义的逻辑。(类似于AOP的绕环通知)
前提条件:如果检测到Bean对象实现了BeanPostProcessor后置处理器才会执行
Before和After方法
BeanPostProcessor
1)Before
2)调用初始化Bean(InitializingBean和init-method,Bean的初始化才算完成)
3)After
完成了Bean的创建工作

8)destory:销毁

二、bean的单例多例模式

1、理论

当我们一个类创建四次对象时,那么如果不是单例模式则会有多少个类就会出创建多少个对象,那么会极大的占用内存空间

如:

package com.xlb.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{
		
	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.name="zs";
		this.age=20;
		this.sex="未知";
	}
	
	
}

在这里插入图片描述

1.1、单例模式(默认)

还是以上面类为例,Spring作者把构造器私有化,然后提供一个构造方法,那么该对象就在整个对象是唯一的

package com.xlb.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();*/
		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;
	}
}

在这里插入图片描述

1.1.1、优缺点

优点:节约内存,运行效率快
在这里插入图片描述
缺点:变量污染

假如一个类有一个属性在最开始是设置了初始值,那么如果其他类改变了初始值,那么就会污染该变量
在这里插入图片描述

2.2、多例模式

2.2.1、优缺点

优点:

防止并发问题;即一个请求改变了对象的状态,此时对象又处理另一个请求,而之前请求对对象的状态改变导致了对象对另一个请求做了错误的处理

缺点:

对象在整个系统中可以有多个实例,每个请求用一个新的对象来处理

2、代码论证

2.1、前置代码准备

我们创建三个类
第一个类InstanceFactory.java(为了印证BeanPostProcessor 初始化javabean

package com.xlb.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.java(印证单例模式跟多例模式区别

package com.xlb.beanLife;

import java.util.List;

/**
 * 印证单例模式跟多例模式区别
 * @author Administrator
 *
 */
public class ParamAction {
	private int age;
	private String name;
	private List<String> hobby;
	private int num = 1;
	// private UserBiz userBiz = new UserBizImpl1();

	public ParamAction() {
		super();
	}

	public ParamAction(int age, String name, List<String> 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.java

package com.xlb.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");
		
	}

}

还需要一个赋值理论配置

<!-- 辅助理论配置 -->
	<bean id="paramAction" class="com.xlb.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.xlb.beanLife.InstanceFactory"
		scope="prototype" init-method="init" destroy-method="destroy"></bean>

2.2、开始验证

首先我们验证我们的测试类的text1,因为id为paramAction,且共有变量num,有因为我们配置文件的Scope的变量默认为单例所以结果是污染的单例模式

// 体现单例与多例的区别
	@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();
	}

在这里插入图片描述
然后我们在配置文件中添加一个属性Scope改为多例模式
在这里插入图片描述
在运行结果为
在这里插入图片描述
这就验证了我们Spring容器默认是单例模式

三、总结

1、javabean生命周期

1)通过三种方式(配置文件,注解,配置类)将Bean标签转为BeanDefinitionReader对象
2)通过BeanFactoryPostProcessor初始化之前修改属性值
3)BeanFactory进行bean实例化(生产javabean)
4)Aware感知接口,能够在拿到Spring上下文中内部资源对象
5)BeanPostProcessor后置处理器,相当于环绕通知

单例模式下的JavaBean的生命周期
容器生对象生,容器死对象死
多例模式是使用时对象生,死亡跟着jvm垃圾回收机制走

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值