spring mvc 的Controller类默认Scope是单例(singleton)的

使用Spring MVC有一段时间了,之前一直使用Struts2,在struts2中action都是原型(prototype)的, 说是因为线程安全问题,对于Spring MVC中bean默认都是(singleton)单例的,那么用@Controller注解标签注入的Controller类是单例实现的?

测试结果发现spring3中的controller默认是单例的,若是某个controller中有一个私有的变量i,所有请求到同一个controller时,使用的i变量是共用的,即若是某个请求中修改了这个变量a,则,在别的请求中能够读到这个修改的内容。 若是在@Controller之前增加@Scope("prototype"),就可以改变单例模式为多例模式

以下是测试步骤,代码与结果.

1. 如果是单例类型类的,那么在Controller类中的类变量应该是共享的,如果不共享,就说明Controller类不是单例。以下是测试代码:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class ExampleAction {
    private int singletonInt=1;

     @RequestMapping(value = "/test")
     @ResponseBody
     public String singleton(HttpServletRequest request,
             HttpServletResponse response) throws Exception {
         String data=request.getParameter("data");
         if(data!=null&&data.length()>0){
             try{
              int paramInt= Integer.parseInt(data);
                 singletonInt = singletonInt + paramInt;
             }catch(Exception ex){
                 singletonInt+=10;
             }
         }else{
             singletonInt+=1000;
         } 
         return String.valueOf(singletonInt);
    }
}

分别三次请求: http://localhost:8080/example/test.do?data=15

得到的返回结果如下。

第一次: singletonInt=15

第二次: singletonInt=30

第三次: singletonInt=45

从以上结果可以得知,singletonInt的状态是共享的,因此Controller是单例的。

2. 如果Controller类是单例,那么多个线程请求同一个Controller类中的同一个方法,线程是否会堵塞

@RequestMapping(value = "/sleepdata")
@ResponseBody
public String switcher(HttpServletRequest request
     , HttpServletResponse response)
           throws Exception {
  String sleep = request.getParameter("sleep");
  if (sleep.equals("on")) {
      Thread.currentThread().sleep(100000);
       return "sleep on";
   } else {
       return sleep;
  }
}  

验证方法:分别发送两个请求,

第一个请求:http://localhost:8080/coreplat/sleepdata.do?sleep=on

第二个请求:http://localhost:8080/coreplat/sleepdata.do?sleep=test

验证结果:第一个请求发出去以后,本地服务器等待100s,然后返回结果"sleep on",在本地服务器等待的者100s当中,发送第二个请求,直接返回结果"test"。说明之间的线程是不互相影响的。



No Scope registered for scope 'Singleton'

02-25

Struts Problem ReportrnStruts has detected an unhandled exception: rnrnMessages: No Scope registered for scope 'Singleton' rnUnable to instantiate Action, tc.mts.model.login.LoginAction, defined for 'login' in namespace '/base/login'No Scope registered for scope 'Singleton' rn rnFile: org/springframework/beans/factory/support/AbstractBeanFactory.java rnLine number: 295 rnrn[code=XML]rnStacktracesrnUnable to instantiate Action, tc.mts.model.login.LoginAction, defined for 'login' in namespace '/base/login'No Scope registered for scope 'Singleton' rn com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:306)rn com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:387)rn com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:186)rn org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)rn org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)rn com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)rn org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:458)rn org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)rn org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn tc.mts.common.RequestFilter.doFilter(RequestFilter.java:50)rn org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)rn org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)rn org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)rn org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)rn org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)rn org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)rn org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)rn org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)rn java.lang.Thread.run(Unknown Source)rn rnjava.lang.IllegalStateException: No Scope registered for scope 'Singleton' rn org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:295)rn org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)rn org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)rn org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:881)rn tc.mts.util.SpringUtil.getBean(SpringUtil.java:18)rn tc.mts.model.login.LoginAction.(LoginAction.java:20)rn sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)rn sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)rn sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)rn java.lang.reflect.Constructor.newInstance(Unknown Source)rn java.lang.Class.newInstance0(Unknown Source)rn java.lang.Class.newInstance(Unknown Source)rn com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:119)rn com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:150)rn com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:139)rn com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:109)rn com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:287)rn com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:387)rn com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:186)rn org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)rn org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)rn com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)rn org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:458)rn org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)rn org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn tc.mts.common.RequestFilter.doFilter(RequestFilter.java:50)rn org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)rn org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)rn org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)rn org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)rn org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)rn org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)rn org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)rn org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)rn java.lang.Thread.run(Unknown Source)rnrnrn[/code]rnrn错误信息如上,请大家帮助.

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试