struts2+spring中action的生命周期问题
2013-03-05 15:15
162 查看
将struts2中的action交给spring管理
如果没有指定scope就会出现验证时第一次验证的结果一直存在,导致后面的验证根本没有进行
所以在spring中配置action时要指定scope属性为prototype
<bean id="randomImageAction"
class="cn.link.sgums.action.RandomImageAction" scope="prototype">
这样就ok了
默认的spring的bean的周期是单态的(Singleton)
对于每一次请求不会生成新的实例
Spring里默认情况下,用BeanFactory和ApplicationContext获得的bean实例都是一个。看下面这个例子:
Bean的定义:
测试:
输出结果为: jack jack
由此可以看出在Spring容器中,默认情况下,每一个bean只有一个实例。
我们对bean的配置文件做一些修改: 加上了singleton="false" 这个配置,再执行上面的测试,结果为: jack Tom 这时每次去获得helloBean的实例,就是一个新的实例了。(singleton默认是等于true)。
对于Spring2.0,上面的配置可以改成:scope = "prototype" , scope的默认预设值singleton,针对Web应用,scope的值还可以设置为“request”,"session","globalSession"分别对应web的请求阶段,会话阶段,web应用程序阶段。
如果没有指定scope就会出现验证时第一次验证的结果一直存在,导致后面的验证根本没有进行
所以在spring中配置action时要指定scope属性为prototype
<bean id="randomImageAction"
class="cn.link.sgums.action.RandomImageAction" scope="prototype">
这样就ok了
默认的spring的bean的周期是单态的(Singleton)
对于每一次请求不会生成新的实例
Spring里默认情况下,用BeanFactory和ApplicationContext获得的bean实例都是一个。看下面这个例子:
public class HelloBean2 { private String name; private String helloWord; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getHelloWord() { return helloWord; } public void setHelloWord(String helloWord) { this.helloWord = helloWord; } }
Bean的定义:
<bean id="helloBean" class="onlyfun.caterpillar.HelloBean2" singleton="true"> <property name="helloWord" value="test"/> <property name="name" value="Tom"/> </bean>
测试:
ApplicationContext context = new ClassPathXmlApplicationContext("beans-config.xml"); HelloBean2 helloBean1 = (HelloBean2)context.getBean("helloBean"); helloBean1.setName("jack"); System.out.println(helloBean1.getName()); HelloBean2 helloBean2 = (HelloBean2)context.getBean("helloBean"); System.out.println(helloBean2.getName());
输出结果为: jack jack
由此可以看出在Spring容器中,默认情况下,每一个bean只有一个实例。
我们对bean的配置文件做一些修改: 加上了singleton="false" 这个配置,再执行上面的测试,结果为: jack Tom 这时每次去获得helloBean的实例,就是一个新的实例了。(singleton默认是等于true)。
对于Spring2.0,上面的配置可以改成:scope = "prototype" , scope的默认预设值singleton,针对Web应用,scope的值还可以设置为“request”,"session","globalSession"分别对应web的请求阶段,会话阶段,web应用程序阶段。
相关文章推荐
- struts-spring-hibernate 框架 action 请求 ,form 表单提交 id 重复问题
- Spring管理Struts action的安全问题
- 如何理解Struts和Spring单例和多例,以及Action请求的问题
- 关于用spring来管理Struts中的action 所出现的问题
- 使用spring 的action-servlet.xml解决struts线程问题
- Struts的Action访问spring的ApplicationContext的问题
- 解决spring+struts 找不到Action问题
- struts和spring整合创建Action对象的问题?
- 线程安全问题(解决) -------使用spring 的action-servlet.xml解决struts线程问题
- struts2 spring集成时struts.xml文件中action的class属性值意义
- Struts的action自动装配到spring容器可能出现异常的解决方法
- extjs 3,struts 动态form 提交action不能自动装配值问题
- struts2.3.16.3升级遇到无法动态访问action的方法的问题。
- [开发]resin+spring+struts配搭在线上常见的三个问题
- 关于struts2中与Spring中Action对象创建的问题
- struts 中default-action-ref的问题
- struts整合spring(struts创建action)--ssh整合
- struts2.5.12版本使用action通配符提示找不到action的问题
- java struts+spring,自动装配action中的变量
- spring-test单元测试(二)-进行struts action的请求单元测试