Spring学习笔记1
2013-02-18 00:00
246 查看
you should use the prototype scope for all beans that are stateful, while the singleton scope should be used for stateless beans.
The singleton scope is the default scope in Spring.
后向兼容:
ApplicationContext实现的默认行为就是在启动时将所有singleton bean提前进行实例化。提前实例化意味着作为初始化过程的一部分,ApplicationContext实例会创建并配置所有的singleton bean。通常情况下这是件好事,因为这样在配置中的任何错误就会即刻被发现(否则的话可能要花几个小时甚至几天)。
有时候这种默认处理可能并不是你想要的。如果你不想让一个singleton bean在ApplicationContext实现在初始化时被提前实例化,那么可以将bean设置为延迟实例化。一个延迟初始化bean将告诉IoC 容器是在启动时还是在第一次被用到时实例化。
在XML配置文件中,延迟初始化将通过<bean/>元素中的lazy-init属性来进行控制。例如:
当ApplicationContext实现加载上述配置时,设置为lazy的bean将不会在ApplicationContext启动时提前被实例化,而not.lazy却会被提前实例化。
需要说明的是,如果一个bean被设置为延迟初始化,而另一个非延迟初始化的singleton bean依赖于它,那么当ApplicationContext提前实例化singleton bean时,它必须也确保所有上述singleton 依赖bean也被预先初始化,当然也包括设置为延迟实例化的bean。因此,如果Ioc容器在启动的时候创建了那些设置为延迟实例化的bean的实例,你也不要觉得奇怪,因为那些延迟初始化的bean可能在配置的某个地方被注入到了一个非延迟初始化singleton bean里面。
在容器层次中通过在<beans/>元素上使用'default-lazy-init'属性来控制延迟初始化也是可能的。如下面的配置:
还看到bean的组装,autowire="no" 是默认的格式,也可以使用混合的模式,即显示指定,也可以使用autowire=四种类型的一种,实践中,等于no是最佳的,清晰明了。
文档参考: http://mojianpo.iteye.com/blog/803707 http://static.springsource.org/spring/docs/2.0.x/reference/beans.html#beans-factory-lazy-init http://static.springsource.org/spring/docs/2.5.3/reference/beans.html#beans-factory-scopes
The singleton scope is the default scope in Spring.
<!-- the following is equivalent, though redundant (singleton scope is the default); using spring-beans-2.0.dtd --> <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"/>
ApplicationContext实现的默认行为就是在启动时将所有singleton bean提前进行实例化。提前实例化意味着作为初始化过程的一部分,ApplicationContext实例会创建并配置所有的singleton bean。通常情况下这是件好事,因为这样在配置中的任何错误就会即刻被发现(否则的话可能要花几个小时甚至几天)。
有时候这种默认处理可能并不是你想要的。如果你不想让一个singleton bean在ApplicationContext实现在初始化时被提前实例化,那么可以将bean设置为延迟实例化。一个延迟初始化bean将告诉IoC 容器是在启动时还是在第一次被用到时实例化。
在XML配置文件中,延迟初始化将通过<bean/>元素中的lazy-init属性来进行控制。例如:
<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"> <!-- various properties here... --> </bean> <bean name="not.lazy" class="com.foo.AnotherBean"> <!-- various properties here... --> </bean>
当ApplicationContext实现加载上述配置时,设置为lazy的bean将不会在ApplicationContext启动时提前被实例化,而not.lazy却会被提前实例化。
需要说明的是,如果一个bean被设置为延迟初始化,而另一个非延迟初始化的singleton bean依赖于它,那么当ApplicationContext提前实例化singleton bean时,它必须也确保所有上述singleton 依赖bean也被预先初始化,当然也包括设置为延迟实例化的bean。因此,如果Ioc容器在启动的时候创建了那些设置为延迟实例化的bean的实例,你也不要觉得奇怪,因为那些延迟初始化的bean可能在配置的某个地方被注入到了一个非延迟初始化singleton bean里面。
在容器层次中通过在<beans/>元素上使用'default-lazy-init'属性来控制延迟初始化也是可能的。如下面的配置:
<beans default-lazy-init="true"> <!-- no beans will be eagerly pre-instantiated... --> </beans>
还看到bean的组装,autowire="no" 是默认的格式,也可以使用混合的模式,即显示指定,也可以使用autowire=四种类型的一种,实践中,等于no是最佳的,清晰明了。
文档参考: http://mojianpo.iteye.com/blog/803707 http://static.springsource.org/spring/docs/2.0.x/reference/beans.html#beans-factory-lazy-init http://static.springsource.org/spring/docs/2.5.3/reference/beans.html#beans-factory-scopes
相关文章推荐
- Beginning Spring学习笔记——第4章(三)将JDBC操作模块化为Java对象
- spring ioc容器的学习笔记2----ioc容器设计
- Spring学习笔记2
- spring学习笔记八 bean生命周期
- osgi学习笔记3之集成Spring-DM的cxf-DOSGI HelloWorld
- Spring Web MVC框架学习笔记
- Beginning Spring学习笔记——第6章(一)Spring事务管理基础
- 《精通Spring 4.x 企业应用开发实战》学习笔记
- Spring 学习笔记【一】自动装配bean
- spring学习笔记(二)
- Spring学习笔记--IoC
- 学习笔记-----------------struts2 hibernate3 spring2.5整合
- Spring学习笔记之整合hibernate
- spring学习笔记 —— spring概述
- Spring 学习笔记2 Spring开发环境搭建
- Spring in Action 学习笔记—第七章访问企业服务
- 学习Spring笔记五
- Spring学习笔记(三)Spring注解方式实现AOP
- spring源码学习笔记-初始化(一)-概览
- Spring入门篇学习笔记(3)