Java 没有 Web 服务器的 Spring Boot
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26105061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot without the web server
提问by Michael
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 Spring Boot without the web server?
我有一个简单的 Spring Boot 应用程序,它从 JMS 队列获取消息并将一些数据保存到日志文件中,但不需要 Web 服务器。有没有办法在没有 Web 服务器的情况下启动 Spring Boot?
采纳答案by geoand
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.
如果类路径上没有 Tomcat 依赖项,则 Spring Boot 将不包含嵌入式 tomcat。您可以在EmbeddedServletContainerAutoConfiguration
可以在此处找到其来源的课程中亲自查看此事实。
The meat of the code is the use of the @ConditionalOnClass
annotation on the class EmbeddedTomcat
代码的核心是@ConditionalOnClass
在类上使用注解EmbeddedTomcat
Also, for more information check out thisand thisguide and thispart of the documentation
回答by Stefan K.
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:
如果您想在没有 servlet 容器的情况下运行 spring boot,但在类路径上有一个(例如用于测试),请使用以下内容,如spring boot 文档中所述:
@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
回答by murmelssonic
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:
如果您想使用 spring.io 站点中的“入门”模板之一,但您不需要“默认”(“gs/spring-boot”)模板附带的任何与 servlet 相关的内容,您可以尝试使用调度任务模板(其 pom* 包含 spring-boot-starter 等):
https://spring.io/guides/gs/scheduling-tasks/
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).
这为您提供了 Spring Boot,并且该应用程序作为独立运行(pom 中不包含 servlet 或 spring-webmvc 等)。这就是您想要的(尽管您可能需要添加一些特定于 JMS 的东西,正如其他人已经指出的那样)。
[* I'm using Maven, but assume that a Gradle build will work similarly].
[* 我正在使用 Maven,但假设 Gradle 构建的工作方式类似]。
回答by sancho21
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.
依赖仍然存在,但没有使用。
回答by kinjelom
Spring Boot 2.x
春季启动 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); } }
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); } }
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.
NONE
- 应用程序不应作为 Web 应用程序运行,也不应启动嵌入式 Web 服务器。REACTIVE
- 应用程序应作为反应式 Web 应用程序运行,并应启动嵌入式反应式 Web 服务器。SERVLET
- 应用程序应作为基于 servlet 的 Web 应用程序运行,并应启动嵌入式 servlet Web 服务器。
回答by Mehdi
The simplest solution. in your application.properties file. add the following property as mentioned by a previous answer:
最简单的解决方案。在您的 application.properties 文件中。添加先前答案中提到的以下属性:
spring.main.web-environment=false
spring.main.web-environment=false
For version 2.0.0 of Spring boot starter, use the following property :
对于 Spring boot starter 2.0.0 版,使用以下属性:
spring.main.web-application-type=none
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
有关所有属性的文档,请使用此链接:https: //docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
回答by Sucheth Shivakumar
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
通过程序:
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(YourApplicationMain.class) .web(WebApplicationType.NONE) .run(args);
通过 application.properties 文件:
spring.main.web-environment=false
通过 application.yml 文件:
spring: main: web-environment:false
回答by Paul Rambags
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:
如果您的应用程序需要 Web 功能(例如org.springframework.web.client.RestTemplate
REST 调用)但不想启动 TOMCAT 服务器,只需在 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>
回答by Chun Hau Lai
For Spring boot v2.1.3.RELEASE, just add the follow properties into application.propertes:
对于 Spring boot v2.1.3.RELEASE,只需将以下属性添加到 application.propertes 中:
spring.main.web-application-type=none
回答by vaquar khan
Remove folowing dependancy on your pom file will work for me
删除对您的 pom 文件的以下依赖对我有用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>