52

I am using spring from more than 6 months. I am not able to understand this underlying mechanism related to the below scenario.

I have a spring web app. Now I autowired the model in controller. Based on url matching it calls respective method. all my methods are singleton.

Now when two users are opening app at same time spring is able to run them parallelly and give results to them. I didnt understand how can it do this. i mean as the bean is singleton it has to either the wait till the bean is not used or overwrite the data in the bean. But spring is working correctly. Can someone explain this behaviour with some analogy.

To explain my question clearly below is a piece of code:

My default controller is simple one:

@Autowired  
private AppModel aModel; 
public AppModel getModel(){
    return aModel;
}
public void setModel(AppModel aModel){
    this.aModel = aModel;
}

@RequestMapping(method = RequestMethod.GET)
public ModelAndView defaultGetter(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    ModelAndView mav = new ModelAndView(getViewName());
    mav.addObject("model", aModel);
    Runtime.getRuntime().gc();
    return mav;
}

Also can some one tell me when two clients open the app will two seperate models get generated when i use @autowired . If only one model bean exists for all clients then say the request from client 1 came in and it take me 30 sec to get results back. Now if second client sends request in 3rd sec then will the first clients request gets overwritten?

I think I am getting confused. Can some one clarify how this magic is happening?

Thanks

3
  • 3
    You should read about threads in application servers. Commented Jun 21, 2013 at 13:02
  • 2
    I read the above discussion but still it is not clear for me. If a new thread is generated for each request when is this happening. will new thread have whole set of new beans copied for it so that other users data wont over write this set? I dont know if i am making any sense :P Commented Jun 21, 2013 at 13:12
  • 1
    the bean is a singleton but each method gets it own reference, per thread, on the stack ... Commented Jun 21, 2013 at 13:17

1 Answer 1

41

Every web request generate a new thread as explained in this thread.

Spring manages different scopes (prototype, request, session, singleton). If two simultaneous requests access a singleton bean, then the bean must be stateless (or at least synchronized to avoid problems). If you access a bean in scope request, then a new instance will be generated per request. Spring manages this for you but you have to be careful and use the correct scope for your beans. Typically, your controller is a singleton but the AppModel has to be of scope request, otherwise you will have problems with two simultaneous requests. This thread could also help you.

About your last question "how this magic is happening?", the answer is "aspect/proxy". Spring create proxy classes. You can imagine that Spring will create a proxy to your AppModel class. As soon as you try to access it in the controller, Spring forwards the method call to the right instance.

Sign up to request clarification or add additional context in comments.

4 Comments

the answer was great, however I have a doubt... "Spring forwards it to the right instance." How is this done? A proxy class is created and does it create a new object of the proxy for each request spawned? so that values remain intact for each request?
Any sample programs to explain this difference in each request would be more helpful.
This behavior can be overridden using @Scope("prototype").
This is factually incorrect; each request gets a dedicated thread to service it for its duration, but those threads are reused.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.