spring 和 struts 的问题

来源:百度知道 编辑:UC知道 时间:2024/04/30 01:33:11
请教各位高手
1struts的Action是不是就像一个servlet,如果有成员变量的话会造成多线成的问题?
2如果第一个问题回答“是”的话那么以spring的IoC方式注入到Action中,那必须要存在成员变量,会不会也存在多线程的问题呢?或者说它是怎么解决这个问题的?
3什么时候用Spring配置文件中的singleton=false,什么时候用Spring配置文件中的singleton=true?
谢谢各位了

下面是摘录STRUTS源代码中的注释:
Actions must be programmed in a thread-safe manner, because the controller will share the same instance for multiple simultaneous requests.
Instance and static variables MUST NOT be used to store information related to the state of a particular request.
这个是用于创建ACTION实例的源码:
actions是用于存放已创建的ACTION实例的HASHMAP
从代码中很容易可以看出创建一个ACTION实例之前要先检查这个ACTION的实例是否存在,如果存在则返回在HASHMAP中的实例,很明显ACTION的实例只会创建一个

Action instance;
synchronized (actions) {
instance = (Action) actions.get(className);

if (instance != null) {
if (log.isTraceEnabled()) {
log.trace(" Returning existing Action instance");
}

return (instance);
}

// Create and return a new Action instance
if (log.isTraceEnabled()) {
log.trace(" Creating new A