Spring - IOC创建对象的时机,创建Bean的Scope 及其 IOC的 生命周期
来源:互联网 发布:js视频进度条 编辑:程序博客网 时间:2024/05/17 03:06
一. Spring容器创建Bean的Scope
spring容器创建bean的scope : 验证创建的对象是单例的还是多例的?
1. 由spring产生的bean默认是单例的;
2. 在spring配置文件中bean的属性scope="singleton/prototype/request/session/global session" 默认就是 singletion
3. 如果spring的配置文件的scope为prototype,则在得到该bean时(context.getBean)才创建对象;
4.示例验证 :
(1)新建类,验证集合 ,如果两个对象操作一个集合,集合长度改变,那么是同一个集合,则为单例模式;
public class HelloScope { public List<String> lists=new ArrayList<String>(); public void hello(){ System.out.println("Hello bean scope"); }}
(2)bean.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="HelloScope" class="cn.labelnet.scope.HelloScope"></bean></beans>
(3) 测试
@Testpublic void testHelloWordFactoryScope(){ApplicationContext applicationContext=new ClassPathXmlApplicationContext(BENA_CONTEXT_XML);HelloScope helloScope=(HelloScope) applicationContext.getBean("HelloScope");helloScope.lists.add("yuan");helloScope.lists.add("an");HelloScope helloScope2=(HelloScope) applicationContext.getBean("HelloScope");System.out.println(helloScope2.lists.size());}
(4)结果
(5)修改配置 设置 scope属性为prototype
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="HelloScope" class="cn.labelnet.scope.HelloScope" scope="prototype"></bean></beans>
(6)结果
二. spring容器创建对象的时机 :
1. 条件1: 在单例的情况下
(1) 时机 :1). 默认的情况下,启动spring容器时就创建对象了;
2). 在spring的配置文件中的一个属性lazy-init="default/true/false"
default/false: 在启动spring容器的时候创建对象;
true :在context.getBean时就要创建对象;
(2) 意义:
在默认情况下,启动spring容器的时候,检查spring容器配置文件的正确性,如果再结果tomcat,如果spring容器不能正常启动,整个tomcat就不能启动,但是这样的缺点就是把一些bean放在了内存中,如果有数据,则对内存来是一个消耗;
在为true时,减少内存消耗,但不会检测配置是否有错误;
2. 条件2 :
在多例的情况下,在conext.getBean时才创建对象;
3.配置 lazy-init属性
<bean id="HelloScope" class="cn.labelnet.scope.HelloScope" lazy-init="true"></bean>
三. spring容器的生命周期
1. spring容器创建对象
2. 执行init方法
3. 调用自己的方法
4. 当spring容器关闭的时候执行destory方法
5. 配置实现:
1). 初始化 init 对应 bean属性 init-method ;2). 销毁 destory 对应 bean属性 destory-method;
3). spring关闭的时候,销毁对象 (ClassPathXMLApplicationContext 对象有close方法)
4). 当scope为protitype时,spring容器不调用destory方法
<bean id="helloInitdestory" class="cn.labelnet.initdestory.HelloInitDestory" init-method="onInit" destroy-method="onDestory" scope="prototype"></bean>
6.示例
1)创建类
public class HelloInitDestory {public HelloInitDestory() { System.out.println("我是构造函数");}public void onInit(){System.out.println("我是初始化操作");}public void hello(){System.out.println("Hello , Spring !");}public void onDestory(){System.out.println("我是销毁操作");}}
2)创建bean.xml
init-method属性对应初始化方法,destory-method属性对应销毁方法;
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="helloInitdestory" class="cn.labelnet.initdestory.HelloInitDestory" init-method="onInit" destroy-method="onDestory" scope="prototype"></bean> </beans
3)测试
private final String APPLICATION_CONTEXT="ApplicationContext.xml";@Testpublic void testHelloInitDestory() {ApplicationContext applicationContext=new ClassPathXmlApplicationContext(APPLICATION_CONTEXT);HelloInitDestory helloInitDestory=(HelloInitDestory) applicationContext.getBean("helloInitdestory");helloInitDestory.hello();ClassPathXmlApplicationContext xmlApplicationContext=(ClassPathXmlApplicationContext)applicationContext; xmlApplicationContext.close();}
4)结果
5)更改bean非单例模式
<bean id="helloInitdestory" class="cn.labelnet.initdestory.HelloInitDestory" init-method="onInit" destroy-method="onDestory" scope="prototype"></bean>
执行后,就没有执行销毁方法;即 当scope为protitype时,spring容器不调用destory方法;
6)结果
四.Demo免积分下载
http://download.csdn.net/detail/lablenet/9377346
0 0
- Spring - IOC创建对象的时机,创建Bean的Scope 及其 IOC的 生命周期
- Spring的IOC、Spring对象初始化bean时机、Spring容器生命周期
- Spring的IOC、Spring对象初始化bean时机、Spring容器生命周期
- Spring IOC -bean对象的生命周期详解
- Spring IOC -bean对象的生命周期详解
- Spring IOC -bean对象的生命周期详解
- Spring IOC -bean对象的生命周期详解
- Spring IOC 创建bean实例的方式
- spring(IOC) 对象创建时机、对象作用域scope、对象的初始化方法和销毁方法的调用、spring容器总结
- Spring 创建bean的时机
- Spring IOC中bean的生命周期
- Spring IOC容器中Bean的生命周期
- [Spring]Bean在IOC容器的生命周期
- Spring IOC容器中Bean的生命周期
- 【Spring源码--IOC容器的实现】(五)Bean对象的创建
- spring 8 bean配置--IOC容器中bean的生命周期
- 重温spring框架1--ioc创建对象的方式
- spring IOC容器创建对象的三种方式
- 响应式-瀑布流
- aapt命令详解
- android 图片 高斯模糊 Blur Android Studio JNI NDK 生成 so 问题汇总
- Android线程模型--在子线程中更新UI
- 实现圆形头像效果
- Spring - IOC创建对象的时机,创建Bean的Scope 及其 IOC的 生命周期
- 代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧
- 关于更新局部listview中数据的问题
- 阅读任玉刚博客有感
- webdriver UI自动化测试执行JS脚本
- vc中关闭对话框dialog
- KING_UNITY学习之鼠标点击地面控制人物移动
- LeetCode 144:Binary Tree Preorder Traversal
- qpixmap 鼠标移动,局部放大