0

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?

dwjohnston
  • 11,163
  • 32
  • 99
  • 194
  • 1
    Don't ever create a context because you need beans, for that purpose use dependency injection. This solution will eventually give you memory problems, performance issues, weird transactional issues, database connection issues just to name a few. Add the `queryRunner` bean to the `MyServletController-servlet.xml` and simply autowire it into your controller. – M. Deinum Apr 13 '15 at 06:22

1 Answers1

0

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

Phoenix
  • 335
  • 2
  • 9
  • You shouldn't be creating contexts just for the purpose of getting beans. The context should be created once and after that you should use dependency injection to get references to your beans. – M. Deinum Apr 13 '15 at 06:23
  • I get this error and I do use dependency injection. It's like the scope attribute is not recognized or something? – user1884155 Feb 16 '16 at 17:22