2

Spring beans instantiated by spring container are singleton beans by default...datasource, sessionFactory instantiated through spring xml are all singletons When i use @Controller, @Repository, @Service, objects are created.

If only one object is created by that name, how simultaneous users sending requests to a spring MVC application are handled? Are they handled by the same objects

Are those requests controlled/serviced by spring controller/service singleton objects? If yes then it could lead to mess i guess...

As a parallel in STRUTS2, there is value stack which creates separate objects. Is there some concept like that in SPRING MVC/Annotation???

This sounds very basic, but so far as i have searched spring website/stackoverflow, i have not come across something which explains this aspect.

Please throw some light on this..

Amar
  • 1,556
  • 3
  • 18
  • 28
  • Yes, the same objects process requests from all the users(as long as there is only JVM instance). This does not "go to mess" because mostly controllers are stateless or readonly(i.e. they don't modify their own state) and keep the request and user information in the dedicated objects. This question is rather about how java servlets work because this is not Spring specific, see http://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-session-variables-and-multithreading?rq=1 So yes, the controllers and repositories may exist for weeks and even months until the shutdown. – Boris Treukhov Nov 24 '13 at 10:51
  • @BorisTreukhov when you mention "this is about java servlets work", does the controller or service class extend HttpServlet?? The controller annotation simply mentions that the class serves role of controller. – Amar Nov 24 '13 at 11:33
  • No, controller does not extend HttpServlet, but Spring DispatcherServlet does(it implements Front Controller design pattern) it has it's own Spring context (a set of objects) and delegates the request processing to the objects in this context(mapper classes actually, which in turn delegate the processing to the `@Controller` annotated beans). – Boris Treukhov Nov 24 '13 at 12:05

1 Answers1

1

Consider the following example

public class Driver {
    public static void main(String[] args) {
        Controller controller = new Controller();
        Thread t1 = new Thread(new Example(controller));
        Thread t2 = new Thread(new Example(controller));
        t1.start();
        t2.start();
    }

    public static class Example implements Runnable {
        private Controller controller;
        public Example(Controller controller) {
            this.controller = controller;
        }
        @Override
        public void run() {
            controller.doSomething();
        }       
    }

    public static class Controller {
        public void doSomething() {
            System.out.println("hello world");
        }
    }
}

There is only one Controller object. It is shared between Threads t1 and t2. This is not a problem because all data that the Controller uses in its doSomething() method is local, nothing is shared.

In the same way, in a Spring web application, you will have a single instance of a @Controller class which shouldn't have any member variables. When a request arrives, the Servlet container will dispatch a Thread to execute the DispatcherServlet and the handler method you defined.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • now i understand how important it is **NOT TO HAVE MEMBER VARIABLES IN CONTROLLER/SERVICE OBJECTS**. These classes should have only methods. – Amar Nov 24 '13 at 11:38