Java 没有web服务器的Spring引导
我有一个简单的Spring引导应用程序,它从JMS队列获取消息并将一些数据保存到日志文件中,但不需要web服务器。有没有办法在没有web服务器的情况下启动Spring Boot?如果类路径上没有tomcat依赖项,Spring Boot将不包括嵌入式tomcat。 您可以在类Java 没有web服务器的Spring引导,java,spring-boot,Java,Spring Boot,我有一个简单的Spring引导应用程序,它从JMS队列获取消息并将一些数据保存到日志文件中,但不需要web服务器。有没有办法在没有web服务器的情况下启动Spring Boot?如果类路径上没有tomcat依赖项,Spring Boot将不包括嵌入式tomcat。 您可以在类EmbeddedServletContainerAutoConfiguration中查看这一事实,您可以找到该类的源代码 代码的核心是在类EmbeddedTomcat 另外,有关更多信息,请查看和指导以及部分文档如果您希望
EmbeddedServletContainerAutoConfiguration
中查看这一事实,您可以找到该类的源代码
代码的核心是在类EmbeddedTomcat
另外,有关更多信息,请查看和指导以及部分文档如果您希望在没有servlet容器的情况下运行spring boot,但在类路径上有servlet容器(例如,用于测试),请使用以下命令,如中所述:
如果您想使用spring.io站点中的一个“入门”模板,但不需要“默认”(“gs/spring boot”)模板附带的任何与servlet相关的内容,您可以尝试调度任务模板(其pom*包含spring boot starter等): 这将为您提供Spring引导,并且该应用程序作为独立程序运行(pom中不包括servlet或SpringWebMVC等)。这正是您想要的(尽管您可能需要添加一些特定于JMS的东西,正如其他人已经指出的)
[*我使用的是Maven,但假设Gradle构建也可以类似地工作]。您可以创建如下内容:
@springboot应用程序
公共类应用程序{
公共静态void main(字符串[]args){
新的SpringApplicationBuilder(Application.class).web(false).run(args);
}
}
及
@组件
公共类CommandLiner实现CommandLineRunner{
@凌驾
公共无效运行(字符串…参数)引发异常{
//把你的逻辑放在这里
}
}
依赖项仍然存在,但没有使用。Spring Boot 2.x
- 或
其中:
-应用程序不应作为web应用程序运行,也不应启动嵌入式web服务器NONE
-应用程序应作为REACTIVE web应用程序运行,并应启动嵌入式REACTIVE web服务器REACTIVE
-应用程序应作为基于SERVLET的web应用程序运行,并应启动嵌入式SERVLET web服务器SERVLET
最简单的解决方案。在application.properties文件中。添加上一个答案中提到的以下属性: spring.main.web环境=false 对于2.0.0版Spring boot starter,请使用以下属性: spring.main.web应用程序类型=无 有关所有属性的文档,请使用以下链接:
- 通过以下程序:
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
<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>
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧启动机tomcat
对于Spring boot v2.1.3.RELEASE,只需将以下属性添加到application.propertes中:
spring.main.web-application-type=none
删除以下依赖于你的pom文件将为我工作
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
org.springframework.boot
SpringBootStarterWeb
使用此代码
SpringApplication application = new SpringApplication(DemoApplication.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);
对于Koling,我最近使用的是:
// 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")
}
}
//src/main/com.blabla/ShellApplication.kt
/**
*shell应用程序的主要入口点。
*/
@SpringBoot应用程序
公共类ShellApplication:CommandLineRunner{
伴星{
@JvmStatic
趣味主线(args:Array){
val application=SpringApplication(ShellApplication::class.java)
application.webApplicationType=webApplicationType.NONE
application.run(*args);
}
}
重写有趣的运行(vararg args:String?{}
}
//src/main/com.blabla/command/CustomCommand.kt
@壳组件
公共类自定义命令{
private val logger=KotlinLogging.logger{}
@ShellMethod(“从CSV导入、创建和更新数据”)
public fun importsv(@ShellOption()文件:String){
logger.info(“Hi”)
}
}
所有启动程序通常都会以一个shell结束,并提供我的自定义命令。类似于上面的@nayun oh answer,但对于较旧版本的Spring,请使用以下代码:
SpringApplication application = new SpringApplication(DemoApplication.class);
application.setApplicationContextClass(AnnotationConfigApplicationContext.class);
application.run(args);
您可以使用spring引导启动器依赖项。这将不会有网络的东西
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
org.springframework.boot
弹簧靴起动器
在Spring引导中,Spring Web依赖项提供了一个嵌入式Apache Tomcat Web服务器。如果删除pom.xml中的spring boot starter web依赖项,则它不提供嵌入式web服务器
删除以下依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
org.springframework.boot
SpringBootStarterWeb
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
...
选择一个满足您的需求且不支持服务器的
我只需要在我的spring应用程序中发出restful json api请求,所以我需要的启动程序是
spring-boot-starter-json
它提供了
restemplate
和jackson
供我使用。如果您不需要web,那么不要包含它,如果不包含,嵌入式服务器将不会启动。您只需要starter父级并添加springjms
(我猜)作为依赖项。然后只启动应用程序,不会启动任何服务器。如果你错了,我只使用spring boot starter批处理,我的pom.xml中不包含任何tomcat或其他服务器配置,但是执行应用程序会启动web c
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
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
...
spring-boot-starter-json