웹 서버가없는 스프링 부트
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
-
@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
'developer tip' 카테고리의 다른 글
제네릭의 멋진 점은 무엇이며 왜 사용합니까? (0) | 2020.10.06 |
---|---|
MVC, C # 및 jQuery를 사용하여 CSV로 내보내기 (0) | 2020.10.06 |
C # 코드가 컴파일되지 않습니다. (0) | 2020.10.06 |
Firebase 용 Cloud Functions로 업로드 된 파일에서 다운로드 URL 가져 오기 (0) | 2020.10.06 |
항상 다음 정수로 반올림하는 방법 (0) | 2020.10.06 |