2014-03-13 39 views
60

這是我的第一個Spring Boot代碼。不幸的是,它總是關閉。我期待它不斷運行,以便我的Web客戶端可以從瀏覽器獲取一些數據。爲什麼我的Spring Boot App在啓動後總是立即關閉?

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.spring[email protected]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.spring[email protected]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 

請指教。

謝謝

P.S. 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[email protected]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]] 
+0

你使用的是Maven還是Gradle? –

+1

你的'ApplicationContext'是錯誤的類型('AnnotationConfigApplicationContext'),所以它不是一個web應用程序。它應該默認爲一個取決於你的類路徑的值,所以它看起來像是被設置或默認的方式是錯誤的。也許你有一個'application.properties'或一個ENV var你沒有顯示? –

+0

你說得對。我的ApplicationContext不正確。我沒有一個application.properties文件。我如何使它運行正確的ApplicationContext?也許一個Web ApplicationContext? – johnsam

回答

157

解決方案:該應用程序不是一個Web應用程序,因爲它沒有在類路徑中的嵌入式容器(例如Tomcat)。添加一個修復它。如果您正在使用Maven的,然後在pom.xml補充一點:

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

對於搖籃build.gradle),它看起來像

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

你是如何做到這一點? – maxime

+0

優秀這是我失蹤! –

+0

你節省了我的一天。萬分感謝 !! – varunkr

1

我認爲正確的答案是在Why does Spring Boot web app close immediately after starting?有關starter- tomcat沒有被設置,並且如果通過IDE設置並運行,所提供的範圍應該被註釋掉。範圍在通過命令運行時不會產生問題。我想知道爲什麼。

無論如何只是添加了我的額外想法。

15

我有同樣的問題,但是當我刪除

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

它開始再次合作。

+0

我添加了這個tomcat依賴項後,我的應用程序開始工作。 –

+0

你已經拯救了我的生命。 – mainframer

+0

@georgiana_e我的應用程序在我刪除tomcat依賴關係後開始工作:D發生了什麼? – Daria

2

在我的情況下,當我修復了一個靜態分析錯誤,沒有使用方法的返回值時,引入了這個問題。在我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); 
     } 
    } 

顯然,隨着資源塊的請嘗試啓動後會關閉上下文應用程序將導致應用程序以狀態0退出。這是由snarqube靜態分析報告的資源泄漏錯誤應忽略的情況。

11

這裏是你如何解決這個問題:

  1. 檢查,如果你沒有對彈簧引導啓動,網絡依賴在你的pom.xml文件做。爲了讓你的pom.xml文件正確,使用這個鏈接start.spring.io

  2. 如果你有上述的依賴關係,但仍然面臨這個問題,你的嵌入式tomcat極有可能存在。 爲了證實這一點,運行Maven構建在調試模式 -

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

+0

一旦需要的罐子丟失,dispatcherServlet無法啓動。所以上面的解決方案適用於我。 – user3029620

0

也許它並不適合你的代碼,但我發現,如果你有一段代碼是這樣的:

@SpringBootApplication 
public class SpringBootApacheKafkaApplication { 

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

然後刪除close()方法。這解決了我的問題!也許我可以幫助那個人

相關問題