1.Sparing容器管理javabean的初始化过程
Spring的IOC和AOP:
初始化Spring上下文容器(IOC)
ApplicationContext ac=new ClassXmlPathApplicationContext("spring.xml");
Spring Bean的生命周期:
1)通过XML、Java annotation(注解)以及Java Configuration(配置类)
等方式加载Spring Bean
2)BeanDefinitionReader:解析Bean的定义。在Spring容器启动过程中,
会将Bean解析成Spring内部的BeanDefinition结构;
理解为:将spring.xml中的<bean>标签转换成BeanDefinition结构
有点类似于XML解析
3)BeanDefinition:包含了很多属性和方法。例如:id、class(类名)、
scope、ref(依赖的bean)等等。其实就是将bean(例如<bean>)的定义信息
存储到这个对应BeanDefinition相应的属性中
例如:
<bean id="" class="" scope=""> -----> BeanDefinition(id/class/scope)
4)BeanFactoryPostProcessor:是Spring容器功能的扩展接口。
注意:
1)BeanFactoryPostProcessor在spring容器加载完BeanDefinition之后,
在bean实例化之前执行的
2)对bean元数据(BeanDefinition)进行加工处理,也就是BeanDefinition
属性填充、修改等操作
5)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属性反射机制实例化对象
//反射赋值设置属性
}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:豆子
Definition:定义、阐述
Reader:读取
aware:感知
destory:销毁
总结:
1.在xml/annotation/configuation配置javabean
2.BeanDefinitionReader解析配置的javaBean得到BeanDefiniton,最终的到BeanDefinition,最终的到一个List<BeanDefinitionReader>的集合,说白了这就是一个建模的过程
3.触发BeanFactoryPostProcessor,javaBean初始化之前执行你自己的业务;
4.spring中的beanFacotry(bean工厂),会通过LIst<BeanDefinitionReader>集合遍历初始化所有的JavaBean对象
5.如果自己的javabean需要调动spring上下文中的资源(资源指的方法或属性),那么需要事项Aware感知接口
6.如果自己的javabean已经给你初始化好了,还需要扩展功能呢,需要借助BeanPostProcessor来帮你完成
2.spring中javabean是单例的还是多例的:
1.默认是单例的但是可以配置多例
2.单例的优点,节约内纯,弊端,是有变量污染
多例的优点:无变量污染,弊端:非常消耗内纯
打个比方,又一个家庭有两个孩子,两个小孩都想要小汽车,那这个时候,这个家庭为了省钱只买了(单例模式)一辆小汽车,哥哥早上玩,弟弟下午玩,汽车买回去后,哥哥早上玩,让后到弟弟下午玩的时候,小汽车可能就没电了,或者说,汽车外观上就收到了破坏(变量污染)
那么做好处就是省钱了(节约内存)
那么如果买两辆(多例模式)小汽车,那么就没有上面那种情况(变量污染),但是花的钱多了(非常消耗内纯)
代码演示:
单例:
package xzs.beanlife;
import java.util.List;
import xzs.ioc.serverce.UserServerce;
import xzs.ioc.serverce.impl.UserServerceImpl;
import xzs.ioc.serverce.impl.UserServerceImpl2;
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 xzs.beanlife;
public class InstanceFactory {
public void init() {
System.out.println("初始化方法");
}
public void destroy() {
System.out.println("销毁方法");
}
public void service() {
System.out.println("业务方法");
}
}
<!-- springde1bean的生命周期-->
<bean id="paramAction" class="xzs.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>
</beans>
package xzs.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");
}
}
那么运行结果为:
那么可以看出单例模式确确实实会造成变量污染,第一次使用的时候是一当是第二次的时候确实2了
那么接下来是多例模式演示:
注意:上面提到过单例模式是默认的,那么在这里要使用多例模式就是需要指定:
也就是说要在paraAction那个bean标签中添加scope的属性且值设置为:prototy
<bean id="paramAction" class="xzs.beanlife.ParamAction" scope="prototype">
<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="xzs.beanlife.InstanceFactory"
scope="prototype" init-method="init" destroy-method="destroy"></bean>
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();
}
在上面一段代码中我们比较p1与p2看他们内纯地址是否相同
结果:
可以看出的到的false也即是说在第二次使用指定的对象时又重新开批了一个内纯空间
以上便是多例模式和单例模式的一个代码演示。
3.单例:javabean是跟着spring上下文初始化:容器生,对象说,容器死,对象死
多例:javabean是使用的时候才会创建,销毁跟着jvm走。
代码验证:
单例:
<bean id="instanceFactory" class="xzs.beanlife.InstanceFactory"
scope="singleton" init-method="init" destroy-method="destroy"></bean>
public void test2() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
}
结果:
以上代码可以得出单例模式,对象在spring上下文初始化就有了
多例:
<bean id="instanceFactory" class="xzs.beanlife.InstanceFactory"
scope="prototype" init-method="init" destroy-method="destroy"></bean>
测试:
没有调用instanceFactory标签
public void test2() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
}
结果:
调用instanceFactory标签
public void test2() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-context.xml");
Object instanceFactory = applicationContext.getBean("instanceFactory");
}
上调用instanceFactory标签出现了初始化方法,而没调用时则没出现
目录
3.单例:javabean是跟着spring上下文初始化:容器生,对象说,容器死,对象死
多例:javabean是使用的时候才会创建,销毁跟着jvm走。
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的初始化,单例模式立马会执行,但是此时XmlBeanFactory作为子类,单例模式下容器创建,bean依赖没有初始化,只有要获取使用bean对象才进行初始化
结果:
那么现在使用他
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");
}
结果: