XML is no longer hip. Actually, there is nothing as unhip as last year’s hip. That is until it becomes hip again 30 years later in failed irony.
Honestly, if you’ve been programming Java EE since the 90s, you know full well how error prone XML config files can be. A glance at an EJB XML config file – especially the CMP relationship config from version 2 – would make any sane person run screaming into the night. Tools were supposed to help, but they really didn’t.
Even the Spring Framework, a music major’s wildly successful solution to Java EE’s problems, has been inundated by XML config files. Until now.
Let’s see how we can configure a Spring @MVC web app with no XML.
Starting with the Servlet 3.0 specification, we can do away with the venerable WEB-INF/web.xml configuration file. In order to do this you must do the following:
javax.servlet.ServletContainerInitializer
Beginning with release 3.1, Spring provides an implementation of the ServletContainerInitializer interface aptly named SpringServletContainerInitializer. Take a peek inside spring-web-[version 3.1 and above].jar and you’ll see the META-INF/services file mentioned above.
The SpringServletContainerInitializer class delegates to an implementation of
org.springframework.web.WebApplicationInitializer that you provide. There is just one method that you need to implement: WebApplicationInitializer#onStartup(ServletContext). You are handed the ServletContext that you need to initialize.
Since we are avoiding writing XML config files, I suggest that instead of using the XmlWebApplicationContext class, use the AnnotationConfigWebApplicationContext which supports classpath scanning for Spring annotation based configuration. You can explicitly add configuration classes, or have the context scan for them.
To start up and shut down the context, we add a ContextLoaderListener.
The RootConfig Java class we specified in the previous example needs to use the @Configuration annotation. Essentially this class corresponds to the <beans> element in Spring XML config files. The <bean> elements are now methods with the @Bean annotation that return a bean instance. The <context:component-scan> element is now the class level annotation @ComponentScan.
You can also create any Servlet Filters here. Here are a few Spring provided Filters.
In this example I set up the CharacterEncodingFilter.
XML namespaces are replaced by class level annotations that begin with @Enable.
Right now we configure MVC using the mvc: XML namespace, for example: <mvc:annotation:driven/>. The @EnableWebMvc annotation is the replacement.
Write a configuration class that contains the @EnableWebMvc annotation, which is defined byDelegatingWebMvcConfiguration. In addition, you will probably do a component scan for controllers here.
To customize the defaults, implement WebMvcConfigurer or extend WebMvcConfigurerAdapter. Any overridden method that does not return NULL will use that value instead of the default.
Github project
Spring MVC
Spring MVC config
Spring Java-based container configuration
Tomcat 7 mapping bug
Servlet 3.0 Specification
Beams’ 3.1 presentation