Spring Boot without the web server

后端 未结 16 792
眼角桃花
眼角桃花 2020-11-29 16:50

I have a simple Spring Boot application that gets messages from a JMS queue and saves some data to a log file, but does not need a web server. Is there any way of starting S

相关标签:
16条回答
  • 2020-11-29 17:05

    if you want to run spring boot without a servlet container, but with one on the classpath (e.g. for tests), use the following, as described in the spring boot documentation:

    @Configuration
    @EnableAutoConfiguration
    public class MyClass{
        public static void main(String[] args) throws JAXBException {
                     SpringApplication app = new SpringApplication(MyClass.class);
             app.setWebEnvironment(false); //<<<<<<<<<
             ConfigurableApplicationContext ctx = app.run(args);
        }
    }
    

    also, I just stumbled across this property:

    spring.main.web-environment=false
    
    0 讨论(0)
  • 2020-11-29 17:06

    Remove folowing dependancy on your pom file will work for me

      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
    0 讨论(0)
  • 2020-11-29 17:07

    Spring Boot 2.x

    • Application Properties

      spring.main.web-application-type=NONE 
      # REACTIVE, SERVLET
      
    • or SpringApplicationBuilder

      @SpringBootApplication
      public class MyApplication {
      
          public static void main(String[] args) {
              new SpringApplicationBuilder(MyApplication.class)
                  .web(WebApplicationType.NONE) // .REACTIVE, .SERVLET
                  .run(args);
         }
      }
      

    Where WebApplicationType:

    • NONE - The application should not run as a web application and should not start an embedded web server.
    • REACTIVE - The application should run as a reactive web application and should start an embedded reactive web server.
    • SERVLET - The application should run as a servlet-based web application and should start an embedded servlet web server.
    0 讨论(0)
  • 2020-11-29 17:08

    Spring boot will not include embedded tomcat if you don't have Tomcat dependencies on the classpath. You can view this fact yourself at the class EmbeddedServletContainerAutoConfiguration whose source you can find here.

    The meat of the code is the use of the @ConditionalOnClass annotation on the class EmbeddedTomcat


    Also, for more information check out this and this guide and this part of the documentation

    0 讨论(0)
  • 2020-11-29 17:10

    In Spring boot, Spring Web dependency provides an embedded Apache Tomcat web server. If you remove spring-boot-starter-web dependency in the pom.xml then it doesn't provide an embedded web server.

    remove the following dependency

    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    0 讨论(0)
  • 2020-11-29 17:14

    If you want to use one of the "Getting Started" templates from spring.io site, but you don't need any of the servlet-related stuff that comes with the "default" ("gs/spring-boot") template, you can try the scheduling-tasks template (whose pom* contains spring-boot-starter etc) instead:

    https://spring.io/guides/gs/scheduling-tasks/

    That gives you Spring Boot, and the app runs as a standalone (no servlets or spring-webmvc etc are included in the pom). Which is what you wanted (though you may need to add some JMS-specific stuff, as someone else points out already).

    [* I'm using Maven, but assume that a Gradle build will work similarly].

    0 讨论(0)
提交回复
热议问题