Announcement Announcement Module
No announcement yet.
java.lang.IllegalStateException: No Scope registered for scope 'session' Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • java.lang.IllegalStateException: No Scope registered for scope 'session'

    Hello Friends,

    I am developing a very basic Spring-Hibernate Application for my school.
    I was trying to store the user related information in a bean within session scope.

    I looked and tried various solutions in this forum and web but none worked out
    A very similar problem at
    Tried it by creating a interface and calling setter via interface OR adding proxy-target-class="true" in dispatcherservlet.xml

    I would be pleased of ,if I will get rid of this error stucking past few days.

    There is no issue in getting the bean object but i try to call a method of that class it throws below exception

    java.lang.IllegalStateException: No Scope registered for scope 'session' BeanFactory.doGetBean( ) BeanFactory.getBean( rce.getTarget(
    org.springframework.aop.framework.Cglib2AopProxy$D ynamicAdvisedInterceptor.getTarget(Cglib2AopProxy. java:654)
    org.springframework.aop.framework.Cglib2AopProxy$D ynamicAdvisedInterceptor.intercept(Cglib2AopProxy. java:605)
    com.gloria.sessionbeans.UserInfo$$EnhancerByCGLIB$ $b8ea29b6.setUserid(<generated>)

    Below is my DispatcherServlet(servlet-context.xml)

    ************************************************** *********************
    <?xml version="1.0" encoding="UTF-8"?>
    <beans: beans xmlns="http: //"
    	xmlns: xsi="http: //"
    	xmlns: beans="http: //"
    	xmlns: context="http: //"
    	xmlns: aop="http: //"
    	xsi: schemaLocation="http: // http: //
    		http: // http: //
    		http: // http: //
    		http: // http: //">
    	<!-- DispatcherServlet Context:  defines this servlet's request-processing infrastructure -->
    	<!-- Enables the Spring MVC @Controller programming model -->
    	<annotation-driven />
    	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    	<resources mapping="/resources/**" location="/resources/" />
    	<context: annotation-config />
        <context: component-scan base-package="com.gloria.form" />
        <beans: bean id="messageSource" class="">
            <!-- After classpath:  we give the name of the resource file -->
            <beans: property name="basename" value="classpath: GPSResource" />
            <beans: property name="defaultEncoding" value="UTF-8" />
        </beans: bean>
    	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    	<beans: bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<beans: property name="prefix" value="/WEB-INF/views/" />
    		<beans: property name="suffix" value=".jsp" />
    	</beans: bean>
    	<beans: bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <beans: property name="viewClass">
            <beans: value>
            </beans: value>        
           </beans: property>  
    	</beans: bean>
     	<!-- an HTTP Session-scoped bean exposed as a proxy -->
     	<beans: bean id="userInfo" class="com.gloria.sessionbeans.UserInfo" scope="session">
     		<!-- this next element effects the proxying of the surrounding bean -->
     		<aop: scoped-proxy/>
     	</beans: bean>
     	<beans: bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    		<beans: property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<beans: property name="url" value="jdbc: mysql: //localhost/gloria" />
    		<beans: property name="username" value="root" />
    		<beans: property name="password" value="developer" />
    	</beans: bean>
        <beans: bean id="sessionFactory"
            <beans: property name="dataSource" ref="dataSource" />
            <beans: property name="configLocation">
                <beans: value>classpath: hibernate.cfg.xml</beans: value>
            </beans: property>
            <beans: property name="configurationClass">
                <beans: value>org.hibernate.cfg.AnnotationConfiguration</beans: value>
            </beans: property>
            <beans: property name="hibernateProperties">
                <beans: props>
                    <beans: prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans: prop>
                    <beans: prop key="hibernate.show_sql">true</beans: prop>
                </beans: props>
            </beans: property>
        </beans: bean>
        <beans: bean id="transactionManager"
            <beans: property name="sessionFactory" ref="sessionFactory" />
        </beans: bean>
    	<context: component-scan base-package="" />
    	<context: component-scan base-package="com.gloria.dao" />
    	<context: component-scan base-package="com.gloria.dao.impl" />
    	<context: component-scan base-package="com.gloria.hibernate" />
    	<context: component-scan base-package="com.gloria.service" />
    	<context: component-scan base-package="com.gloria.service.impl" />
    </beans: beans>
    ************************************************** ******************************

    ************************************************** ******************************
    import java.text.DateFormat;
    import java.util.Date;
    import java.util.Locale;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import com.gloria.hibernate.Student;
    import com.gloria.sessionbeans.UserInfo;
     * Handles requests for the application home page.
    public class HomeController {
    	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    	 * Simply selects the home view to render by returning its name.
    	@RequestMapping(value = {"/","/home"}, method = RequestMethod.GET)
    	public String home(@ModelAttribute("student")Student student, BindingResult result) 
    		System.out.println("First Name:" + student.getFirstname() + " Last Name:" + student.getLastname());
    		ApplicationContext context = new ClassPathXmlApplicationContext("servlet-context.xml");
    		//ApplicationContext context = appContext;
    		System.out.println("helloo -->>"+context.containsBean("userInfo"));
    		UserInfo user = (UserInfo)context.getBean("userInfo");
    		System.out.println("Userid-->" + user.getUserid());
    		/*(Locale locale, Model model) {"Welcome home! the client locale is "+ locale.toString());
    		Date date = new Date();
    		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
    		String formattedDate = dateFormat.format(date);
    		model.addAttribute("serverTime", formattedDate );
    		return "home";
    ************************************************** *****************************
    Below is the attached diagram of structure

    Thanks & Regards,
    Ankit Mishra
    Attached Files
    Last edited by ankitmishra; Jul 19th, 2012, 09:29 PM.

  • #2
    Please use [ code][/code ] tags when posting code, that way we can easily read your code... ...

    Why are you annotating your request handling method with a Scope annotation?! That is never going to work... Also NEVER NEVER NEVER construct an application context yourself and especially not on each incoming request (well unless you want to have memory issues, performance problems etc. then you should).


    • #3
      Hi Marten,

      Thanks for your time,added "[code]" tags in the post.
      I removed the "@Scope("session") from controller.
      I simply want to store user related information in a session scope bean when user logs first time and retrieving the infomation from there onwards and that will happen only once.

      Below links suggested to create a spring session bean for above requirement.

      I was searching for the solution ,then i got below approach
      ServletContext context = getServletContext();
      WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContex t(context);

      Please suggest a solution.
      But I am not able to set the value in the object,getting above exception.

      Ankit Mishra
      Last edited by ankitmishra; Jul 20th, 2012, 01:04 AM.


      • #4
        What is it that you want? Do you want a session scoped object or do you want to store something in the http session? The solution is quite different.

        Regarding the application context if you have this kind of code then step back and take another look because you really should be doing dependency injection (there are some corner cases in which you need access to the applicationcontext but in 99% of the cases you shouldn't). So in short NEVER NEVER NEVER create an instance oif the context yourself, in cornercases you might need access but never create one, in general use dependency injection.

        So in short simply inject the user (which will inject a session scoped proxy for the object) which should solve both your problems (also @Scope on the @RequestMapping method should really be removed!).