目录
一,bean的初始化
(1)Spring的IOC和AOP:
(2)Spring Bean的生命周期:
二,单例模式与多例模式的区别
区别
《代码演示》
前言
回顾:Servlet的生命
初始化:init-----》Tomcat启动,servlet对象就创建/初始化了
服务:servlet----》浏览器发送请求,对应的servlet进行处理调用
销毁:destroy----》Tomcat停止
一,bean的初始化
如图
(1)Spring的IOC和AOP:
//初始化Spring上下文容器(IOC)
ioc是一个容器,Spring是管理所有的Javabean对象;
这些Javabean对象什么时候生,什么时间提供服务,什么时候销毁
初始化Spring容器的时候,是通过
ApplicationContext ac = new ClassXmlPathApplicationContext("spring.xml");对象始化的对应的就是图中xml
(2)Spring Bean的生命周期:
1)通过XML、Java 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
属性填充、修改等操作
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
1)Before
2)调用初始化Bean(InitializingBean和init-method,Bean的初始化才算完成)
3)After
完成了Bean的创建工作
8)destory:销毁
简单总结
1.通过三种方式(配置文件,注解,配置类)将bean便签转成BeanDefinition对象
· 2.通过BeanFactoryPostProcessor可以在初始化之前修啊属性值
3.BeanFactory进行bean实例化,说白了就是生产Javabean
4.Aware感知接口,能够在拿到Spring上下文中内部的资源对象
5.BeanPostProcessor后置处理器,相当于环绕通知
《代码测试》
在正式初始化之前会调用init方法,而我调用的this.init();方法会有专门的人员来写,就是BeanFactoryPostProcessor
《提取代码》
public Person() { this.init(); this.name="hhh"; this.sex="nan"; this.age=66; } private void init() { // TODO Auto-generated method stub }
《整体代码》
package com.zking.beanLife; public class Demo1 { public static void main(String[] args) { Person p = new Person(); p.setSex("nan"); System.out.println(p.getSex()); } } class Person{ private String name; private String sex; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Person() { this.init(); this.name="hhh"; this.sex="nan"; this.age=66; } private void init() { // TODO Auto-generated method stub } @Override public String toString() { return "Person [name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }
二,单例模式与多例模式的区别
区别
单例时,容器销毁instanceFactory对象也销毁;
单例模式下Javabean的生命周期:容器生对象生,容器死对象死
优点:内存使用少
弊端:存在变量污染
补充:当配置文件中没有scope=“”时默认就是单例,当scope=“prototype”中填写的是
prototype就是原型的模式(多例模式)但是Spring容器中默认的是单例singleton
多例时,容器销毁对象不一定销毁;
多例模式下Javabean的生命周期:使用时对象生,死亡跟着JVM垃圾回收站机制走
bean的初始化时间点,除了与bean管理模式(单例/多例)有关,还跟beanfactor的子类有关
优点:变量相对独立
弊端:内存占比较多
单例时,容器销毁instanceFactory对象也销毁;
单例模式下Javabean的生命周期:容器生对象生,容器死对象死
优点:内存使用少
弊端:存在变量污染
补充:当配置文件中没有scope=“”时默认就是单例,当scope=“prototype”中填写的是
prototype就是原型的模式(多例模式)但是Spring容器中默认的是单例singleton
多例时,容器销毁对象不一定销毁;
多例模式下Javabean的生命周期:使用时对象生,死亡跟着JVM垃圾回收站机制走
bean的初始化时间点,除了与bean管理模式(单例/多例)有关,还跟beanfactor的子类有关
优点:变量相对独立
弊端:内存占比较多
《代码演示》
package com.zking.beanLife;
import java.util.List;
public class ParamAction {
private int age;
private String name;
private List hobby;
//当初始化值是1
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);
}
}
num的值不一样,是由一个类创建了两个对象,但是两个对象是同一个对象,这就是单例
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("业务方法"); } }
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; 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); InstanceFactory p1 = (InstanceFactory) applicationContext.getBean("instanceFactory"); InstanceFactory p2 = (InstanceFactory) applicationContext.getBean("instanceFactory"); // 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"); } }
配置文件
抽烟 烫头 大保健
效果图
当在配置文件中加上scope=“”里面的值换成prototype多例模式,可以看到效果图,num值就变得不一样了