`
ponlya
  • 浏览: 159757 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Spring3之 bean 作用域scope

阅读更多

Bean scopes 作用域

5:singleton,prototype,request,session,global session

singleton 是默认的作用域,容器只产生一个实例,只要调用的id相同,返回的实例就是同一个;

prototype 每次调用返回的是不同的实例;

Request, session, and global session scopes 这三个是WEB方面使用的,使用基于webSpring ApplicationContext实现(XmlWebApplicationContext

前二个

com.spring305.test.scope.po.SingletonScope.java

public class SingletonScope {

	private int id = (int) (100 * Math.random());

	public void printID() {
		System.out.println(SingletonScope.class + " id=" + id);
	}

}

 com.spring305.test.scope.po.PrototypeScope.java

 

public class PrototypeScope {
	private int id = (int) (100 * Math.random());

	public void printID() {
		System.out.println(PrototypeScope.class + " id=" + id);
	}
}

 

src/testScope.xml

<bean id="singletonScope" class="com.spring305.test.scope.po.SingletonScope"></bean>
<bean id="prototypeScope" class="com.spring305.test.scope.po.PrototypeScope" scope="prototype"></bean>

 

测试:

@Test
public  void testSingletonProtoType(){
	ApplicationContext ctx = new ClassPathXmlApplicationContext("testScope.xml");
	SingletonScope singletonScope1 = ctx.getBean("singletonScope",SingletonScope.class);
	SingletonScope singletonScope2 = ctx.getBean("singletonScope",SingletonScope.class);
	PrototypeScope prototypeScope1 = ctx.getBean("prototypeScope",PrototypeScope.class);
	PrototypeScope prototypeScope2 = ctx.getBean("prototypeScope",PrototypeScope.class);
	singletonScope1.printID();
	singletonScope2.printID();
		
	prototypeScope1.printID();
	prototypeScope2.printID();
}

 

得到结果:

class com.spring305.test.scope.po.SingletonScope id=89
class com.spring305.test.scope.po.SingletonScope id=89
class com.spring305.test.scope.po.PrototypeScope id=12
class com.spring305.test.scope.po.PrototypeScope id=50

 singleton的值没有变化,而prototype的变化

 

再看后三种:

web.xml中加入spring Listener

<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<!-- classpath:applicationContext.xml -->
		<param-value>/WEB-INF/testScope.xml</param-value>
	</context-param>

	<listener>
		<listener-class>
			org.springframework.web.context.request.RequestContextListener
		</listener-class>
	</listener>

	<servlet>
		<servlet-name>ScopeTest</servlet-name>
		<servlet-class>
			com.spring305.test.scope.po.ScopeTest
		</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>ScopeTest</servlet-name>
		<url-pattern>/ScopeTest</url-pattern>
	</servlet-mapping>

 /WEB-INF/testScope.xml

<bean id="singletonScope" class="com.spring305.test.scope.po.SingletonScope"></bean>
<bean id="prototypeScope" class="com.spring305.test.scope.po.PrototypeScope" scope="prototype"></bean>
<bean id="requestScopeT" class="com.spring305.test.scope.po.RequestScope" scope="request"></bean>
<bean id="sessionScopeT" class="com.spring305.test.scope.po.SessionScope" scope="session"></bean>
<bean id="globalSessionScope" class="com.spring305.test.scope.po.GlobalSessionScope" scope="globalSession"></bean>

 servlet doGet内容如下:

public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

                response.setContentType("text/html");
	PrintWriter out = response.getWriter();
	System.out.println("in get");
	ServletContext context = getServletContext();

		ApplicationContext   ctx   =   null; 
        if   (ctx   ==   null)       
        {       
        	ctx   =   WebApplicationContextUtils.getRequiredWebApplicationContext(context);       
        }       
       
        /**
         *     SingletonScope singletonScope1 = ctx.getBean("singletonScope",SingletonScope.class);
                SingletonScope singletonScope2 = ctx.getBean("singletonScope",SingletonScope.class);
	PrototypeScope prototypeScope1 = ctx.getBean("prototypeScope",PrototypeScope.class);
	PrototypeScope prototypeScope2 = ctx.getBean("prototypeScope",PrototypeScope.class);
	singletonScope1.printID();
	singletonScope2.printID();
		
	prototypeScope1.printID();
	prototypeScope2.printID();
         */
        
	RequestScope requestScope = (RequestScope) ctx.getBean("requestScopeT");
	requestScope.printID();

	SessionScope sessionScope = (SessionScope) ctx.getBean("sessionScopeT");
	sessionScope.printID();
	GlobalSessionScope gScope = (GlobalSessionScope)ctx.getBean("globalSessionScope",GlobalSessionScope.class);
	gScope.printID();
		
	out.println("123");
	out.flush();
	out.close();
}

 同一浏览器刷新二次得到结果:

in get
class com.spring305.test.scope.po.RequestScope id=11
class com.spring305.test.scope.po.SessionScope id=50
class com.spring305.test.scope.po.GlobalSessionScope id=70
in get
class com.spring305.test.scope.po.RequestScope id=92
class com.spring305.test.scope.po.SessionScope id=50
class com.spring305.test.scope.po.GlobalSessionScope id=70

 可见,除了requet的值变化了,session,globalsession的都没有变

换个浏览器刷新下servlet

in get
class com.spring305.test.scope.po.RequestScope id=96
class com.spring305.test.scope.po.SessionScope id=89
class com.spring305.test.scope.po.GlobalSessionScope id=38

 相对而言,值都变化了,那么globalsession为什么也会变?官方给出解释:

The global session scope is similar to the standard HTTP Session scope (described above), and
applies only in the context of portlet-based web applications

 基于portlet的web应用中才有意义.

 

<!--EndFragment-->
分享到:
评论

相关推荐

    spring bean 的作用域(scope)

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

    spring Bean的作用域之间有什么区别1

    Spring Bean 的作用域之间有什么区别:Bean的作用域: 可以通过scope 属性来指定bean的作用域 ①singleton: 默认值。当IOC容器

    JSP 中Spring Bean 的作用域详解

    Bean元素有一个scope属性,用于定义Bean的作用域,该属性有如下五个值: 1&gt;singleton: 单例模式,在整个spring IOC容器中,单例模式作用域的Bean都将只生成一个实例。一般Spring容器默认Bean的作用域为singleton ...

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

    * * * * Bean的作用域 作用域的种类 Spring 4.3中为Bean的实例定义了7种作用域,如下表所示: 注意:在上表7种作用域中,singleton和prototype是最常用的两种作用域。 在Spring配置文件中,可以使用&lt;bean&gt;元素的...

    详解Spring中bean的scope以后使用

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

    SSH笔记-bean的作用域

    SSH笔记-bean的作用域,通过在配置文件的bean标签中使用scope属性来实现作用域的配置

    Spring中 Configuration的使用.docx

    (2)、@Bean注解默认作用域为单例singleton作用域,可通过@Scope(“prototype”)设置为原型作用域; (3)、既然@Bean的作用是注册bean对象,那么完全可以使用@Component、@Controller、@Service、@Ripository等注解...

    spring学习笔记(有代码有注解解释)

    scope 作用域;Spring 的继承;Spring 的依赖;Spring 的 p 命名空间;Spring 的工厂方法;IoC 自动装载(Autowire);AOP以及如何使用; 适用人群:比较适合与我一样的在校普通大学生进行学习整理,以及适合初学...

    HelloSpring.zip

    我的博客中“maven环境搭建及...bean的作用域 * 单例 scope=“singleton” * 原型 scope=“prototype” 5。bean的生命周期: 默认情况下IOC容器创建的时候就会自动创建所有对象,只支持懒加载。default-lazy-init=true

    spring.doc

    Scope单例多例作用域拓展: 16 3.4.1 singleton(默认值) 16 3.4.2 prototype 17 3.4.3 Request 17 3.4.4 Session 18 3.4.5 Global session 18 3.4.6 指定Bean的初始化方法和销毁方法 18 Bean的初始化和销毁拓展: 18...

    spring学习过程

    spring基础,主要讲解了 spring的autowire:自动装配 collections:属性配置的细节 properties:外部属性文件的使用 relati:bean之间的关系 scope:bean的作用域 spel:spel的使用

    spring-training:Spring Framework简介和示例

    Spring培训 1. Hello World(helloworld) 基本的hello world应用程序,具有基于XML的配置。 2.策略模式(策略) Spring依赖注入与基于XML的配置...这将bean定义的作用域限定为HTTP会话。 仅在可感知网络的Spring Ap

    Spring3ProjectScopePrototype

    Spring3ProjectScopePrototype Spring 3 + IOC(核心容器)+ Scope(原型),示例由于作用域是原型,因此在这种情况下,如果您多次调用getBean方法,则容器仅将多个对象引用发送到JVM,并会创建多个对象使用...

    SpringMVC-Mybatis-Shiro-redis-master 权限集成缓存中实例

    上面配置是去掉了 Session 的存储Key 的作用域,之前设置的.itboy.net ,是写到当前域名的 一级域名 下,这样就可以做到N 个 二级域名 下,三级、四级....下 Session 都是共享的。 &lt;!-- 用户信息记住我功能的...

    JBoss Seam 工作原理、seam和hibernate的范例、RESTFul的seam、seam-gen起步、seam组件、配置组件、jsf,jboss、标签、PDF、注解等等

    Seam - 语境相关的组件[满江红20071230]............................................................................................................................ 1 Java EE 框架...........................

Global site tag (gtag.js) - Google Analytics