一、Spring Framework支持五种作用域(其中有三种只能用在基于web的Spring ApplicationContext)。
内置支持的作用域分列如下:
作用域
描述
singleton
|
在每个Spring IoC容器中一个bean定义对应一个对象实例。
|
prototype
|
一个bean定义对应多个对象实例。
|
request
|
在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例,
它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext
情形下有效。
|
session
|
在一个HTTP Session
中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext
情形下有效。
|
global session
|
在一个全局的HTTP Session
中,一个bean定义对应一个实例。典型情况下,仅在使用portlet
context的时候有效。该作用域仅在基于web的Spring ApplicationContext
情形下有效。
|
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配置文件中配置如下所示:
<bean id="personService" class="examples.test.PersonServiceBean" ></bean>
测试代码如下所示:
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 id=”xxx” class=”examples.test.OrderServiceBean” lazy-init=”true” />
如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=”true”,如下所示:
<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!
- 大小: 64.9 KB
- 大小: 65.9 KB
分享到:
相关推荐
主要介绍了SPRING FRAMEWORK BEAN作用域和生命周期原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了浅谈Spring中Bean的作用域、生命周期,具有一定借鉴价值,需要的朋友可以参考下
主要给大家介绍了Spring中Bean的生命周期和作用域及实现方式的相关资料,文中介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
主要介绍了深入了解Spring中Bean的作用域和生命周期,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
NULL 博文链接:https://cici9898-007.iteye.com/blog/1407695
spring框架技术+第2天+xmind思维导图:生命周期,介绍simple project,打印出构造方法...bean作用域request session globalSession:web项目获取核心配置文件要配置两个地方:spring监听器、spring作用域范围的监听。
spring的生命周期及BeanPostProcessor的使用,注解方式创建bean 及使用Autowried标签关联类的属性 ,泛型依赖注入的使用 spring-3 演示使用动态代理模式实现面向切面编程 使用注解方式进行AOP编程及使用配置xml方式...
本篇博文适合零基础的同学:主要包括:spring介绍; Ioc 入门案例; Di 入门案例; 核心API介绍;...bean作用域; 生命周期; 属性注入--setter方法 p命名空间; sqel; 集合注入; 装配bean基于注解;
代码及ppt涵盖 Spring4.0 的所有核心内容:在 Eclipse 中安装 SpringIDE 插件、IOC & DI、在 Spring 中配置 Bean、自动装配、Bean 之间的关系(依赖、继承)、Bean 的作用域、使用外部属性文件、SpEL、管理 Bean 的...
摘录了spring的核心逻辑,代码极度简化,保留spring的核心功能,如IoC和AOP,资源加载器,事件监听器,类型转换,容器扩展点,bean生命周期和作用域,应用某些等核心功能。 如果本项目能帮助到你,请给个STAR,谢谢...
1.spring中bean实例化的俩种方式 2.spring中bean的各种元素如何使用 ...4.spring中不同类型bean的作用域 5.spring中bean的生命周期 通过我的项目,可以跟着学习SPringIoC容器和Bean管理,帮助你快速上手SSM框架
提取了Spring的核心逻辑,极大地简化了代码,并完善了Spring的核心功能,例如IoC和AOP,资源加载器,事件侦听器,类型转换,容器扩展点,Bean生命周期和作用域以及应用程序上下文,被保留。 如果这个项目可以为您...
Spring Bean的生命周期? Spring中bean的作用域 说一下Spring基于xml注入bean的几种方式? Spring如何解决循环依赖问题? Spring的自动装配 Spring框架中都用到了哪些设计模式? Spring框架中有哪些不同类型的事件?
我的博客中“maven环境搭建及...bean的作用域 * 单例 scope=“singleton” * 原型 scope=“prototype” 5。bean的生命周期: 默认情况下IOC容器创建的时候就会自动创建所有对象,只支持懒加载。default-lazy-init=true
BeanDefinition则详细描述了bean的配置信息,如类名、作用域、依赖等,为Bean的创建和管理提供了蓝图。 除此之外,文档还深入介绍了Spring的事务管理能力,特别是DataSourceTransactionManager,它为数据库操作提供...
请解释Spring Bean的生命周期 Spring Bean的作用域之间有什么区别 什么是Spring inner beans Spring框架中的单例Beans是线程安全的么 请举例说明如何在Spring中注入一个Java Collection 如何向Spring Bean中注入一个...
在手写Spring 源码的过程中会摘取整体框架中的核心逻辑,简化代码实现过程,保留核心功能,例如:IOC、AOP、Bean生命周期、上下文、作用域、资源处理等内容实现。 适合人群:具备一定编程基础,工作1-3年的研发人员 ...
Spring中注入属性值细节、自动装配、bean之间的关系、bean的作用域、使用外部属性文件、SpEL、IOC容器中Bean 的生命周期、泛型依赖注入。
毕业设计,java语言,基于spring+springMvc+mybatis ...在手写Spring 源码的过程中会摘取整体框架中的核心逻辑,简化代码实现过程保留核心功能,例如:I0C、AOP、Bean生命周期、上下文、作用域、资源处理等内容实现。
Hibernate: 第一课:第一个hibernate项目 第二课:测试实体对象的生命周期 ...第三课:spring Bean的作用域 第四课:spring对AOP的只是(采用Annotation的方式) 第五课:spring对AOP的只是(采用配置文件的方式)