`

Spring4.X之Bean的Scope

    博客分类:
  • S2SH
阅读更多

https://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch04s04.html

Bean scopes


1、singleton
默认范围。
        一个 Spring IoC 容器中只有一个 bean.
        注意:这与单例设计模式不是一个概念。

用法:
<bean id="s" class="x.x.XService"/>

<!-- the following is equivalent, though redundant (singleton scope is the default); using spring-beans-2.0.dtd -->
<bean id="s" class="x.x.XService" scope="singleton"/>

<!-- the following is equivalent and preserved for backward compatibility in spring-beans.dtd -->
<bean id="s" class="x.x.XService" singleton="true"/>



2、prototype
        原型模式:
        就像是使用 Java 类一样,可以产生任意多个bean的实例。
       

用法:
<!-- using spring-beans-2.0.dtd -->
<bean id="s" class="x.x.XService" scope="prototype"/>

<!-- the following is equivalent and preserved for backward compatibility in spring-beans.dtd -->
<bean id="s" class="x.x.XService" singleton="false"/>




下面三种只在 Web 容器中可用

1、request
Scopes a single bean definition to the life cycle of a single HTTP request;
that is each and every HTTP request will have its own instance of a bean created
off the back of a single bean definition.
- Only valid in the context of a web-aware Spring ApplicationContext.

用法:
<bean id="s" class="x.x.XService" scope="request"/>



2、session
Scopes a single bean definition to the life cycle of a HTTP Session.
- Only valid in the context of a web-aware Spring ApplicationContext.
用法:
<bean id="s" class="x.x.XService" scope="session"/>




3、global session
Scopes a single bean definition to the life cycle of a global HTTP Session.
Typically only valid when used in a port-let context.
- Only valid in the context of a web-aware Spring ApplicationContext.

用法:
<bean id="s" class="x.x.XService" scope="globalSession"/>

















-
分享到:
评论

相关推荐

    spring4.x-project:spring4.x系列整理总结

    spring4.x-project spring4.x整理总结样例,在每篇文章后面有对应源代码地址...Spring4.x常用配置(一):Bean的Scope Github项目地址:spring4.x-scope 码云项目地址:spring4.x-scope 文章地址:Spring4.x常用配置(一):

    spring bean 的作用域(scope)

    spring bean 的作用域(scope), SPringle bean的作用域

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    4. Integrating Spring Chapter 15. Working with remote services 15.1. An overview of Spring remoting 15.2. Working with RMI 15.2.1. Exporting an RMI service 15.2.2. Wiring an RMI service 15.3. Exposing...

    spring boot 项目代码,直接启动本人编写的,里面包含PPT下载地址

    spring boot 项目代码,直接启动,第一部分 点睛Spring 4.x 第1 章 Spring 基础 ..........................................2 1.1 Spring 概述 ............................................. 2 1.2 Spring 项目...

    JavaEE开发的颠覆者 Spring Boot实战,多个地址免费下载,

    JavaEE开发的颠覆者 Spring Boot实战,多个地址免费下载,第一部分 点睛Spring 4.x 第1 章 Spring 基础 ..........................................2 1.1 Spring 概述 ................................................

    spring-framework-reference-4.1.2

    What’s New in Spring Framework 4.x .................................................................................... 16 3. New Features and Enhancements in Spring Framework 4.0 .....................

    详解Spring中bean的scope以后使用

    详解Spring中bean的scope以后使用,如何使用spring的作用域:

    spring-framework-reference4.1.4

    What’s New in Spring Framework 4.x .................................................................................... 16 3. New Features and Enhancements in Spring Framework 4.0 .....................

    Spring In Action-3.2@Scope单例、多例Bean

    Spring In Action-3.2@Scope单例、多例Bean,Spring In Action-3.2@Scope单例、多例Bean,Spring In Action-3.2@Scope单例、多例Bean,Spring In Action-3.2@Scope单例、多例Bean

    springjdbc

    &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /&gt; &lt;!-- apache.dbcp连接池的配置 --&gt; &lt;bean id="dataSource" class="org.apache.commons.dbcp....

    JavaEE开发的颠覆者+Spring+Boot实战++完整版

    出版社名称: 电子工业出版社 作者: 汪云飞 书名: JavaEE开发的颠覆者 Spring Boot实战 ISBN编号: 9787121282089 第一部分 点睛Spring 4.x 第1 章 Spring 基础 ..........................................2 1.1 ...

    spring-boot-reference.pdf

    20.2.4. Disabling Restart 20.2.5. Using a Trigger File 20.2.6. Customizing the Restart Classloader 20.2.7. Known Limitations 20.3. LiveReload 20.4. Global Settings 20.5. Remote Applications 20.5.1. ...

    Spring之scope讲解测试示例代码

    我的博客的spring之bean的Scope 的 示例代码。 包含了一个web工程(包括lib包)。但是里面也有可以执行的main函数。 环境是eclipse,spring 是 4.1.5

    创建SpringBean配置工具类

    创建SpringBean配置工具类(安全)如: &lt;bean id=... scope="prototype"&gt;&lt;/bean&gt;

    Struts2+Spring3+MyBatis3完整实例

    网上的东西好大多都不能直接用,自己结合网上资料做了一个Struts2+Spring3+MyBatis3的测试工程,JUnit测试用例和WEB服务。 内涵完整jar包,解压直接可用,包括一个表文件。 Eclipse3.2+Tomcat/5.5+jdk1.5.0_17 - ...

    吴天雄--Spring笔记.doc

    第一天内容:Spring框架简介(EJB、JMX、Spring核心功能、Spring模块详解、Spring重要概念(容器)、Spring容器初始化的整个流程、Spring后处理器),IOC详解,Spring环境搭建,Spring创建Bean的三种方式,scope属性...

    SPRING API 2.0.CHM

    RequestScope RequestToViewNameTranslator RequestUtils Required RequiredAnnotationBeanPostProcessor Resource ResourceAllocationException ResourceArrayPropertyEditor ResourceBundleEditor ...

    基于java的企业级应用开发:Bean的作用域.ppt

    例如: &lt;bean id="scope" class="com.itheima.scope.Scope" scope="singleton"/&gt; 在Spring中如何配置singleton作用域? 在Spring配置文件中,同样使用&lt;bean&gt;元素的scope属性,将Bean的作用域定义成prototype 。 ...

    Spring.html

    ClassPathXmlApplicationContext:使用这个工厂创建对象,他会根据scope智能判断是否懒加载,如果是单例则创建容器时就会创建里面bean的实例,如果是多例在获取使用时才会创建bean实例 ...

Global site tag (gtag.js) - Google Analytics