Category

Spring @MVC configuration without XML

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.

Java EE update

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:

  1. Write a class that implements javax.servlet.ServletContainerInitializer
  2. Create a file named META-INF/services/javax.servlet.ServletContainerInitializer which contains the fully qualified name of your implementation.

Spring

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.

Setting up the app contexts

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.

Java config files

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.

Filters

You can also create any Servlet Filters here. Here are a few Spring provided Filters.

In this example I set up the CharacterEncodingFilter.

@Enable*

XML namespaces are replaced by class level annotations that begin with @Enable.

  • @EnableWebMvc
  • @EnableAsync
  • @EnableScheduling
  • @EnableLoadTimeWeaving
  • @EnableTransactionManagement

Right now we configure MVC using the mvc: XML namespace, for example: <mvc:annotation:driven/>. The @EnableWebMvc annotation is the replacement.

Spring MVC configuration

Write a configuration class that contains the @EnableWebMvc annotation, which is defined by DelegatingWebMvcConfiguration. 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.

Resources

5 thoughts on “Spring @MVC configuration without XML”

  1. Thanks for sharing this, well written and easy to follow. I’ve got rid of the plethora of spring and hibernate xml files and now I’ve just binned my web.xml. Woohoo! 😀

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.