我有一个简单的Spring Boot应用程序,它从JMS队列获取消息并将一些数据保存到日志文件中,但不需要Web服务器.有没有办法在没有Web服务器的情况下启动Spring Boot?
Ste*_* K. 125
如果你想在没有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);
}
}
Run Code Online (Sandbox Code Playgroud)
另外,我偶然发现了这个属性:
spring.main.web-environment=false
Run Code Online (Sandbox Code Playgroud)
wil*_*oop 65
spring.main.web-application-type=NONE
# REACTIVE, SERVLET
Run Code Online (Sandbox Code Playgroud)@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(MyApplication.class)
.web(WebApplicationType.NONE) // .REACTIVE, .SERVLET
.run(args);
}
}
Run Code Online (Sandbox Code Playgroud)
NONE- 应用程序不应作为Web应用程序运行,也不应启动嵌入式Web服务器.REACTIVE- 应用程序应作为响应式Web应用程序运行,并应启动嵌入式响应式Web服务器.SERVLET- 应用程序应作为基于servlet的Web应用程序运行,并应启动嵌入式servlet Web服务器.
san*_*o21 52
你可以创建这样的东西:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(false).run(args);
}
}
Run Code Online (Sandbox Code Playgroud)
和
@Component
public class CommandLiner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// Put your logic here
}
}
Run Code Online (Sandbox Code Playgroud)
依赖仍然存在,但没有使用.
小智 11
对于 Spring boot v2.1.3.RELEASE,只需将以下属性添加到 application.propertes 中:
spring.main.web-application-type=none
Run Code Online (Sandbox Code Playgroud)
小智 11
使用此代码。
SpringApplication application = new SpringApplication(DemoApplication.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);
Run Code Online (Sandbox Code Playgroud)
最简单的解决方案。在您的application.properties文件中。添加上一个答案提到的以下属性:
spring.main.web-environment = false
对于Spring Boot Starter的2.0.0版本,请使用以下属性:
spring.main.web-application-type =无
有关所有属性的文档,请使用以下链接:https : //docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
通过程序:
ConfigurableApplicationContext ctx = new SpringApplicationBuilder(YourApplicationMain.class)
.web(WebApplicationType.NONE)
.run(args);
Run Code Online (Sandbox Code Playgroud)通过 application.properties 文件:
spring.main.web-environment=false
Run Code Online (Sandbox Code Playgroud)通过 application.yml 文件:
spring:
main:
web-environment:false
Run Code Online (Sandbox Code Playgroud)小智 5
如果您的应用程序需要 Web 功能(例如org.springframework.web.client.RestTemplateREST 调用)但不想启动 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>
Run Code Online (Sandbox Code Playgroud)
Spring Boot 有很多启动器,有些启动器有嵌入式 Web 服务器,有些则没有。以下具有嵌入式 Web 服务器:
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
...
Run Code Online (Sandbox Code Playgroud)
选择满足您的要求且没有服务器支持的一个。
我只需要在我的 spring 应用程序中发出restful json api 请求,所以我需要的启动器是
spring-boot-starter-json
Run Code Online (Sandbox Code Playgroud)
它提供RestTemplate并jackson供我使用。
| 归档时间: |
|
| 查看次数: |
78908 次 |
| 最近记录: |