Search code examples
javaspring-mvcautowiredsession-scope

Session Scoped Bean Spring multiple user having same instance


I am developing a web application using spring mvc 3.2.3.

I have a session scoped bean described in my dispatcher-servlet.xml as :

<bean id="domainCountBean" class="com.count.beans.CountSelectionBean" scope="session">
    <aop:scoped-proxy/>
</bean>

Then I want to use it from a controller as:

@Controller
@RequestMapping("/")
public class DomainController {
    @Autowired
    CountSelectionBean domainCountBean;  

.....

    @RequestMapping(value = "/manual_domain/ajaxSet/", method = RequestMethod.GET)
    public ResponseEntity<String> ajaxSetResetApprovalId(
            HttpServletRequest req, HttpServletResponse res
            ) {



        domainCountBean.getDeselectedIds().put(idStr, appId);



        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "text/html; charset=utf-8");
        return new ResponseEntity<String>(jsonString, responseHeaders, HttpStatus.CREATED);

    }

When I log into my webapp from browser-A : and use domainCountBean things seems to get stored in the session correctly.

In parallel, if I try to login using different user from browser-B: and use domainCountBean then I get value inserted by the user from browser-A. And it happens vice versa.

I am struggling to keep two different instance for the bean mentioned. I would like to if my concept is wrong, or that can fix my problem.

P.S: I can not use sessionAttribute here.


Solution

  • The problem was solved by assigning "session" scope to the controller.

    @Controller
    @Scope("session")
    @RequestMapping("/")
    public class DomainController {
        @Autowired
        CountSelectionBean domainCountBean;  
    

    Although, by definition, I thought it should have worked without the session scope for controller.