目录
一:生命周期
二:Spring生命周期
三:接口方法分类
四:spring生命周期详解
五:Bean的单例与多例模式
ps:单例模式
多例模式
一:生命周期
生命周期就是指一个对象的生老病死。
生命周期(Life Cycle)的概念应用很广泛,特别是在政治、经济、环境、技术、社会等诸多领域经常出现,其基本涵义可以通俗地理解为“从摇篮到坟墓”(Cradle-to-Grave)的整个过程。对于某个产品而言,就是从自然中来回到自然中去的全过程,也就是既包括制造产品所需要的原材料的采集、加工等生产过程,也包括产品贮存、运输等流通过程,还包括产品的使用过程以及产品报废或处置等废弃回到自然过程,这个过程构成了一个完整的产品的生命周期。
二:Spring生命周期
Spring的生命周期是从创建Spring容器开始,直到Spring容器销毁为止
注意:在IOC容器启动之后,并不会马上就实例化相应的bean,此时容器仅仅拥有所有对象的BeanDefinition(BeanDefinition:是容器依赖某些工具加载的XML配置信息进行解析和分析,并将分析后的信息编组为相应的BeanDefinition)。只有当getBean()调用时才是有可能触发Bean实例化阶段的活动
三:接口方法分类
Bean的完整生命周期经历了各种方法调用,这些方法可以划分为以下几类:
1、Bean自身的方法:这个包括了Bean本身调用的方法和通过配置文件中<bean>的init-method和destroy-method指定的方法
2、Bean级生命周期接口方法:这个包括了BeanNameAware、BeanFactoryAware、InitializingBean和DiposableBean这些接口的方法
3、容器级生命周期接口方法:这个包括了InstantiationAwareBeanPostProcessor 和 BeanPostProcessor 这两个接口实现,一般称它们的实现类为“后处理器”。
4、工厂后处理器接口方法:这个包括了AspectJWeavingEnabler, ConfigurationClassPostProcessor, CustomAutowireConfigurer等等非常有用的工厂后处理器 接口的方法。工厂后处理器也是容器级的。在应用上下文装配配置文件之后立即调用。
四:spring生命周期详解
①.实例化一个Bean-------------也就是我们常说的new;
②:通过Spring上下文容器(IOC)对实例化的Bean进行初始化配置--也就是IOC注入;
ApplicationContext ac= new ClassXmlPathApplicationContext("spring.xml");
③:通过XML、Java annotation(注解)以及Java Configuration(配置类)
等方式加载Spring Bean
④:BeanDefinitionReader:解析Bean的定义。在Spring容器启动过程中,
会将Bean解析成Spring内部的BeanDefinition结构;
可以理解为:将spring.xml中的<bean>标签转换成BeanDefinition结构,有点类似于XML解析
⑤:BeanDefinition:包含了很多属性和方法。例如:id、class(类名)、scope、ref(依赖的bean)等等。其实就是将bean(例如<bean>)的定义信息存储到这个对应BeanDefinition相应的属性中
<bean id="" class="" scope=""> -----> BeanDefinition(id/class/scope)
⑥:BeanFactoryPostProcessor:是Spring容器功能的扩展接口
ps:
- BeanFactoryPostProcessor在spring容器加载完BeanDefinition之后,在bean实例化之前执行的
- 对bean元数据(BeanDefinition)进行加工处理,也就是BeanDefinition属性填充、修改等操作
⑦:BeanFactory:bean工厂,它按照我们的要求生产我们需要的各种各样的bean。
BeanFactory -> List<BeanDefinition>
BeanDefinition(id/class/scope/init-method)
<bean class="com.zking.spring02.biz.BookBizImpl"/>
foreach(BeanDefinition bean : List<BeanDefinition>){
//根据class属性反射机制实例化对象
//反射赋值设置属性
}
⑧:Aware感知接口:在实际开发中,经常需要用到Spring容器本身的功能资源
例如:BeanNameAware、ApplicationContextAware等等接口
BeanDefinition 实现了 BeanNameAware、ApplicationContextAware
⑨:BeanPostProcessor:后置处理器。在Bean对象实例化和引入注入完毕后,在显示调用初始化方法的前后添加自定义的逻辑。(类似于AOP的绕环通知)
前提条件:如果检测到Bean对象实现了BeanPostProcessor后置处理器才会执行Before和After方法
BeanPostProcessor流程:
- Before
- 调用初始化Bean(InitializingBean和init-method,Bean的初始化才算完成)
- After
---------------------------------------------完成了Bean的创建工作---------------------------------------------------
⑩:destory:销毁
五:Bean的单例与多例模式
- 编写基础方法类
package com.zking.beanLife;
import java.util.List;
import com.zking.ioc.biz.UserBiz;
import com.zking.ioc.biz.impl.UserBizImpl1;
import com.zking.ioc.biz.impl.UserBizImpl2;
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);
}
}
package com.zking.beanLife;
public class InstanceFactory {
public void init() {
System.out.println("初始化方法");
}
public void destroy() {
System.out.println("销毁方法");
}
public void service() {
System.out.println("业务方法");
}
}
- 配置xml文件
<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>
- 编写测试代码
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");
}
}
ps:单例模式
- 优点:减少内存的消耗,是spring中bean默认的创建方式
- 缺点:会存在变量污染的问题
- 使用场景:会对bean对象中的属性进行修改
多例模式
- 优点:不存在变量污染的问题
- 缺点:增加内存的消耗
- 使用场景:不会对bean对象中的属性进行修改