Announcement Announcement Module
Collapse
No announcement yet.
java.lang.IllegalStateException: No Scope registered for scope 'session' Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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 http://stackoverflow.com/questions/5...e-a-controller
    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'
    org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:325 )
    org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:193)
    org.springframework.aop.target.SimpleBeanTargetSou rce.getTarget(SimpleBeanTargetSource.java:33)
    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)

    ************************************************** *********************
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans: beans xmlns="http: //www.springframework.org/schema/mvc"
    	xmlns: xsi="http: //www.w3.org/2001/XMLSchema-instance"
    	xmlns: beans="http: //www.springframework.org/schema/beans"
    	xmlns: context="http: //www.springframework.org/schema/context"
    	xmlns: aop="http: //www.springframework.org/schema/aop"
    	xsi: schemaLocation="http: //www.springframework.org/schema/mvc http: //www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    		http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-3.0.xsd
    		http: //www.springframework.org/schema/aop http: //www.springframework.org/schema/aop/spring-aop-3.0.xsd
    		http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    		
    
    	<!-- 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="org.springframework.context.support.ReloadableResourceBundleMessageSource">
            <!-- 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>
                org.springframework.web.servlet.view.tiles2.TilesView
            </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"
    		destroy-method="close">
    		<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"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <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"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <beans: property name="sessionFactory" ref="sessionFactory" />
        </beans: bean>
        
    	<context: component-scan base-package="com.gloria.app" />
    	<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>
    ************************************************** ******************************

    HomeController.java

    ************************************************** ******************************
    Code:
    package com.gloria.app;
    
    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.context.support.ClassPathXmlApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    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.
     */
    @Controller
    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)
    	@Scope("session")
    	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");
    		//appContext.refresh();
    		//appContext.setConfigLocation("/servlet-context.xml");	
    		//appContext.setConfigLocation("C:\\Users\\Ankit\\Documents\\workspace-sts-2.9.2.RELEASE\\GPS\\src\\main\\webapp\\WEB-INF\\spring\\appServlet\\servlet-context.xml");
    		//appContext.setConfigLocation("C:/Users/Ankit/Documents/workspace-sts-2.9.2.RELEASE/GPS/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml");	
    		//ApplicationContext context = appContext;
    		System.out.println("helloo -->>"+context.containsBean("userInfo"));
    		UserInfo user = (UserInfo)context.getBean("userInfo");
    		user.setUserid("N1");
    		System.out.println("Userid-->" + user.getUserid());
    		/*(Locale locale, Model model) {
    		logger.info("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
    Attachment


    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).

    Comment


    • #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.
      http://stackoverflow.com/questions/1...-in-spring-mvc

      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.

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

      Comment


      • #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!).

        Comment

        Working...
        X