- 浏览: 159757 次
- 性别:
- 来自: 武汉
文章分类
- 全部博客 (116)
- 随笔 (2)
- spring (24)
- struts (1)
- hibernate (6)
- log4j (0)
- mysql (14)
- oracle (0)
- ext (0)
- jQuery (0)
- HTML+CSS (2)
- Javascript (1)
- 其它杂碎 (0)
- IT (3)
- J2EE综合 (15)
- SQLServer (1)
- 好文章URL 待阅读 (3)
- 编辑器 (2)
- 版本控制 (5)
- Tomcat (4)
- DoJo (0)
- Ubuntu (11)
- Hadoop (3)
- cxf (3)
- maven (6)
- CI (5)
- H2 (1)
- JVM (1)
- FirefoxOS (1)
- Jboss (1)
- 操作系统 (1)
- C3P0 (2)
- Quartz (1)
- maps (10)
- 设计模式 (5)
最新评论
-
yogurt2012:
请问··我如果要调试H2数据库来分析其JOIN算法应该怎么做呢 ...
H2笔记 -
carlosfu:
很好很全,很有收获
Spring3笔记之 JDBC -
ponlya:
coldrush 写道看了你的配置 ,刚绝 file:后加绝对 ...
添加使用dtd文件 -
ponlya:
byp19980911 写道这不是很好的解决办法,最好是使用连 ...
java.net.SocketException:Software caused connection abort: socket write error -
ponlya:
ayanami001 写道为什么spring没有封装分页吗,那 ...
Spring3笔记之 JDBC(分页)
Bean scopes 作用域
5个:singleton,prototype,request,session,global session
singleton 是默认的作用域,容器只产生一个实例,只要调用的id相同,返回的实例就是同一个;
prototype 每次调用返回的是不同的实例;
Request, session, and global session scopes 这三个是WEB方面使用的,使用于基于web的Spring ApplicationContext实现(如XmlWebApplicationContext)
前二个
com.spring305.test.scope.po.SingletonScope.java
com.spring305.test.scope.po.PrototypeScope.javapublic class SingletonScope {
private int id = (int) (100 * Math.random());
public void printID() {
System.out.println(SingletonScope.class + " id=" + id);
}
}
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整合Hibernate(摘录)
2011-05-07 09:48 710简要步骤From:http://blog.csdn.net/s ... -
Spring3笔记之 JDBC(分页)
2011-04-25 22:08 1987接上篇的实体,DAO接口,实现,测试及XML http://p ... -
Spring3笔记之 JDBC
2011-04-25 22:02 7760使用Spring 的JDBC 先创建表: DROP TAB ... -
Spring3笔记之 事务传播
2011-04-24 15:51 1180摘自:http://zhxing.iteye.com/blog ... -
Spring3笔记之 AOP
2011-04-24 14:24 1859Spring AOP部分使用JDK动态代理或者CGLIB来为目 ... -
Spring3笔记之 AOP Cglib 代理
2011-04-24 14:13 1326JDK的Proxy实现代理要求 ... -
Spring3笔记之 AOP JDK 代理
2011-04-24 14:09 1008使用JDK代理,代理对象必须实现一接口 com.spring ... -
Spring3之 Resource
2011-04-21 22:49 3944主要是org.springframework.core.io. ... -
Spring3之 bean 注解
2011-04-20 22:18 1474小记下,spring注解相关: com.spring305. ... -
Spring3之 bean 定制bean特性
2011-04-19 21:10 1260Customizing the nature of a bea ... -
Spring3之 bean Method injection
2011-04-17 20:04 1434Method injection(方法注入) bean都是s ... -
Spring3之 bean AutoWire
2011-04-17 12:01 5895Autowiring collaborators 自动装配 ... -
Spring3之 bean depends-on
2011-04-17 08:56 3226depends-on depend-on用来表 ... -
Spring3之 bean Lazy-initialized beans
2011-04-17 08:48 1938Lazy-initialized beans延迟 ... -
Spring3之 集合对象属性注入
2011-04-16 23:17 1181com.spring305.test.beanInit.cpo ... -
Spring3之 bean idref?
2011-04-16 18:39 916很是奇怪idref是干什么 ... -
Spring3之 bean简单属性注入
2011-04-16 17:40 1315DI (依赖注入)有二种: Constructor-based ... -
Spring3之 实例化bean2
2011-04-16 14:30 981Spring3之 bean命名(http://ponlya.i ... -
Spring3之 bean命名
2011-04-16 11:36 1971一、bean的命名采用标准Java命名约定:小写字母开头,首字 ... -
Spring3之 IoC容器的实例化
2011-04-16 08:39 1474Bean:在Spring中,那些组成你应用程序的主体(ba ...
相关推荐
spring bean 的作用域(scope), SPringle bean的作用域
Spring Bean 的作用域之间有什么区别:Bean的作用域: 可以通过scope 属性来指定bean的作用域 ①singleton: 默认值。当IOC容器
Bean元素有一个scope属性,用于定义Bean的作用域,该属性有如下五个值: 1>singleton: 单例模式,在整个spring IOC容器中,单例模式作用域的Bean都将只生成一个实例。一般Spring容器默认Bean的作用域为singleton ...
* * * * Bean的作用域 作用域的种类 Spring 4.3中为Bean的实例定义了7种作用域,如下表所示: 注意:在上表7种作用域中,singleton和prototype是最常用的两种作用域。 在Spring配置文件中,可以使用<bean>元素的...
详解Spring中bean的scope以后使用,如何使用spring的作用域:
SSH笔记-bean的作用域,通过在配置文件的bean标签中使用scope属性来实现作用域的配置
(2)、@Bean注解默认作用域为单例singleton作用域,可通过@Scope(“prototype”)设置为原型作用域; (3)、既然@Bean的作用是注册bean对象,那么完全可以使用@Component、@Controller、@Service、@Ripository等注解...
scope 作用域;Spring 的继承;Spring 的依赖;Spring 的 p 命名空间;Spring 的工厂方法;IoC 自动装载(Autowire);AOP以及如何使用; 适用人群:比较适合与我一样的在校普通大学生进行学习整理,以及适合初学...
我的博客中“maven环境搭建及...bean的作用域 * 单例 scope=“singleton” * 原型 scope=“prototype” 5。bean的生命周期: 默认情况下IOC容器创建的时候就会自动创建所有对象,只支持懒加载。default-lazy-init=true
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的autowire:自动装配 collections:属性配置的细节 properties:外部属性文件的使用 relati:bean之间的关系 scope:bean的作用域 spel:spel的使用
Spring培训 1. Hello World(helloworld) 基本的hello world应用程序,具有基于XML的配置。 2.策略模式(策略) Spring依赖注入与基于XML的配置...这将bean定义的作用域限定为HTTP会话。 仅在可感知网络的Spring Ap
Spring3ProjectScopePrototype Spring 3 + IOC(核心容器)+ Scope(原型),示例由于作用域是原型,因此在这种情况下,如果您多次调用getBean方法,则容器仅将多个对象引用发送到JVM,并会创建多个对象使用...
上面配置是去掉了 Session 的存储Key 的作用域,之前设置的.itboy.net ,是写到当前域名的 一级域名 下,这样就可以做到N 个 二级域名 下,三级、四级....下 Session 都是共享的。 <!-- 用户信息记住我功能的...
Seam - 语境相关的组件[满江红20071230]............................................................................................................................ 1 Java EE 框架...........................