Results 1 to 5 of 5

Thread: Trouble setting sesison variable

  1. #1
    Join Date
    Apr 2012
    Posts
    4

    Default Trouble setting sesison variable

    Hi all,
    I've been struggling with this problem for a while now.

    I am new to Spring and have been trying my best to come to terms with it for this project I am working on.

    I need to set a session variable that can be accessed at any time anywhere within the same session.

    I have a class called UsernameService which does not implement or extend anything (I know that's probably wrong) and has a getter/setter method for one String, username.
    It's bean is configured like this in dispatcher-servlet.xml:
    Code:
    <bean id="usernameService" class="service.UsernameService" scope="session" />
    However, in my 'test' class/webpage (which is also configured in the dispatcher servlet) when I run this code:
    Code:
    UsernameService u = new UsernameService();
    u.setUsername("pleasechange");
    String name = u.getUsername();
            
    mv.addObject("name",name);
            
    UsernameService us = new UsernameService();
            
    String name2 = us.getUsername();
            
    mv.addObject("name2",name2);
    ... the variable name2 should also be set to 'pleasechange' however it is reset to the default username.

    I get the feeling there is something (probably many things) that I've missed along the way but I would love if someone could explain to me where I've gone wrong.

    Thanks!
    James.

  2. #2
    Join Date
    Mar 2012
    Location
    Gurgaon, India
    Posts
    49

    Default UsernameService instances are not managed by Spring

    You are creating your own instances of UsernameService:

    Code:
    UsernameService u = new UsernameService();
    and

    Code:
    UsernameService us = new UsernameService();
    The bean instance from which you are trying to get the username is not the same one on which you set the username.

    If you want Spring to manage the lifetime of bean instances, you will have to obtain the instances through the Spring IoC container. For example:

    Code:
    @Controller
    @RequestMapping("/login")
    public class LoginController {
        @Autowired
        UsernameService service;
    
        @RequestMapping(method = RequestMethod.POST)
        public void submit(@RequestParam String username, @RequestParam String password) {
            this.service.setUsername(username);
        }
    }
    
    @Controller
    @RequestMapping("/home")
    public class HomeController {
        @Autowired
        UsernameService service;
    
        public ModelAndView home() {
            return new ModelAndView("home", new ModelMap("username", this.service.getUsername()));
        }
    }

  3. #3
    Join Date
    Apr 2012
    Posts
    4

    Default

    Thanks for the help Manish!

    I've added what you said into my code, and I have to add:
    Code:
    @Scope("session");
    into my UsernameService class as for some reason the 'scope="session"' in dispatcher-servlet.xml was throwing an error.

    Unfortunately I have a new problem though - I can set a variable now which can be passed to my other beans. To test this I have a test page where I change the username variable, and then a completely separate page where I check what the current username is. The problem is, no matter what type of scope I use (I have tried session, prototype, and singleton) the username always remains as the one I changed it to. My understanding is that if I were to view it in a new session (for this I am using another web browser entirely) then the username should revert back to the default, however it doesn't.

    Thanks again for the help

    EDIT: I've made a little progress with this:
    Above I mentioned that if I write scope="session" in the bean definition then I get an error - I have just tried it with singleton and prototype and it does not give the error. If I have it set to prototype then when I view the page in another browser (or even just look at another page that accesses that bean) then the username goes back to its default value, and if I set it to singleton then the problem remains as I said above which seems weird to me.
    I thought setting this in the bean definition would be irrelevant considering I have the @Scope annotation set, is that correct?

    Here is the error that I am getting when I set scope="session":
    Code:
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'signInController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: service.UsernameService controller.SignInController.uns; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usernameService': Scope 'session' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
    Thanks!
    Last edited by ymhr; Apr 9th, 2012 at 05:17 PM.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    I suggest a read of the reference guide especially the part about scoped proxies!.

    Add a aop:scoped-proxy inside your bean definition to make it a scoped proxy...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Apr 2012
    Posts
    4

    Default

    That solved it, thank you very much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •