Announcement Announcement Module
Collapse
No announcement yet.
My Session and Request Scoped bean behave like singleton Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • My Session and Request Scoped bean behave like singleton

    I recently started using spring. I'm trying implement user session, but I always fail. I've spent two days to looking for the answer but I can't find it. I have used annotation and configuration xml files and nothing. Firstly I tried writting this in applicationContext.xml:

    Code:
    <bean id="usuarioLogueado" class="modelos.Usuarios" scope="session">
    <!-- Inyected in next bean placed in dispatcher-servlet.xml -->
    <bean name="registroController" class="controladores.RegistroController">
       <property name="logueado" ref="usuarioLogueado"/>
    </bean>
    The error was: "Scope 'session' is not active for the current thread". I found the solution. I had to put the next code in web.xml file:

    Code:
    <listener>    
    <listenerclass>
    org.springframework.web.context.request.RequestContextListener
    </listener-class>
    </listener>
    After, that problem dissapeared, I became happy, but hapyness was short... Testing application in my pc and anothers in my network I checked that bean (usuarioLogueado) was still behaving like a singleton bean. I've seen in blogs and forums the need to use scoped-proxy as follow:

    Code:
    <bean id="usuarioLogueado" class="modelos.Usuarios" scope="session">
            <aop:scoped-proxy/>
    </bean>
    in ApplicationContext.xml file. The namespace are:

    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                               http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    Now, the application doesn't run. If I write any aop element, this doesn't run. I've checked with anothers namespaces, 3.1 and previous version of spring and nothing. I use netbeans ide and this ide don't give a lot of information when a application don't run. It just say "the context can't run". If I remove aop:scoped-proxy tag and use the equivalent annotation (@Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)), I have the same problem. So, I thought this problem is due to context described in web.xml. I stick here this web.xml file:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
        <!-- Por defecto en web.xml -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!-- Lo siguiente es para que la aplicación reconozca los scope request y session -->
        <listener>
            <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
        </listener>
        <!-- Por defecto en web.xml -->
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>2</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>*.htm</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        <welcome-file-list>
            <welcome-file>index.htm</welcome-file>
        </welcome-file-list>
        <!-- Lo siguiente es para que el contexto de la aplicación reconozca la codificación UTF-8 -->
        <filter>  
            <filter-name>encodingFilter</filter-name>  
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
            <init-param>  
                <param-name>encoding</param-name>  
                <param-value>UTF-8</param-value>  
            </init-param>  
            <init-param>  
                <param-name>forceEncoding</param-name>  
                <param-value>true</param-value>  
            </init-param>  
        </filter>  
        <filter-mapping>  
            <filter-name>encodingFilter</filter-name>  
            <url-pattern>/*</url-pattern>  
        </filter-mapping> 
    </web-app>
    I've tried with filters too. First filter I tested:

    Code:
       <filter>
         <filter-name>springSecurityFilterChain</filter-name>
         <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
         </filter>
         <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
         </filter-mapping>
    Result, proyect doesn't run. Second filter:

    Code:
     <filter>
            <filter-name>requestContextFilter</filter-name>
            <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>requestContextFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    Result: proyect run, but scoped beans still are singleton and when I put or equivalent annotation, proyect doesn't run.

    In other forum, a person ask me which spring-mvc and cglib version I was using. I use spring-mvc 3. I didn't have idea about what is cglib, but I documented myself and downloaded cglib2.2.3 and put it in the environment system variable classpath. And nothing

    Please help.

  • #2
    I've created the proyect again and it work. And without <aop:scoped-proxy />. Now my session beans behave as such

    Comment

    Working...
    X