Session scoped bean as class attribute of Spring MVC Controller
        Posted  
        
            by 
                Sotirios Delimanolis
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Sotirios Delimanolis
        
        
        
        Published on 2012-11-23T16:47:44Z
        Indexed on 
            2012/11/23
            17:04 UTC
        
        
        Read the original article
        Hit count: 237
        
I have a User class:
@Component
@Scope("session")
public class User {
    private String username;
}
And a Controller class:
@Controller
public class UserManager {
    @Autowired
    private User user;
    @ModelAttribute("user")
    private User createUser() {
        return user;
    }
    @RequestMapping(value = "/user")
    public String getUser(HttpServletRequest request) {
        Random r = new Random();
        user.setUsername(new Double(r.nextDouble()).toString());
        request.getSession().invalidate();
        request.getSession(true);
        return "user";
    }
}
I invalidate the session so that the next time i got to /users, I get another user. I'm expecting a different user because of user's session scope, but I get the same user. I checked in debug mode and it is the same object id in memory. My bean is declared as so:
    <bean id="user" class="org.synchronica.domain.User">
        <aop:scoped-proxy/>
    </bean>
I'm new to spring, so I'm obviously doing something wrong. I want one instance of User for each session. How?
© Stack Overflow or respective owner