Spring Bootアプリが起動直後に常にシャットダウンするのはなぜですか?


164

これが私の最初のSpring Bootコードです。残念ながら、常にシャットダウンします。Webクライアントがブラウザからデータを取得できるように、継続的に実行されることを期待していました。

package hello;
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}


[@localhost initial]$ java -jar build/libs/gs-spring-boot-0.1.0.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::            (v1.0.0.RC4)

2014-03-13 09:20:24.805  INFO 14650 --- [           main] hello.SampleController                   : Starting SampleController on localhost.localdomain with PID 14650 (/home/xxx/dev/gs-spring-boot/initial/build/libs/gs-spring-boot-0.1.0.jar started by xxx)
2014-03-13 09:20:25.002  INFO 14650 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@b9eec: startup date [Thu Mar 13 09:20:24 EDT 2014]; root of context hierarchy
2014-03-13 09:20:28.833  INFO 14650 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Registering beans for JMX exposure on startup
2014-03-13 09:20:30.148  INFO 14650 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2014-03-13 09:20:30.154  INFO 14650 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'requestMappingEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=requestMappingEndpoint]
2014-03-13 09:20:30.316  INFO 14650 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'environmentEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=environmentEndpoint]
2014-03-13 09:20:30.335  INFO 14650 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'healthEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=healthEndpoint]
2014-03-13 09:20:30.351  INFO 14650 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'beansEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=beansEndpoint]
2014-03-13 09:20:30.376  INFO 14650 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'infoEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=infoEndpoint]
2014-03-13 09:20:30.400  INFO 14650 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'metricsEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=metricsEndpoint]
2014-03-13 09:20:30.413  INFO 14650 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'traceEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=traceEndpoint]
2014-03-13 09:20:30.428  INFO 14650 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'dumpEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=dumpEndpoint]
2014-03-13 09:20:30.450  INFO 14650 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'autoConfigurationAuditEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=autoConfigurationAuditEndpoint]
2014-03-13 09:20:30.465  INFO 14650 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'shutdownEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=shutdownEndpoint]
2014-03-13 09:20:30.548  INFO 14650 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Located managed bean 'configurationPropertiesReportEndpoint': registering with JMX server as MBean [org.springframework.boot:type=Endpoint,name=configurationPropertiesReportEndpoint]
2014-03-13 09:20:30.589  INFO 14650 --- [           main] hello.SampleController                   : Started SampleController in 7.396 seconds (JVM running for 9.569)
2014-03-13 09:20:30.608  INFO 14650 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@b9eec: startup date [Thu Mar 13 09:20:24 EDT 2014]; root of context hierarchy
2014-03-13 09:20:30.610  INFO 14650 --- [       Thread-2] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 0
2014-03-13 09:20:30.624  INFO 14650 --- [       Thread-2] o.s.b.a.e.jmx.EndpointMBeanExporter      : Unregistering JMX-exposed beans on shutdown

お知らせ下さい。

ありがとう

PS build.gradleが障害です。

dependencies {
    // tag::jetty[]
    compile("org.springframework.boot:spring-boot-starter-web") {
        **exclude module: "spring-boot-starter-tomcat"**
    }

上の行を太字にすると、すべてが機能します。私のアプリケーションコンテキストは正しいです。おかげでデイブ

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::            (v1.0.0.RC4)

2014-03-13 13:58:08.965  INFO 7307 --- [           main] hello.Application                        : Starting
 Application on  with PID 7307 (/ladev/home/xxx/dev/gs-spring-boot/initial/build/libs/gs-spring-boo
t-0.1.0.jar started by xxx)
2014-03-13 13:58:09.021  INFO 7307 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshi
ng org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@45490eb5: startup
 date [Thu Mar 13 13:58:09 MDT 2014]; root of context hierarchy
2014-03-13 13:58:09.653  INFO 7307 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overridi
ng bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=fal
se; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanNam
e=org.springframework.boot.actuate.autoconfigure.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration;
 factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class
 path resource [org/springframework/boot/actuate/autoconfigure/ErrorMvcAutoConfiguration$WhitelabelErrorView
Configuration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3;
 dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconf
igure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; in
itMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/au
toconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]

MavenまたはGradleを使用していますか?
ロメインモロー2014年

1
あなたApplicationContextは間違ったタイプ(AnnotationConfigApplicationContext)なので、それはウェブアプリではありません。デフォルトではクラスパスに依存する値に設定する必要があるため、間違った方法で設定またはデフォルト設定されているように見えます。多分あなたはapplication.propertiesあなたが示していない、またはENV変数を持っていますか?
Dave Syer 14年

あなたが正しいです。ApplicationContextが正しくありません。application.propertiesファイルがありません。適切なApplicationContextを実行するにはどうすればよいですか?多分Web ApplicationContext?
johnsam 14年

ビルドを単純化してみましょう。spring-boot-starter-webを除くすべての依存関係を削除して--debugから、コマンドラインでwith を実行してログをここに投稿してください。
Dave Syer 14年

Dave、返答が長すぎたので、元の投稿を更新しました。
johnsam 14年

回答:


345

解決策:クラスパスに埋め込みコンテナー(Tomcatなど)がないため、アプリはWebアプリではありません。1つ追加して修正しました。Mavenを使用している場合は、次の場所に追加しpom.xmlます。

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

以下のためのGradlebuild.gradle)、それは次のようになります

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
}

どうやってやるの?
マキシム

