I have, what I would consider a pretty simple Spring MVC setup. My applicationContext.xml is this:
You need to make following changes to web.xml
in order to support java based configuration. This will tell the the DispatcherServlet
to load configuration using the annotation based java configuration AnnotationConfigWebApplicationContext
. You only need to pass the location of your java config file to the contextConfigLocation
param, as below
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/*path to your WebSpringConfig*/ </param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Update: Doing the same without making changes to web.xml
You can even do this without web.xml
as Servlet Specification 3.0 makes the web.xml
optional. You only need to implement/configure WebApplicationInitializer
interface to configure the ServletContext
which will allow you to create, configure, and perform registration of DispatcherServlet
programmatically. The good thing is that WebApplicationInitializer
is detected automatically.
In summary, one needs to implement WebApplicationInitializer
to get rid of web.xml
.
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(WebSpringConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(rootContext));
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(DispatcherConfig.class);
// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
Update: from comments
A slightly more convoluted explanation is also included in the official Spring reference Spring 4 Release
Reference:
http://docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/web/WebApplicationInitializer.html
Java-based configuration without adding any elements to web.xml
. WebApplicationInitializer is a perfect fit for use with Spring's code-based @Configuration
classes
WebApplicationInitializer « Interface to be implemented in Servlet 3.0+ environments
in order to configure the ServletContext programmatically -- as opposed to (or possibly in conjunction with) the traditional web.xml-based approach. Implementations of this SPI will be detected automatically by SpringServletContainerInitializer, which itself is bootstrapped automatically by any Servlet 3.0 container
. Using Servlet Spec 3.0 of Tomcat 7
From Spring 3.2 some Abstract class were listed which implemented WebApplicationInitializer which will be detected automatically by the SrevletContainer.
AbstractAnnotationConfigDispatcherServletInitializer extends
AbstractDispatcherServletInitializer extends
AbstractContextLoaderInitializer implements WebApplicationInitializer
Using Spring 4.1.6.RELEASE
version with the modules core, web, webmvc, beans
.
public class WebXML_DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { MvcServletXMLConfigurer.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return null;
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Java-based configuration to Serve Static Resources with Spring. Spring Boot
@Configuration
@EnableWebMvc // <mvc:annotation-driven />
@ComponentScan(value = {"com.github.yash777.controllers"})
// <context:component-scan base-package="com.github.yash777" />
public class MvcServletXMLConfigurer extends WebMvcConfigurerAdapter implements WebMvcConfigurer {
/**
* <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
* p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
*
* @return InternalResourceViewResolver as a bean configuration.
*/
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
System.out.println("WebMvcConfigurer - addResourceHandlers() function get loaded...");
// <mvc:resources mapping="/styles/**" location="/css/" />
registry
.addResourceHandler("/styles/**")
.addResourceLocations("/css/") // webapp/css/
.setCachePeriod(3600)
.resourceChain(true) // Spring 4.1
.addResolver(new GzipResourceResolver()) // Spring 4.1
.addResolver(new PathResourceResolver()); // Spring 4.1
// <mvc:resources mapping="/static/**" location="/static/" />
registry.addResourceHandler("/static/**")
.addResourceLocations("/static/", "classpath:/static/") // src/main/resources/static/
.setCachePeriod(3600)
.resourceChain(true)
.addResolver(new PathResourceResolver());
}
}
Listed a sample controller:
@Controller
@RequestMapping(value = { "/controller", "/c" })
public class Test extends HttpServlet {
private static final long serialVersionUID = 1L;
@RequestMapping(value = {"/message", "/m"}, method = RequestMethod.GET )
public void message(HttpServletRequest request, HttpServletResponse response ) throws IOException {
System.out.println("@Controller Get method called.");
}
@RequestMapping(value = "/getView", method = RequestMethod.GET )
public ModelAndView setViewName( Model model ) {
System.out.println("GET... /getView");
ModelAndView mav = new ModelAndView();
mav.setViewName("test");
return mav;
}
}
WEB-INF/web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
</web-app>