spring bean scope="prototype" 有状态bean 无状态bean
2012-09-24 08:55
393 查看
在Spring的Bean配置中,存在这样两种情况:
[xhtml]
view plaincopyprint?
<bean id="testManager" class="com.sw.TestManagerImpl" scope="singleton" />
<bean id="testManager" class="com.sw.TestManagerImpl" scope="prototype" />
如果该Bean配置为singleton,会出现什么样的状况呢?
如果有2个用户访问,都调用到了该Bean.
假定为user1,user2
当user1 调用到程序中的1步骤的时候,该Bean的私有变量user被付值为user1
当user1的程序走到2步骤的时候,该Bean的私有变量user被重新付值为user1_create
理想的状况,当user1走到3步骤的时候,私有变量user应该为user1_create;
但如果在user1调用到3步骤之前,user2开始运行到了1步骤了,由于单态的资源共享,则私有变量user被修改为user2
这种情况下,user1的步骤3用到的user.getId()实际用到是user2的对象。
而如果是prototype的话,就不会出现资源共享的问题。
对于SSH来说,Bean的配置是没错的,配置为singleton ;实际应该是这个例子不应该用私有变量。这样就使得这个Bean
由无状态变成了有状态Bean.还是应该尽量使用无状态Bean.如果在程序中出现私有变量,尽量替换为参数。
对于每个访问私有变量的方法增加变量传入或者通过ThreadLocal来获取也是不错的方法。
真正出现上面代码问题的也是少数,出现的时候,一般是为了图方便,一个很多方法都要用到的变量,如果都需要用参数的
方式传递多麻烦呀,这样私有变量多好,不用参数那样丑陋。但是丑陋并不代表不好,以对的,自己习惯的方式编程,才能
尽量避免问题的发生。
[xhtml]
view plaincopyprint?
<bean id="testManager" class="com.sw.TestManagerImpl" scope="singleton" />
<bean id="testManager" class="com.sw.TestManagerImpl" scope="prototype" />
[java] view plaincopyprint? package com.sw; public class TestManagerImpl implements TestManager{ private User user; public void deleteUser(User e) throws Exception { user = e ; //1 prepareData(e); } public void prepareData(User e) throws Exception { user = getUserByID(e.getId()); //2 ..... //使用user.getId(); //3 ..... ..... } } package com.sw; public class TestManagerImpl implements TestManager{ private User user; public void deleteUser(User e) throws Exception { user = e ; //1 prepareData(e); } public void prepareData(User e) throws Exception { user = getUserByID(e.getId()); //2 ..... //使用user.getId(); //3 ..... ..... } }
如果该Bean配置为singleton,会出现什么样的状况呢?
如果有2个用户访问,都调用到了该Bean.
假定为user1,user2
当user1 调用到程序中的1步骤的时候,该Bean的私有变量user被付值为user1
当user1的程序走到2步骤的时候,该Bean的私有变量user被重新付值为user1_create
理想的状况,当user1走到3步骤的时候,私有变量user应该为user1_create;
但如果在user1调用到3步骤之前,user2开始运行到了1步骤了,由于单态的资源共享,则私有变量user被修改为user2
这种情况下,user1的步骤3用到的user.getId()实际用到是user2的对象。
而如果是prototype的话,就不会出现资源共享的问题。
对于SSH来说,Bean的配置是没错的,配置为singleton ;实际应该是这个例子不应该用私有变量。这样就使得这个Bean
由无状态变成了有状态Bean.还是应该尽量使用无状态Bean.如果在程序中出现私有变量,尽量替换为参数。
对于每个访问私有变量的方法增加变量传入或者通过ThreadLocal来获取也是不错的方法。
真正出现上面代码问题的也是少数,出现的时候,一般是为了图方便,一个很多方法都要用到的变量,如果都需要用参数的
方式传递多麻烦呀,这样私有变量多好,不用参数那样丑陋。但是丑陋并不代表不好,以对的,自己习惯的方式编程,才能
尽量避免问题的发生。
相关文章推荐
- spring <bean scope="prototype">探讨
- Spring配置<bean>时加scope="prototype"的原因
- Spring注解@Scope("prototype")
- struts2.0中spring配置文件中action的属性 scope="prototype"
- spring scope="prototype" 和scope="singleton"区分
- spring <bean scope="prototype">探讨
- spring注解中@Scope("prototype")的重要性
- struts+spring action应配置为scope="prototype"
- Spring注解@Scope("prototype") Spring在Action上面注解@Scope("prototype")
- sping+struts2中配置Action的bean中scope="prototype"的作用
- Spring的scope="prototype"属性
- 关于Spring的destroy-method和scope="prototype"不能共存问题
- spring scope="prototype" 和scope="singleton"区分
- Struts2+hibernate+spring 配置文件中scope="prototype"的作用
- spring scope="prototype" 学习笔记
- struts+spring action应配置为scope="prototype"
- Spring 注解 @Scope("prototype") 的作用
- sping+struts2中配置Action的bean中scope="prototype"的作用
- Attribute "scope" must be declared for element type "bean"-[spring]
- spring scope="prototype" 和scope="singleton"区分