- 浏览: 193171 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (121)
- java_基础 (26)
- java_web (15)
- javascript (9)
- extJS (0)
- dwr (0)
- guobin_ETL (1)
- guobin_ssh (17)
- guobin_webservice (1)
- guobin_jbpm (0)
- guobin_jquery (0)
- guobin_acegi (1)
- guobin_poi/jxl (2)
- guobin_multiThread (0)
- guobin_名称解释 (0)
- guobin_FLEX (0)
- guobin_php (0)
- guobin_ORACLE (1)
- guobin_mysql (0)
- guobin_linux (0)
- guobin_powerDesigner (0)
- guobin_visol (0)
- guobin_ER/STUDIO (0)
- guobin_jmesa (0)
- guobin_weblogic (0)
- guobin_kettle (0)
- guobin_一路风雨 (5)
- guobin_spark (0)
- java_xml (7)
- guobin_规则引擎 (0)
- 插件开发 (2)
- 软件管理 (3)
- spring mvc (2)
- java_jni (1)
- eclipse (1)
- oracle (0)
- 项目学习笔记 (1)
- java多线程学习记录 (9)
- websphere性能设置和日常维护 (2)
- (1)
- 系统软件架构设计 (1)
- ces (1)
- 需求分析 (2)
- IBM-CICS GATEWAY (1)
- 工具使用 (1)
- 网络信息安全编程 (1)
最新评论
-
yzh__:
学习一个
Hibernate 自关联注解方式 -
xiaoyahuang:
"2)将Sysdeo Plugin下的DevLoad ...
eclipse项目引用 -
guobin6125:
guobin6125 写道sfasshahhah评论的评论
欢迎使用Kit! -
guobin6125:
sfass
欢迎使用Kit! -
guobin6125:
tst23
欢迎使用Kit!
一、Spring Framework支持五种作用域(其中有三种只能用在基于web的Spring ApplicationContext)。
内置支持的作用域分列如下:
singleton |
在每个Spring IoC容器中一个bean定义对应一个对象实例。 |
prototype |
一个bean定义对应多个对象实例。 |
request |
在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例, 它们依据某个bean定义创建而成。该作用域仅在基于web的Spring |
session |
在一个HTTP |
global session |
在一个全局的HTTP |
1.Singleton作用域
当一个bean的作用域为singleton, 那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。换言之,当把一个bean定义设置为singlton作用域时,Spring IoC容器只会创建该bean定义的唯一实例。这个单一实例会被存储到单例缓存(singleton cache)中,并且所有针对该bean的后续请求和引用都将返回被缓存的对象实例。
请注意Spring的singleton bean概念与GoF模式一书中定义的Singleton模式是完全不同的。经典的GoF Singleton模式中所谓的对象范围是指在每一个ClassLoader
中 指定class创建的实例有且仅有一个 。把Spring的singleton作用域描述成一个container
(容器)对应一个bean 实例最为贴切。也就是说假如在单个Spring容器内定义了某个指定class的bean,那么Spring容器将会创建一个并且仅有一个 由该bean定义指定的类实例。Singleton作用域是Spring中的缺省作用域 。要在XML中将bean定义成singleton,可以这样配置:
- <bean id="accountService" class="com.foo.DefaultAccountService"/>
- <!-- the following is equivalent, though redundant (singleton scope is the default); using spring-beans-2.0.dtd or upper-->
- <bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/>
- <!-- the following is equivalent and preserved for backward compatibility in spring-beans.dtd -->
- <bean id="accountService" class="com.foo.DefaultAccountService" singleton="true"/>
测试:
beans.xml配置文件中配置如下所示:
测试代码如下所示:
- public class SpringTest {
- @Test
- public void instanceSpring(){
- ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- PersonService personService1 = (PersonService)ctx.getBean("personService");
- PersonService personService2 = (PersonService)ctx.getBean("personService");
- System.out.println(personService1==personService2);
- }
- }
运行测试程序,结果输出为: true .说明是只创建了一个PersonServiceBean.
2. Prototype作用域
Prototype作用域的bean会导致在每次对该bean请求(将其注入到另一个bean中,或者以程序的方式调用容器的getBean()
方法)时都会创建一个新的bean实例 。根据经验,对有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用singleton作用域。
下图演示了Spring的prototype作用域。请注意,通常情况下,DAO不会被配置成prototype,因为DAO通常不会持有任何会话状态,因此应该使用singleton作用域。
在XML中将bean定义成prototype,可以这样配置:
- <!-- using spring-beans-2.0.dtd or upper -->
- <bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/>
- <!-- the following is equivalent and preserved for backward compatibility in spring-beans.dtd -->
- <bean id="accountService" class="com.foo.DefaultAccountService" singleton="false"/>
将beans.xml配置文件中的配置信息改为如下所示:
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="prototype"></bean>
再运行上面的测试程序,输出结果为: false ,说明创建了两个PersonServiceBean.
其他作用域,即request、session以及global session 仅在基于web的应用中使用(不必关心你所采用的是什么web应用框架)。这些作用域仅仅在使用基于web的Spring ApplicationContext实现(如XmlWebApplicationContext)时有用。 如果在普通的Spring IoC容器中,比如像XmlBeanFactory或ClassPathXmlApplicationContext, 尝试使用这些作用域,你将会得到一个IllegalStateException异常(未知的bean作用域)。
二、Bean的生命周期
(1)什么时候初始化bean实例
当scope=singleton,即默认情况,会在容器初始化时实例化。但我们可以指定Bean节点的lazy-init=”true”来延迟初始化bean,这时候,只有第一次获取bean才会初始化bean,即第一次请求该bean时才初始化。如下配置所示:
如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=”true”,如下所示:
当scope=prototype时,也会延迟初始化bean,即第一次请求该bean时才初始化(如调用getBean()方法时).
测试如下:
PersonServiceBean代码如下所示:
- public class PersonServiceBean implements PersonService {
- public void init(){
- System.out.println("Get the database connection and initialize other beans!");
- }
- public PersonServiceBean(){
- System.out.println("PersonServiceBean is initialized!");
- }
- public void destroy(){
- System.out.println("close the database connection!");
- }
- }
beans.xml配置文件如下所示:
- <bean id="personService" class="examples.test.PersonServiceBean" /> <!-- 1 -->
- <bean id="personService" class="examples.test.PersonServiceBean" scope="singleton" /> <!-- 2 -->
测试程序如下所示:
- public class SpringTest {
- @Test
- public void instanceSpring(){
- ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- System.out.println("------------------");
- PersonService personService = (PersonServiceBean)ctx.getBean("personService");
- }
- }
分1,2两种情况运行程序,控制台打印信息如下所示:
PersonServiceBean is initialized!
------------------
将配置文件修改成如下所示:
- <bean id="personService" class="examples.test.PersonServiceBean" lazy-init="true" /> <!-- 1 -->
- <bean id="personService" class="examples.test.PersonServiceBean" scope="prototype" /> <!-- 2 -->
分1,2两种情况运行程序,控制台打印信息如下所示:
------------------
PersonServiceBean is initialized!
以上测试说明:
Bean默认是在容器初始化时初始化的,即
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");语句执行时就实例化bean了。如果把scope设成scope=”prototype” 或设置lazy-init=”true”,则会延迟bean的实例化,bean会在PersonService personService = (PersonServiceBean)ctx.getBean("personService");语句执行时才实例化。
当配置文件为如下所示时,测试结果同上。
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context= "http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-lazy-init="true"
- >
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" >
- </beans>
(2)生命周期:
构造器、init方法、获取bean后的操作、destroy方法(ctx.close时执行).
注意:如果bean的scope设为prototype时,当ctx.close时,destroy方法不会被调用.
原因:对于prototype作用域的bean,有一点非常重要,那就是Spring不能对一个prototype bean的整个生命周期负责:容器在初始化、配置、装饰或者是装配完一个prototype实例后,将它交给客户端,随后就对该prototype实例不闻不问了。不管何种作用域,容器都会调用所有对象的初始化生命周期回调方法。但对prototype而言,任何配置好的析构生命周期回调方法都将不会 被调用。清除prototype作用域的对象并释放任何prototype bean所持有的昂贵资源,都是客户端代码的职责。(让Spring容器释放被prototype作用域bean占用资源的一种可行方式是,通过使用bean的后置处理器,该处理器持有要被清除的bean的引用。)谈及prototype作用域的bean时,在某些方面你可以将Spring容器的角色看作是Java new
操作的替代者。任何迟于该时间点的生命周期事宜都得交由客户端来处理。
配置文件信息如下:
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" init-method="init" destroy-method="destroy"/>
测试程序如下:
- public class SpringTest {
- @Test
- public void instanceSpring(){
- AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- System.out.println("------------------");
- PersonService personService = (PersonServiceBean)ctx.getBean("personService");
- ctx.close();
- }
运行测试程序,控制台打印信息如下所示:
PersonServiceBean is initialized!
Get the database connection and initialize other beans!
------------------
close the database connection!
修改配置文件信息如下:
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="prototype" init-method="init" destroy-method="destroy"/>
再次运行测试程序,控制台打印信息如下所示:
------------------
PersonServiceBean is initialized!
Get the database connection and initialize other beans!
修改配置文件信息如下所示:
- <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" lazy-init="true" init-method="init" destroy-method="destroy"/>
此时再次运行测试程序,控制台打印信息如下所示:
------------------
PersonServiceBean is initialized!
Get the database connection and initialize other beans!
close the database connection!
发表评论
-
OpenSessionInViewFilter失效问题解决(SSH2)
2012-07-19 14:07 2264今天在用Hibernate延迟加载时,显示到界面出现如下问题: ... -
Hibernate 自关联注解方式
2012-07-18 18:08 10810Hibernate自关联(使用注解) Hib ... -
hibernate 缓存机制
2012-07-18 11:54 1039CacheConcurrencyStrategy有五种缓 ... -
Spring事务的传播行为和隔离级别[transaction behavior and isolated level]
2012-07-17 23:43 0事务的传播行为和隔离级别[transaction beh ... -
EJB中7个事务属性(PROPAGATION_REQUIRED等)以及5个隔离属性
2012-07-17 23:42 0EJB中7个事务属性(PROPAGATION_REQUI ... -
junit测试之hiberante延迟加载问题
2012-07-17 22:25 0junit测试之hiberante延迟加载问题 ... -
Hibernate 注解对Hibernate属性(CascadeType、JoinColumn、JoinTable、ForeignKey等)的研究
2012-07-17 20:28 6706------------------------------- ... -
Hibernate SQL方言 (hibernate.dialect) Spring配置文件applicationContext.xml
2012-07-17 07:56 8656Hibernate SQL方言 (hibernate.d ... -
hibernate注解 关系映射 -多对多
2012-07-16 23:23 929使用Hibernate Annotations 维护多对 ... -
spring 中配置sqlserver2008
2012-07-16 23:12 1023不得不说的在 MS SQL SERVER 下的 JDBC ... -
BeanCreationException: No unique bean of type
2012-07-16 13:04 1015我定义了一个基类接口BaseDao,下面有些update\sa ... -
Struts 1 和 Struts 2 的线程安全
2012-06-20 10:24 805昨天人问我Struts 2是怎么保证全局变量的线程安全 ... -
学习Spring必学的Java基础知识(1)----反射
2012-05-22 13:40 746http://www.iteye.com/topic/1123 ... -
Hibernate总结篇
2012-04-26 07:47 1318一、HQL经验总结: HQL ... -
Hibernate HQL语句的拼写
2012-04-22 19:11 1012Hibernate HQL语句的拼写 ... -
HQL中如何实现select top n这样的功能?
2012-04-22 19:09 2544HQL中如何实现select top n这样的功能? ... -
使用strtuts2的iterator标签循环输出二维数组和嵌套集合
2012-04-22 17:31 1250其实都一样的嵌套对象跟二维数组。先看看嵌套对象 ... -
hibernate注解 关系映射
2012-04-17 17:12 10591)一对一外键关联映射(单向) @OneToOne(c ... -
Hibernate 注解及简单实例
2012-04-17 16:09 685hibernate注解 1、@Entity(name= ...
相关推荐
主要介绍了浅谈Spring中Bean的作用域、生命周期,具有一定借鉴价值,需要的朋友可以参考下
主要给大家介绍了Spring中Bean的生命周期和作用域及实现方式的相关资料,文中介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
主要介绍了深入了解Spring中Bean的作用域和生命周期,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
NULL 博文链接:https://cici9898-007.iteye.com/blog/1407695
主要介绍了SPRING FRAMEWORK BEAN作用域和生命周期原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Spring Bean的生命周期? Spring中bean的作用域 说一下Spring基于xml注入bean的几种方式? Spring如何解决循环依赖问题? Spring的自动装配 Spring框架中都用到了哪些设计模式? Spring框架中有哪些不同类型的事件?
代码及ppt涵盖 Spring4.0 的所有核心内容:在 Eclipse 中安装 SpringIDE 插件、IOC & DI、在 Spring 中配置 Bean、自动装配、Bean 之间的关系(依赖、继承)、Bean 的作用域、使用外部属性文件、SpEL、管理 Bean 的...
请解释Spring Bean的生命周期 Spring Bean的作用域之间有什么区别 什么是Spring inner beans Spring框架中的单例Beans是线程安全的么 请举例说明如何在Spring中注入一个Java Collection 如何向Spring Bean中注入一个...
spring的生命周期及BeanPostProcessor的使用,注解方式创建bean 及使用Autowried标签关联类的属性 ,泛型依赖注入的使用 spring-3 演示使用动态代理模式实现面向切面编程 使用注解方式进行AOP编程及使用配置xml方式...
11、Spring Bean 的作用域之间有什么区别? 12、什么是 Spring inner beans? 13、Spring 框架中的单例 Beans 是线程安全的么? 14、请举例说明如何在 Spring 中注入一个 Java Collection? 15、如何向 Spring Bean ...
摘录了spring的核心逻辑,代码极度简化,保留spring的核心功能,如IoC和AOP,资源加载器,事件监听器,类型转换,容器扩展点,bean生命周期和作用域,应用某些等核心功能。 如果本项目能帮助到你,请给个STAR,谢谢...
11、Spring Bean 的作用域之间有什么区别? 12、什么是 Spring inner beans? 13、Spring 框架中的单例 Beans 是线程安全的么? 14、请举例说明如何在 Spring 中注入一个 Java Collection? 15、如何向 Spring Bean ...
spring框架技术+第2天+xmind思维导图:生命周期,介绍simple project,打印出构造方法...bean作用域request session globalSession:web项目获取核心配置文件要配置两个地方:spring监听器、spring作用域范围的监听。
1.spring中bean实例化的俩种方式 2.spring中bean的各种元素如何使用 ...4.spring中不同类型bean的作用域 5.spring中bean的生命周期 通过我的项目,可以跟着学习SPringIoC容器和Bean管理,帮助你快速上手SSM框架
提取了Spring的核心逻辑,极大地简化了代码,并完善了Spring的核心功能,例如IoC和AOP,资源加载器,事件侦听器,类型转换,容器扩展点,Bean生命周期和作用域以及应用程序上下文,被保留。 如果这个项目可以为您...
Hibernate: 第一课:第一个hibernate项目 第二课:测试实体对象的生命周期 ...第三课:spring Bean的作用域 第四课:spring对AOP的只是(采用Annotation的方式) 第五课:spring对AOP的只是(采用配置文件的方式)
3. Spring Bean部分围绕Bean的生命周期、作用域、自动装配等进行了详细阐述。 4. 注解部分列举了常用的Spring注解及其作用,如@Component、@Autowired等。 5. 数据访问部分涉及了Spring对JDBC、Hibernate等数据访问...
05_配置Spring管理的bean的作用域 06_Spring管理的Bean的生命周期 07_编码剖析Spring依赖注入的原理 08_编码剖析Spring装配基本属性的原理 09_Spring如何装配各种集合类型的属性 10_使用构造器装配属性 11_用...
01_全面阐释Spring及其各项功能 ...05_配置Spring管理的bean的作用域 06_Spring管理的Bean的生命周期 07_编码剖析Spring依赖注入的原理 08_编码剖析Spring装配基本属性的原理 09_Spring如何装配各种集合类
25. 解释 Spring 支持的几种 bean 的作用域 26. Spring 框架中的单例 bean 是线程安全的吗? 27. 解释 Spring 框架中 bean 的生命周期 28. 哪些是重要的 bean 生命周期方法? 你能重载它们吗? 29. 什么是 Spring ...