Spring Boot without the web server

后端 未结 16 790
眼角桃花
眼角桃花 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 16:55
    • Through program :

      ConfigurableApplicationContext ctx =  new  SpringApplicationBuilder(YourApplicationMain.class)
      .web(WebApplicationType.NONE)
      .run(args);
      
    • Through application.properties file :

      spring.main.web-environment=false 
      
    • Through application.yml file :

      spring:
       main:
        web-environment:false
      
    0 讨论(0)
  • 2020-11-29 16:59

    You can create something like this:

    @SpringBootApplication
    public class Application {
      public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(false).run(args);
      }
    }
    

    And

    @Component
    public class CommandLiner implements CommandLineRunner {
    
      @Override
      public void run(String... args) throws Exception {
        // Put your logic here
      }
    
    }
    

    The dependency is still there though but not used.

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

    If you need web functionality in your application (like org.springframework.web.client.RestTemplate for REST calls) but you don't want to start a TOMCAT server, just exclude it in the POM:

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

    Use this code.

    SpringApplication application = new SpringApplication(DemoApplication.class);
    application.setWebApplicationType(WebApplicationType.NONE);
    application.run(args);
    
    0 讨论(0)
  • 2020-11-29 17:01

    For Kotling here is what I used lately:

    
    // src/main/com.blabla/ShellApplication.kt
    
    /**
     * Main entry point for the shell application.
     */
    @SpringBootApplication
    public class ShellApplication : CommandLineRunner {
        companion object {
            @JvmStatic
            fun main(args: Array<String>) {
                val application = SpringApplication(ShellApplication::class.java)
                application.webApplicationType = WebApplicationType.NONE
                application.run(*args);
            }
        }
    
        override fun run(vararg args: String?) {}
    }
    
    // src/main/com.blabla/command/CustomCommand.kt
    
    @ShellComponent
    public class CustomCommand {
        private val logger = KotlinLogging.logger {}
    
        @ShellMethod("Import, create and update data from CSV")
        public fun importCsv(@ShellOption() file: String) {
            logger.info("Hi")
        }
    }
    

    And everything boot normally ending up with a shell with my custom command available.

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

    Spring boot has many starters, some starters have an embedded web server, some don't. The following have the embedded web server:

    spring-boot-starter-web
    spring-boot-starter-data-jpa
    spring-boot-starter-jetty
    spring-boot-starter-tomcat
    spring-boot-starter-jdbc
    spring-boot-starter-data-rest
    ...
    

    Pick the one that meets your requirements and that does not have server support.

    I only need to make restful json api request in my spring application, so the starter I need is

    spring-boot-starter-json
    

    which provide RestTemplate and jackson for me to use.

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