6

Every time I create a project from spring.io a ServletInitializer file is created inside the project. I only have the spring cloud gateway dependency. Is this the reason this file exists, and if yes, could someone please explain why?

These are its contents:

package com.springcloudgatewayexample;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

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

}
C96
  • 477
  • 8
  • 33
  • 1
    I'm not entirely sure, but if a spring app is not a web app (using embedded tomcat), it will default to the traditional `WAR` archive. I believe that `SpringBootServletInitializer` is used to configure that `WAR` file. I might be wrong here, hence this is a comment. I mainly use spring projects with `spring web`, and I do not get that file. – AlexT Oct 31 '20 at 01:32
  • Thank you but I dont think the absence of `spring web` is the reason why this file is included in the generated project. When I add `Spring Web`, or `Spring Reactive Web` I still get a project with a `SpringBootServletInitializer` file. – C96 Oct 31 '20 at 08:43

1 Answers1

2

This is the case when you choose the packaging as WAR instead of the JAR while creating a project using SpringInitializr. If you will choose the packaging as JAR (which is selected by default), then only 1 class i.e. ApplicationClass having main method and @SpringBootApplication will be created.

Piyush Agarwal
  • 102
  • 1
  • 14
  • I want to deploy on an external Tomcat server in a local network. Can I use .jar or I must use .war? – Perkone Mar 21 '22 at 08:52