我有一个简单的Spring Boot应用程序,该应用程序从JMS队列获取消息并将一些数据保存到日志文件,但不需要Web服务器。没有Web服务器,有什么方法可以启动Spring Boot吗?
我有一个简单的Spring Boot应用程序,该应用程序从JMS队列获取消息并将一些数据保存到日志文件,但不需要Web服务器。没有Web服务器,有什么方法可以启动Spring Boot吗?
Answers:
如果您在类路径上没有Tomcat依赖项,则Spring Boot将不包括嵌入式tomcat。您可以在班级自己查看此事实EmbeddedServletContainerAutoConfiguration,您可以在此处找到其来源。
代码的@ConditionalOnClass实质是在类上使用注释EmbeddedTomcat
gs-convert-jar-to-war/completeMaven项目的确添加了嵌入式Tomcat服务器。感觉像个虫子。另请参阅stackoverflow.com/q/25991789/923560spring-boot-starter-tomcatprovided
                    如果要在没有servlet容器的情况下运行spring boot,而在类路径上运行servlet容器(例如,用于测试),请使用以下内容,如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);
    }
}另外,我偶然发现了这个属性:
spring.main.web-environment=falseapplication.properties。
                    spring.main.web-environment现已弃用。仍适用于Boot 2.1.1
                    spring.main.web-application-type=none
                    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);
   }
}
NONE-该应用程序不应作为Web应用程序运行,也不应启动嵌入式Web服务器。
REACTIVE-该应用程序应作为反应式Web应用程序运行,并应启动嵌入式反应式Web服务器。
SERVLET-该应用程序应作为基于Servlet的Web应用程序运行,并应启动嵌入式Servlet Web服务器。
您可以创建如下内容:
@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
  }
}依存关系仍然存在,但没有使用。
最简单的解决方案。在您的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
如果您需要在应用程序中使用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>通过程序:
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如果您要使用spring.io网站上的“入门”模板之一,但不需要“默认”(“ gs / spring-boot”)模板随附的任何与servlet相关的内容,您可以尝试使用调度任务模板(其pom *包含spring-boot-starter等):
https://spring.io/guides/gs/scheduling-tasks/
这样就可以使用Spring Boot,并且该应用程序可以独立运行(pom中不包含servlet或spring-webmvc等)。这就是您想要的(尽管您可能需要添加一些JMS特定的内容,正如其他人已经指出的那样)。
[*我正在使用Maven,但假设Gradle构建将类似地工作]。
对于科特林,这是我最近使用的方法:
// 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")
    }
}并且所有启动通常以带有我的自定义命令的shell结尾。
spring-jms(我想)作为依赖项。然后只要启动应用程序,就不会启动任何服务器。