java.lang.IllegalStateException: No Scope register

2019-03-03 07:32发布

问题:

I'm getting started with Spring and I want to create a fairly simple webapp.

First I have a my web.xml

   <servlet>
        <servlet-name>MyServletController</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyServletController</servlet-name>
        <url-pattern>/submitQuery</url-pattern>     
          <url-pattern>/saveTextAttributes</url-pattern>
          <url-pattern>/saveTextLinks</url-pattern>
    </servlet-mapping>

And the configuration for this, MyServletController-servlet.xml:

<context:component-scan
    base-package="world.hello.mycontrollers"/>

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

The java implementation of the controller MyServletController.java:

package world.hello.mycontrollers;

@Controller
public class MyServletController
{

private QueryRunner queryRunner;
@RequestMapping("/submitQuery")
    public ModelAndView submitQuery(HttpServletRequest request) 
    {
     ApplicationContext context = 
                        new ClassPathXmlApplicationContext("springBeans.xml");

    this.queryRunner = (QueryRunner)context.getBean("queryRunner"); 


    Query myQuery = new Query(request.getParameter("name"));
    ResponseCode rc = queryRunner.runQuery(myQuery);
    String json = "not yet implmented";

    try {
        json = mapper.writeValueAsString(rc);
    } catch (JsonProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return new ModelAndView("text", "model", json);


    }

}

Finally the springBeans.xml

<bean id="queryRunner"
    class="world.hello.business.QueryRunner"
    scope = "session">
     <aop:scoped-proxy/>
</bean>

Run I access this servlet, I get java.lang.IllegalStateException: No Scope registered for scope 'session'

What does this mean?

回答1:

You could try replacing the ClassPathXmlApplicationContext with XmlWebApplicationContext, may be? Since session is a web related scope.