Reputation: 5259
Background
I'm new to spring MVC but I can clearly see the benefit of using a class annotated with @Controller as opposed to a child class of HttpServlet.
Question
Since spring allows you to have multiple servlet-context files, I will assume some people mix both annotated controller classes and standard HttpServlets. My question is why would you want to? Wouldn't you then have to go through the trouble of connecting that servlet to the appropriate view and lose out on the reason you chose Spring MVC in the first place?
Upvotes: 0
Views: 1439
Reputation: 242686
One of the main ideas of Spring is to make it non-obtrusive - you should not be forced to rewrite all your code if you decide to use Spring in your application.
From that point of view use of Servlet
s other than DispatcherServlet
in Spring application can be easily justified: you may have Servlet
s containing legacy code, or third-party libraries implemented as Servlet
s (web services, RPC, other web frameworks, etc). You don't want to rewrite these legacy components at once, but Spring allows you to leverage its advantages in these components by moving core services of your application to the root application context, so that you can use them from your Spring MVC controllers as well as from other Servlet
s.
Upvotes: 2
Reputation: 7218
Spring annotated controllers are used in conjunction with the Spring DispatcherServlet. This is a Spring implementation of HttpServlet that provides all of the functionality described in the documentation.
The DispatcherServlet is configured in web.xml and the location of the Spring configuration is provided. This can one or more xml files, or one of more classes annotated as @Configuration (from Spring 3.1).
This configuration should contain the element (or @EnableWebMvc). This will trigger spring to scan the classpath for @Controller classes with the application is started.
Upvotes: 1