5

A general question about building a war from a spring boot application and running it in a standalone servlet container. The documentation I've seems seems at odds with examples on Stack Overflow.

The answer here shows the way I read of doing this a couple of months ago. I read this here, but the guide seems to have changed losing the actual example app.

Here the "configure" method references the main spring boot Application.class.

public class WebInitializer extends SpringBootServletInitializer {   

   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
    }  
}

There are also these posts here and here that show the "configure" method referring to the SpringBootServletInitializer sub class itself.

public class BootStrap extends SpringBootServletInitializer {

   public static void main(String[] args) {
      SpringApplication.run(BootStrap.class, args);
   }

   @Override
   protected SpringApplicationBuilder configure(
          SpringApplicationBuilder application) {
      return application.sources(BootStrap.class);
   }   
}

and also there is a main method.

Also the spring-boot-sample-traditional  example app at https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples, which shows "WAR packaging" does it differently

 public class WebConfig extends WebMvcConfigurerAdapter {.........

I was wondering is there are issues with choosing over these different ways of seemingly achieving the same thing in spring boot? Or do they all work equally as well and are interchangeable?

Community
  • 1
  • 1
n99
  • 417
  • 1
  • 7
  • 14

2 Answers2

8

Having your main application class extend SpringBootServletInitializer (Bootstrap in your question) or using a separate class (WebInitializer in your question) is down to personal taste. My preference is to take the Bootstrap approach but they both work in the same way; pick which ever you prefer.

If you are only going to deploy your application to a standalone servlet container then you don't need a main method. The main method is used if you want to run the application as an executable war (java -jar my-app.war), or you want to be able to run it directly in your IDE, i.e. without having your IDE deploy it to a servlet container.

spring-boot-sample-traditional illustrates the use of web.xml to Bootstrap a Spring Boot application. Generally speaking, this isn't a recommended approach unless you're stuck on a Servlet 2.5 container. The use of WebMvcConfigurerAdapter has nothing to do with WAR packaging. Take a look at its web.xml to see the relevant pieces of configuration.

Andy Wilkinson
  • 108,729
  • 24
  • 257
  • 242
  • thanks for the thorough explanation - much appreciated :) . I ended up choosing the "WebInitializer" approach as the Spring Initializr mentioned below generated that.... – n99 Feb 15 '15 at 21:24
  • @Andy Wilkinson, you say "If you are only going to deploy your application to a standalone servlet container then you don't need a main method." ... but how would one go about doing that? All the literature on Spring seems to be about Spring Boot but no one will take Spring Boot to production. At some point in time, they will deploy to a standalone server but there is close to no literature on doing that. Any advice? – Jeach Feb 21 '17 at 20:34
  • AFAIK, the vast majority of people run Spring Boot in production using an executable jar rather than a standalone server. If you need to use a standalone server then both [the documentation](http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file) and this question tell you how to do so. – Andy Wilkinson Feb 21 '17 at 21:15
4

Use Spring Initializr http://start.spring.io/

Choose your project type (Gradle or Maven) and Packaging as war.

Add Web as dependency and Generate the project.

This will bootstrap your app with the "correct" way.

selvinsource
  • 1,837
  • 2
  • 17
  • 20
  • thanks - would also accept this as a answer as well if it was possible to accept multiple answers.... – n99 Feb 15 '15 at 21:24