developer tip

웹 서버가없는 스프링 부트

copycodes 2020. 10. 6. 08:23
반응형

웹 서버가없는 스프링 부트


JMS 대기열에서 메시지를 가져오고 일부 데이터를 로그 파일에 저장하지만 웹 서버가 필요하지 않은 간단한 Spring Boot 애플리케이션이 있습니다. 웹 서버없이 Spring Boot를 시작할 수있는 방법이 있습니까?


클래스 경로에 Tomcat 종속성이없는 경우 Spring 부트에는 임베디드 Tomcat이 포함되지 않습니다. 이 사실 EmbeddedServletContainerAutoConfiguration여기에서 소스를 찾을 수 있는 클래스에서 직접 볼 있습니다 .

코드의 @ConditionalOnClass핵심은 클래스 에서 주석을 사용하는 것입니다.EmbeddedTomcat


또한, 자세한 내용은 체크 아웃에 대한 가이드와 문서의 일부


서블릿 컨테이너없이 스프링 부트를 실행하고 싶지만 클래스 경로 (예 : 테스트 용)에 하나를 사용하려면 스프링 부트 문서에 설명 된대로 다음을 사용하십시오 .

@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);
    }
}

또한이 속성을 우연히 발견했습니다.

spring.main.web-environment=false

스프링 부트 2.x

  • 응용 프로그램 속성

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

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

어디 WebApplicationType를 :

  • NONE -응용 프로그램은 웹 응용 프로그램으로 실행되지 않아야하며 내장 웹 서버를 시작해서는 안됩니다.
  • REACTIVE -애플리케이션은 반응 형 웹 애플리케이션으로 실행되어야하며 내장 된 반응 형 웹 서버를 시작해야합니다.
  • SERVLET -애플리케이션은 서블릿 기반 웹 애플리케이션으로 실행되어야하며 임베디드 서블릿 웹 서버를 시작해야합니다.

다음과 같이 만들 수 있습니다.

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

@Component
public class CommandLiner implements CommandLineRunner {

  @Override
  public void run(String... args) throws Exception {
    // Put your logic here
  }

}

종속성은 여전히 ​​존재하지만 사용되지는 않습니다.


The simplest solution. in your application.properties file. add the following property as mentioned by a previous answer:

spring.main.web-environment=false

For version 2.0.0 of Spring boot starter, use the following property :

spring.main.web-application-type=none

For documentation on all properties use this link : https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html


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].


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>

For Spring boot v2.1.3.RELEASE, just add the follow properties into application.propertes:

spring.main.web-application-type=none

  • 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
    

Remove folowing dependancy on your pom file will work for me

  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

참고URL : https://stackoverflow.com/questions/26105061/spring-boot-without-the-web-server

반응형