WebサーバーなしのSpring Boot


101

JMSキューからメッセージを取得し、一部のデータをログファイルに保存する単純なSpring Bootアプリケーションがありますが、Webサーバーは必要ありません。WebサーバーなしでSpring Bootを起動する方法はありますか?


3
Webが必要ない場合は含めないでください。含めない場合、組み込みサーバーは起動しません。必要なのは、スターターの親とspring-jms依存関係として(おそらく)追加するだけです。次に、アプリケーションを起動するだけで、サーバーは起動されません。
M. Deinum 2014


1
あなたは間違っています。私はspring-boot-starter-batchのみを使用しています。pom.xmlにTomcatやその他のサーバー構成を含めていませんが、アプリケーションを実行するとWebコンテナが起動します。どこかでパラメータを取る必要があります。
Mehdi 2017

回答:


24

クラスパスにTomcatの依存関係がない場合、Spring Bootには組み込みのTomcatが含まれません。この事実は、EmbeddedServletContainerAutoConfigurationソースがここにあるクラスで自分で確認できます。

コードの@ConditionalOnClass本質は、クラスでのアノテーションの使用ですEmbeddedTomcat


また、より多くの情報をチェックしてください。この、このガイドおよびこのマニュアルの一部


現在のバージョンでは、スコープで宣言された依存関係にもかかわらず、gs-convert-jar-to-war/completeMavenプロジェクト組み込みのTomcatサーバーを追加spring-boot-starter-tomcatしますprovided。これはバグのように感じます。また、stackoverflow.com
q /

136

サーブレットコンテナなしでスプリングブートを実行したいが、クラスパス上にある場合(たとえば、テスト用)、スプリングブートのドキュメントで説明されているように、以下を使用します。

@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

12
プロパティを追加するだけでapplication.properties問題なく動作します。
Wim Deblauwe 16

8
これは機能しますが、Spring Boot 2.0では非推奨です。2.0バージョンについては、この回答をご覧ください
。stackoverflow.com/ a

1
参考:このプロパティspring.main.web-environmentは廃止されました。
引き続き

1
ブート2.x-application.setWebApplicationType(WebApplicationType.NONE);
Michael Munsey、

1
あなたがこれらの日を使用することになっているのはspring.main.web-application-type=none
くそったれ

94

Spring Boot 2.x

  • アプリケーションのプロパティ

    spring.main.web-application-type=NONE 
    # REACTIVE, SERVLET
  • またはSpringApplicationBuilder

    @SpringBootApplication
    public class MyApplication {
    
        public static void main(String[] args) {
            new SpringApplicationBuilder(MyApplication.class)
                .web(WebApplicationType.NONE) // .REACTIVE, .SERVLET
                .run(args);
       }
    }

場所WebApplicationType

  • NONE -アプリケーションは、Webアプリケーションとして実行してはならず、組み込みWebサーバーを起動してはなりません。
  • REACTIVE -アプリケーションは、リアクティブWebアプリケーションとして実行し、組み込みのリアクティブWebサーバーを起動する必要があります。
  • SERVLET -アプリケーションはサーブレットベースのWebアプリケーションとして実行され、埋め込みサーブレットWebサーバーを起動する必要があります。

53

次のようなものを作成できます。

@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
  }

}

依存関係はまだ存在していますが、使用されていません。


sprign-boot-starter-web依存関係を安全に削除できますか?現時点では、アプリを非Webにし、この依存関係を削除すると、例外ClassNotFound:javax.servlet.ServleContext
Simon Logic

ソリューション1は非推奨です
ACV

9

最も簡単な解決策。application.propertiesファイル内。前の回答で述べたように、次のプロパティを追加します。

spring.main.web-environment = false

Springブートスターターのバージョン2.0.0の場合、次のプロパティを使用します。

spring.main.web-application-type = none

すべてのプロパティに関するドキュメントについては、次のリンクを使用してください。https//docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html


7

Spring boot v2.1.3.RELEASEの場合、次のプロパティをapplication.propertesに追加するだけです。

spring.main.web-application-type=none

6

このコードを使用します。

SpringApplication application = new SpringApplication(DemoApplication.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);

3

アプリケーションで(org.springframework.web.client.RestTemplateREST呼び出しのように)Web機能が必要だが、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>

3
  • プログラムを通じて:

    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

2

spring.ioサイトの「Getting Started」テンプレートのいずれかを使用したいが、「default」(「gs / spring-boot」)テンプレートに付属しているサーブレット関連のものは必要ない場合は、代わりに、scheduling-tasksテンプレート(pom *にはspring-boot-starterなどが含まれています)を試すことができます。

https://spring.io/guides/gs/scheduling-tasks/

これにより、Spring Bootが提供され、アプリはスタンドアロンとして実行されます(サーブレットやspring-webmvcなどはpomに含まれていません)。これはあなたが望んでいたことです(他の誰かがすでに指摘しているように、JMS固有のものを追加する必要があるかもしれませんが)。

[*私はMavenを使用していますが、Gradleビルドも同様に機能すると想定しています]。


2

あなたのpomファイルへの依存関係を削除する

  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

1

コトリングについては、ここで私が最近使用したものです:


// 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")
    }
}

そして、すべてのブートは通常、使用可能なカスタムコマンドを使用したシェルで終了します。


1

spring-boot-starter依存関係を使用できます。これにはWeb機能はありません。

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
</dependency>

0

上記の@nayun ohに似ていますが、Springの古いバージョンでは、次のコードを使用します。

SpringApplication application = new SpringApplication(DemoApplication.class);
application.setApplicationContextClass(AnnotationConfigApplicationContext.class);
application.run(args);
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.