目录
一、bean的初始化过程
Spring Bean的生命周期:
1)通过XML、Java annotation(注解)以及Java Configuration(配置类)
等方式加载Spring Bean
2)BeanDefinitionReader:解析Bean的定义。
3)BeanDefinition:包含了很多属性和方法。
4)BeanFactoryPostProcessor:是Spring容器功能的扩展接口。
5)BeanFactory:bean工厂。它按照我们的要求生产我们需要的各种各样的bean。
6)Aware感知接口:在实际开发中,经常需要用到Spring容器本身的功能资源
7)BeanPostProcessor:后置处理器。
8)destory:销毁
二、Bean的单例与多例模式
1、定义实体ParamAction类
2、编写方法
3、配置spring-context.xml
4、测试单例与多例
5、单例与多例的区别
1)单例:
2)多例:
3)单例与多例选择:
4)单例的生命周期
5)多例的生命周期
一、bean的初始化过程
Spring Bean的生命周期:
1)通过XML、Java annotation(注解)以及Java Configuration(配置类)
等方式加载Spring Bean
2)BeanDefinitionReader:解析Bean的定义。
在Spring容器启动过程中,会将Bean解析成Spring内部的BeanDefinition结构;
理解为:将spring.xml中的标签转换成BeanDefinition结构,有点类似于XML解析
2)BeanDefinitionReader:解析Bean的定义。
在Spring容器启动过程中,会将Bean解析成Spring内部的BeanDefinition结构;
理解为:将spring.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:销毁
二、Bean的单例与多例模式
1、定义实体ParamAction类
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 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);
}
}
1、定义实体ParamAction类
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 Listhobby; 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); } }
2、编写方法
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("业务方法"); } }
3、配置spring-context.xml
抽烟 烫头 大保健
4、测试单例与多例
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); 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"); } }
5、单例与多例的区别
1)单例:
优点:减少内存的使用,也是bean默认的创建方式
缺点:会存在变量污染的问题
2)多例:
优点:不存在变量污染
缺点:耗内存
3)单例与多例选择:
是否会对bean对象中的属性进行修改,如果会使用多例,如果不会使用单例。
4)单例的生命周期
伴随容器的生成与销毁
5)多例的生命周期
bean在使用的时候才进行初始化,销毁与容器五个,与java的垃圾回收机制有关



