miércoles, junio 15, 2011

It's Strange But It's True, I Can't Get Over The Way You Love Me Like You Do, But I Have To Be Sure, When I Walk Out That Door (I Want To Break Free - Queen)



Spring Framework 3.1 M2 has been released past week. This new release is the last milestone, and next versions will be tagged as RC.

This new version completes the work started in M1, and adds new functionalities.

To summarize them we can name:

  • Java-based application configuration approach has changed from the @Feature approach in M1 to @Enable* annotations (I will talk about this in next post).
  • Cache abstraction has been revised.
  • A new "packagesToScan" feature for JPA.
  • REST support refinements with respect to URI templates.
  • Many more refinements.

In current post I will talk about one new feature that will allow us to configure Spring Web MVC application without web.xml.

With Servlet 3.0 specification, web.xml is not required anymore, you can configure your servlets using @WebServlet annotation. Prior to Spring 3.1, DispatcherServlet should be declared and configured in web.xml, so although our application was deployed using Servlet 3.0 specification, web.xml was "a must". With Spring 3.1, things are different. You can use WebApplicationInitializer approach for bootstrapping a Spring web application without web.xml.

First of all let’s take a look at traditional way (using web.xml).


No problem here, if you have developed applications using Spring MVC this file will sound you familiar.

But now let’s use WebApplicationInitializer approach.

Spring 3.1 comes with WebApplicationInitializer interface, that must be implemented in Servlet 3.0 environments in order to configure the ServletContext programmatically - as opposed to (or in conjunction with) the traditional web.xml-based approach.

What we need is a class (can be created in any package, next I will explain why!!!) that will implement WebApplicationInitializer. Our equivalent class to previous web.xml is:


See that same information is required, but is provided programmatically instead of using web.xml. Spring application context must be created by you setting config file/s location, DispatcherServlet is still valid and must be registered too using addServlet method, and of course context-param can be added using setInitParameter method. And only creating this class that implements WebApplicationInitializer, you can remove your web.xml from your application.

Look ma' no web.xml and Spring Web application is still working. And now let me explain why if our class is not configured anywhere and moreover is created in any place (inside classpath), is instantiated and its onStartup method called? Servlet 3.0 ServletContainerInitializer is designed to support code-based configuration of the servlet container at startup phase. Spring people have created SpringServletContainerInitializer class that implements ServletContainerInitializer, and this class will be loaded and instantiated and onStartup method invoked by any Servlet 3.0-compliant container during container startup. This occurs through the JAR Services API ServiceLoader.load(Class) method detecting the spring-web module's META-INF/services/javax.servlet.ServletContainerInitializer service provider configuration file.

Hope you like this new feature.

Note: Servlet 3.0 container is required (for example Tomcat 7), and versions of Tomcat <=7.0.14, root mapping ("/") cannot be used.
Download Code.

Music: http://www.youtube.com/watch?v=Odk3W3qLByI

6 comentarios:

  1. Anónimo3:24 p. m.

    If there is no special of conditional logic when configuring the app, then there is no point in doing it programmatically: if you change something like a context parameter, then you need to recompile, repackage and redeploy! if you use web.xml you can change it in place and restart the app (in Tomcat AFAIK)...this is simpler.

    ResponderEliminar
  2. Great article but the source link is dead :)

    ResponderEliminar
  3. I have tried now and link has been downloaded correctly, maybe when you have tried was some problems with github. But anyway thank you very much if I find that more people have the same problem I will try to upload in different server.

    Thanks.

    ResponderEliminar
  4. replica bags us fake hermes l2l78r2w33 replica bags toronto replica bags wholesale hong kong More Info a9p33p2x09 replica bags aaa quality my review here p2u21j5l36 aaa replica bags replica bags online pakistan

    ResponderEliminar