为什么我的Spring Boot App总是closures

这是我的第一个Spring启动代码。 不幸的是,它总是关机。 我期待它会不断运行,以便我的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] scaAnnotationConfigApplicationContext : 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] osbaejmx.EndpointMBeanExporter : Registering beans for JMX exposure on startup 2014-03-13 09:20:30.148 INFO 14650 --- [ main] oscsupport.DefaultLifecycleProcessor : Starting beans in phase 0 2014-03-13 09:20:30.154 INFO 14650 --- [ main] osbaejmx.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] osbaejmx.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] osbaejmx.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] osbaejmx.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] osbaejmx.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] osbaejmx.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] osbaejmx.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] osbaejmx.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] osbaejmx.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] osbaejmx.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] osbaejmx.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] scaAnnotationConfigApplicationContext : 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] oscsupport.DefaultLifecycleProcessor : Stopping beans in phase 0 2014-03-13 09:20:30.624 INFO 14650 --- [ Thread-2] osbaejmx.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] osbfsDefaultListableBeanFactory : 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]] 

解决scheme:该应用程序不是一个Web应用程序,因为它没有在类path中的embedded式容器(例如Tomcat)。 添加一个固定它。 如果您正在使用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' } 

如果您使用的是Maven,那么更新pom.xml中的依赖关系

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

我有同样的问题,但当我删除

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

它再次开始工作。

这里是你如何解决它:

  1. 检查你的pom.xml文件是否依赖于spring-boot-starter-web。 为了让你的pom.xml文件正确,使用这个链接start.spring.io

  2. 如果你有上面的依赖关系,但仍然面临这个问题,你的embedded式tomcatjar很有可能存在。 为了确认这一点,在debugging模式下运行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

在我的情况下,当我修复了一个静态分析错误,没有使用方法的返回值时,引入了这个问题。

旧的工作代码在我的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); } } 

显然,使用资源块的尝试将在启动应用程序后closures上下文,这将导致应用程序退出状态0.这是一个由snarqube静态分析报告的资源泄漏错误应该被忽略的情况。

我认为正确的答案是为什么Spring Boot Web应用程序启动后立即closures? 关于starter-tomcat没有设置,如果通过IDE设置并运行,提供的范围应该被注释掉。 范围在运行命令时不会产生问题。 我想知道为什么。

反正只是添加了我的额外想法。

也许它不适合你的代码,但我发现,如果你有这样的代码片段:

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

那么只需删除close()方法。 这是解决我的问题! 也许我可以帮助那个人