2
これは私が欠けていたものでした!
Taobitz

5
単純なJavaアプリケーションがあるのに、なぜこの依存関係を追加する必要があるのでしょうか。
AMAN KUMAR 2017年

この依存関係が最初の依存関係である必要があることを述べてください。
Cosmin Oprea

しかし、そうではありません。クラスパス上にあります。
Dave Syer

31

これを修正する方法は次のとおりです。

  1. pom.xmlファイルでspring-boot-starter-webに依存していないかどうかを確認します。pom.xmlファイルを正しくするには、このリンクstart.spring.ioを使用してください

  2. 上記の依存関係があっても問題が解決しない場合は、組み込みのTomcat jarが存在する可能性が高いです。これを確認するには、デバッグモードでmaven buildを実行します-

mvn spring-boot:run --debug

次のようなメッセージを探します-

[WARNING] error reading /Users/sparrowmac1/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.20/tomcat-embed-core-8.5.20.jar; invalid LOC header (bad signature) [WARNING] error reading /Users/sparrowmac1/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.8.10/jackson-core-2.8.10.jar; invalid LOC header (bad signature)

そのようなメッセージが存在する場合は、ローカルのMavenリポジトリーをパージして、再試行してください-

mvn dependency:purge-local-repository


必要なjarファイルが欠落すると、dispatcherServletは起動しません。したがって、上記の解決策は私にとってうまくいきます。
user3029620 2017

30

同じ問題がありましたが、削除したとき

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

それは再び働き始めました。


このtomcat依存関係を追加した後、アプリケーションが動作し始めました。
georgiana_e 2017

1
@georgiana_e tomcat依存関係を削除した後、アプリケーションが動作し始めました:D何が起こっているのですか?
Daria

:D sam as me @Daria。
RockOnGom 2017年

エラーを修正するために、デフォルトの組み込みコンテナーをJettyに置き換えました。Tomcat JARが壊れているようで、.m2パージが必要でした。
CᴴᴀZ

7

多分それはあなたのコードに適合しないかもしれませんが、私はあなたがこのようなコードスニペットを持っているかどうかを見つけました:

@SpringBootApplication
public class SpringBootApacheKafkaApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootApacheKafkaApplication.class,args).close();
    }
}

次に、close()メソッドを削除します。それで問題は解決しました!多分私はそれで誰かを助けることができます


3

私の場合、メソッドの戻り値が使用されないという静的分析エラーを修正したときに問題が発生しました。

私のApplication.javaの古い作業コードは:

    public static void main(String[] args) {        
      SpringApplication.run(Application.class, args);
    }

この問題を引き起こした新しいコードは次のとおりです。

    public static void main(String[] args) {        
      try (ConfigurableApplicationContext context = 
          SpringApplication.run(Application.class, args)) {
        LOG.trace("context: " + context);
      }
    }

明らかに、try with resourceブロックはアプリケーションの起動後にコンテキストを閉じ、その結果、アプリケーションはステータス0で終了します。これは、snarqube静的分析によって報告されたリソースリークエラーを無視する必要がある場合でした。


3

Gradleを使用して、依存関係ブロック内のbuild.gradle.ktsファイルでこの行を置き換えました

providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")

これとともに

compile("org.springframework.boot:spring-boot-starter-web")

正常に動作します。


2

正解は「Spring Boot Webアプリが起動直後に閉じるのはなぜですか?」starter-tomcatが設定されていないこと、およびIDEを介して設定および実行されている場合は、提供されたスコープをコメント化する必要があります。コマンドを実行している間、スコープは問題を作成しません。なんでかしら。

とにかく、ちょうど私の追加の考えを追加しました。


2

別の可能性、

交換しました

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

そしてそれは問題なく始まった


1

これはSpring Boot 2.0.0で動作します

取り替える

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

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.6</version>
    </dependency>

1

私の場合、私はこの問題を以下のように修正しました:-

  1. 最初に削除しました(Apache) C:\Users\myuserId\.m2\repository\org\apache

  2. pom.xmlファイルに以下の依存関係を追加しました

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  3. リソースファイルに以下の行を追加してデフォルトのソケットを変更しました ..\yourprojectfolder\src\main\resourcesand\application.properties(このファイルを手動で作成しました)

     server.port=8099
     spring.profiles.active=@spring.profiles.active@

    そのため、私は自分のpom.xmlアンダー<build>セクションのブロックの下に追加しました。

      <build>
      .
      .
     <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
       .
       .    
      </build>

私の最終的なpom.xmlファイルは次のようになります

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bhaiti</groupId>
    <artifactId>spring-boot-rest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-rest</name>
    <description>Welcome project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

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

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

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

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

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

    </build>


</project>

0

あなたは春のWebアプリケーションを作成したくない場合は、単に追加し@EnableAsyncたり@EnableScheduling、あなたのスターターに

@EnableAsync
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}

0

私の場合、すでに「spring-boot-starter-web」へのmaven依存関係があり、IDE内から springbootアプリとして実行すると、プロジェクトは自動停止せずに正常に起動しました。ただし、それをK8sに展開すると、アプリはすぐに起動して自動停止します。そこで、SpringBootServletInitializerを拡張するようにメインアプリクラスを変更しました。これにより、自動停止が修正されたようです。

@SpringBootApplication public class MyApp extends SpringBootServletInitializer {  public static void main(String[] args) {
SpringApplication.run(MyApp.class, args);  }}
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.