为啥 Spring Boot web app 启动后会立即关闭?
Posted
技术标签:
【中文标题】为啥 Spring Boot web app 启动后会立即关闭?【英文标题】:Why does Spring Boot web app close immediately after starting?为什么 Spring Boot web app 启动后会立即关闭? 【发布时间】:2014-08-04 12:55:50 【问题描述】:使用STS,如果我使用最新的Spring Boot导入"Rest Service"
入门项目并选择"Run As Spring Boot App"
,它会启动,然后立即关闭。
查看调试日志,Spring 出于某种原因决定该项目不是 Web 应用程序,但我不知道为什么。
如果我从 Spring Boot 1.1.1.RELEASE
切换到 1.0.1.RELEASE
,项目将按预期工作,并找到默认的 Tomcat 嵌入式服务器。
我在1.1.1
发行说明中找不到任何暗示配置更改或任何内容的内容?
更新:重现和记录输出的步骤
我正在运行 STS 3.6.0.M1 和 Spring 4.0.5 以及 Spring Boot 1.1.1。我可以通过执行以下操作轻松重现这一点:
-
导入 REST 服务入门应用。
选择“作为 Spring Boot 项目运行”
这是输出:
:: Spring Boot :: (v1.1.1.RELEASE)
2014-06-14 11:08:34.226 INFO 47728 --- [ main] hello.Application : Starting Application on localhost with PID 47728 (/Users/pdrummond/src/sts/gs-rest-service-complete/target/classes started by pdrummond in /Users/pdrummond/src/sts/gs-rest-service-complete)
2014-06-14 11:08:34.257 INFO 47728 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@802b249: startup date [Sat Jun 14 11:08:34 BST 2014]; root of context hierarchy
2014-06-14 11:08:34.680 INFO 47728 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2014-06-14 11:08:34.908 INFO 47728 --- [ main] hello.Application : Started Application in 0.917 seconds (JVM running for 1.351)
2014-06-14 11:08:36.397 INFO 47728 --- [ Thread-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@802b249: startup date [Sat Jun 14 11:08:34 BST 2014]; root of context hierarchy
2014-06-14 11:08:36.398 INFO 47728 --- [ Thread-3] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
如果我编辑 pom 并将 spring-boot-starter-parent
更改为 1.0.1.RELEASE
,它会按预期启动。
更新 2:添加了 --debug 命令的输出
:: Spring Boot :: (v1.1.1.RELEASE)
2014-06-14 19:29:03.814 INFO 986 --- [ main] hello.Application : Starting Application on localhost with PID 986 (/Users/pdrummond/src/sts/gs-rest-service-complete/target/classes started by pdrummond in /Users/pdrummond/src/sts/gs-rest-service-complete)
2014-06-14 19:29:03.816 DEBUG 986 --- [ main] o.s.boot.SpringApplication : Loading source class hello.Application
2014-06-14 19:29:03.838 INFO 986 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@c163956: startup date [Sat Jun 14 19:29:03 BST 2014]; root of context hierarchy
2014-06-14 19:29:04.177 INFO 986 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2014-06-14 19:29:04.346 DEBUG 986 --- [ main] utoConfigurationReportLoggingInitializer :
=========================
AUTO-CONFIGURATION REPORT
=========================
Positive matches:
-----------------
PropertyPlaceholderAutoConfiguration#propertySourcesPlaceholderConfigurer
- @ConditionalOnMissingBean (types: org.springframework.context.support.PropertySourcesPlaceholderConfigurer; SearchStrategy: current) found no beans (OnBeanCondition)
JacksonAutoConfiguration
- @ConditionalOnClass classes found: com.fasterxml.jackson.databind.ObjectMapper (OnClassCondition)
JacksonAutoConfiguration#jacksonObjectMapper
- @ConditionalOnMissingBean (types: com.fasterxml.jackson.databind.ObjectMapper; SearchStrategy: all) found no beans (OnBeanCondition)
JmxAutoConfiguration
- @ConditionalOnClass classes found: org.springframework.jmx.export.MBeanExporter (OnClassCondition)
- SpEL expression on org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration: $spring.jmx.enabled:true (OnExpressionCondition)
JmxAutoConfiguration#mbeanServer
- @ConditionalOnMissingBean (types: javax.management.MBeanServer; SearchStrategy: all) found no beans (OnBeanCondition)
JmxAutoConfiguration#objectNamingStrategy
- @ConditionalOnMissingBean (types: org.springframework.jmx.export.naming.ObjectNamingStrategy; SearchStrategy: all) found no beans (OnBeanCondition)
HttpMessageConvertersAutoConfiguration
- @ConditionalOnClass classes found: org.springframework.http.converter.HttpMessageConverter (OnClassCondition)
HttpMessageConvertersAutoConfiguration#messageConverters
- @ConditionalOnMissingBean (types: org.springframework.boot.autoconfigure.web.HttpMessageConverters; SearchStrategy: all) found no beans (OnBeanCondition)
HttpMessageConvertersAutoConfiguration.ObjectMappers
- @ConditionalOnClass classes found: com.fasterxml.jackson.databind.ObjectMapper (OnClassCondition)
HttpMessageConvertersAutoConfiguration.ObjectMappers#mappingJackson2HttpMessageConverter
- @ConditionalOnMissingBean (types: org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; SearchStrategy: all) found no beans (OnBeanCondition)
Negative matches:
-----------------
MessageSourceAutoConfiguration
- Bundle found for spring.messages.basename: messages (MessageSourceAutoConfiguration.ResourceBundleCondition)
RabbitAutoConfiguration
- required @ConditionalOnClass classes not found: org.springframework.amqp.rabbit.core.RabbitTemplate,com.rabbitmq.client.Channel (OnClassCondition)
AopAutoConfiguration
- required @ConditionalOnClass classes not found: org.aspectj.lang.annotation.Aspect,org.aspectj.lang.reflect.Advice (OnClassCondition)
BatchAutoConfiguration
- required @ConditionalOnClass classes not found: org.springframework.batch.core.launch.JobLauncher,org.springframework.jdbc.core.JdbcOperations (OnClassCondition)
ElasticsearchRepositoriesAutoConfiguration
- required @ConditionalOnClass classes not found: org.elasticsearch.client.Client,org.springframework.data.elasticsearch.repository.ElasticsearchRepository (OnClassCondition)
JpaRepositoriesAutoConfiguration
- required @ConditionalOnClass classes not found: org.springframework.data.jpa.repository.JpaRepository (OnClassCondition)
MongoRepositoriesAutoConfiguration
- required @ConditionalOnClass classes not found: com.mongodb.Mongo,org.springframework.data.mongodb.repository.MongoRepository (OnClassCondition)
RepositoryRestMvcAutoConfiguration
- not a web application (OnWebApplicationCondition)
SolrRepositoriesAutoConfiguration
- required @ConditionalOnClass classes not found: org.apache.solr.client.solrj.SolrServer,org.springframework.data.solr.repository.SolrRepository (OnClassCondition)
ElasticsearchAutoConfiguration
- required @ConditionalOnClass classes not found: org.elasticsearch.client.Client,org.springframework.data.elasticsearch.client.TransportClientFactoryBean,org.springframework.data.elasticsearch.client.NodeClientFactoryBean (OnClassCondition)
ElasticsearchDataAutoConfiguration
- required @ConditionalOnClass classes not found: org.elasticsearch.client.Client,org.springframework.data.elasticsearch.core.ElasticsearchTemplate (OnClassCondition)
FlywayAutoConfiguration
- required @ConditionalOnClass classes not found: org.flywaydb.core.Flyway (OnClassCondition)
FreeMarkerAutoConfiguration
- required @ConditionalOnClass classes not found: freemarker.template.Configuration,org.springframework.ui.freemarker.FreeMarkerConfigurationFactory (OnClassCondition)
GroovyTemplateAutoConfiguration
- required @ConditionalOnClass classes not found: groovy.text.TemplateEngine (OnClassCondition)
HypermediaAutoConfiguration
- required @ConditionalOnClass classes not found: org.springframework.hateoas.Resource (OnClassCondition)
IntegrationAutoConfiguration
- required @ConditionalOnClass classes not found: org.springframework.integration.config.EnableIntegration (OnClassCondition)
JacksonAutoConfiguration.JodaModuleAutoConfiguration
- required @ConditionalOnClass classes not found: com.fasterxml.jackson.datatype.joda.JodaModule (OnClassCondition)
JacksonAutoConfiguration.Jsr310ModuleAutoConfiguration
- Required JVM version 1.8 or newer found 1.6 (OnJavaCondition)
DataSourceAutoConfiguration
- required @ConditionalOnClass classes not found: org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType (OnClassCondition)
DataSourceTransactionManagerAutoConfiguration
- required @ConditionalOnClass classes not found: org.springframework.jdbc.core.JdbcTemplate,org.springframework.transaction.PlatformTransactionManager (OnClassCondition)
JmsAutoConfiguration
- required @ConditionalOnClass classes not found: org.springframework.jms.core.JmsTemplate (OnClassCondition)
ActiveMQAutoConfiguration
- required @ConditionalOnClass classes not found: javax.jms.ConnectionFactory,org.apache.activemq.ActiveMQConnectionFactory (OnClassCondition)
HornetQAutoConfiguration
- required @ConditionalOnClass classes not found: javax.jms.ConnectionFactory,org.hornetq.api.jms.HornetQJMSClient (OnClassCondition)
JmxAutoConfiguration#mbeanExporter
- @ConditionalOnMissingBean (types: org.springframework.jmx.export.MBeanExporter; SearchStrategy: current) found the following [mbeanExporter] (OnBeanCondition)
LiquibaseAutoConfiguration
- required @ConditionalOnClass classes not found: liquibase.integration.spring.SpringLiquibase (OnClassCondition)
DeviceDelegatingViewResolverAutoConfiguration
- not a web application (OnWebApplicationCondition)
DeviceResolverAutoConfiguration
- required @ConditionalOnClass classes not found: org.springframework.mobile.device.DeviceResolverHandlerInterceptor,org.springframework.mobile.device.DeviceHandlerMethodArgumentResolver (OnClassCondition)
SitePreferenceAutoConfiguration
- required @ConditionalOnClass classes not found: org.springframework.mobile.device.site.SitePreferenceHandlerInterceptor,org.springframework.mobile.device.site.SitePreferenceHandlerMethodArgumentResolver (OnClassCondition)
MongoAutoConfiguration
- required @ConditionalOnClass classes not found: com.mongodb.Mongo (OnClassCondition)
MongoDataAutoConfiguration
- required @ConditionalOnClass classes not found: com.mongodb.Mongo,org.springframework.data.mongodb.core.MongoTemplate (OnClassCondition)
HibernateJpaAutoConfiguration
- did not find HibernateEntityManager class (HibernateJpaAutoConfiguration.HibernateEntityManagerCondition)
ReactorAutoConfiguration
- required @ConditionalOnClass classes not found: reactor.spring.context.config.EnableReactor (OnClassCondition)
RedisAutoConfiguration
- required @ConditionalOnClass classes not found: org.springframework.data.redis.connection.jedis.JedisConnection,org.springframework.data.redis.core.RedisOperations,redis.clients.jedis.Jedis (OnClassCondition)
FallbackWebSecurityAutoConfiguration
- SpEL expression on org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration: !$security.basic.enabled:true (OnExpressionCondition)
SecurityAutoConfiguration
- required @ConditionalOnClass classes not found: org.springframework.security.authentication.AuthenticationManager (OnClassCondition)
FacebookAutoConfiguration
- required @ConditionalOnClass classes not found: org.springframework.social.facebook.connect.FacebookConnectionFactory (OnClassCondition)
LinkedInAutoConfiguration
- required @ConditionalOnClass classes not found: org.springframework.social.linkedin.connect.LinkedInConnectionFactory (OnClassCondition)
SocialWebAutoConfiguration
- required @ConditionalOnClass classes not found: org.springframework.social.connect.web.ConnectController,org.springframework.social.config.annotation.SocialConfigurerAdapter (OnClassCondition)
TwitterAutoConfiguration
- required @ConditionalOnClass classes not found: org.springframework.social.twitter.connect.TwitterConnectionFactory (OnClassCondition)
SolrAutoConfiguration
- required @ConditionalOnClass classes not found: org.apache.solr.client.solrj.SolrServer (OnClassCondition)
ThymeleafAutoConfiguration
- required @ConditionalOnClass classes not found: org.thymeleaf.spring4.SpringTemplateEngine (OnClassCondition)
VelocityAutoConfiguration
- required @ConditionalOnClass classes not found: org.apache.velocity.app.VelocityEngine,org.springframework.ui.velocity.VelocityEngineFactory (OnClassCondition)
DispatcherServletAutoConfiguration
- not a web application (OnWebApplicationCondition)
EmbeddedServletContainerAutoConfiguration
- not a web application (OnWebApplicationCondition)
ErrorMvcAutoConfiguration
- required @ConditionalOnClass classes not found: javax.servlet.Servlet,org.springframework.web.servlet.DispatcherServlet (OnClassCondition)
MultipartAutoConfiguration
- required @ConditionalOnClass classes not found: javax.servlet.Servlet,javax.servlet.MultipartConfigElement (OnClassCondition)
ServerPropertiesAutoConfiguration
- not a web application (OnWebApplicationCondition)
WebMvcAutoConfiguration
- not a web application (OnWebApplicationCondition)
WebSocketAutoConfiguration
- required @ConditionalOnClass classes not found: javax.servlet.Servlet,org.apache.catalina.startup.Tomcat,org.springframework.web.socket.WebSocketHandler,org.apache.tomcat.websocket.server.WsSci (OnClassCondition)
2014-06-14 19:29:04.348 INFO 986 --- [ main] hello.Application : Started Application in 0.73 seconds (JVM running for 1.085)
2014-06-14 19:29:06.378 INFO 986 --- [ Thread-3] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@c163956: startup date [Sat Jun 14 19:29:03 BST 2014]; root of context hierarchy
2014-06-14 19:29:06.379 INFO 986 --- [ Thread-3] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
【问题讨论】:
将日志或输出粘贴到您的问题中怎么样? 我认为这不是必需的,因为它发生在一个易于重现的标准示例应用程序中(至少对我而言),但无论如何我都会添加它,以防它提供某种线索,我没看到。 为我工作。您是导入为 Maven 还是 Gradle 项目(请尝试其他)? 我一开始使用的是 Gradle,然后尝试了 Maven,它仍然发生。感谢您尝试。如果它对您有用,则可能是我的设置有问题,因此我将对其进行更详细的研究。你知道Spring Boot在使用spring-boot-starter-web
时是否会不默认使用Tomcat?这似乎是正在发生的事情,但调试日志并没有告诉我为什么 Boot 认为它不是网络应用程序!
您可以使用“--debug”(或 application.properties 中的“debug=true”)启动应用程序。然后你会看到所有的自动配置决定。如果没有tomcat,你的类路径一定是乱七八糟的。
【参考方案1】:
我也有同样的问题。为了使它工作,我必须调整 pom.xml 并注释掉 spring-boot-starter-tomcat 工件的“提供”范围并更新依赖项。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--<scope>provided</scope>-->
</dependency>
现在它按预期启动了!感谢其他一些让我走上正轨的答案。
【讨论】:
这里也是 gradle 解决方案:` compile('org.springframework.boot:spring-boot-starter-tomcat') // providedRuntime('org.springframework.boot:spring-boot-starter -tomcat')` 它有什么作用?这是否意味着我必须自己在我的机器上安装tomcat?我问这个,因为有一天你需要为 web 服务器打包它。 这也解决了我的问题。 SpringBootApplication main 在从 IDE 启动时关闭,但在使用 spring-boot 插件运行时按预期工作。请考虑选择此答案作为正确答案。 从start.spring.io 生成项目后,我从依赖项堆栈中删除了以下行:providedRuntime('org.springframework.boot:spring-boot-starter-tomcat') 现在它按预期开始了。感谢您的提示!【参考方案2】:执行打包阶段时检查 mvn 日志,并确保将 tomcat-embed-core jar 分配到您的应用程序 jar 中。
【讨论】:
谢谢哥们...你拯救了我的一天。 :)【参考方案3】:我遇到了同样的问题,我的 spring-boot 应用程序立即关闭,没有任何启动的 tomcat 痕迹,尽管我的 pom.xml 中包含依赖项<artifactId>spring-boot-starter-web</artifactId>
。这表明 spring-boot 没有将我的应用程序识别为 Web 应用程序。
尝试使用 mvn clean install
实用程序再次清理和打包我的应用程序,以验证是否将 tomcat 库打包到我的应用程序二进制/可部署中。发现以下问题(查找以 '[WARNING]' 开头的行):
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ exam-launcher ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to C:\XXXXX\XXXXXX\XXXX_Workspace\my-app\target\classes
[WARNING] error reading c:\.m2\org\apache\tomcat\embed\tomcat-embed-core\8.5.14\tomcat-embed-core-8.5.14.jar; invalid LOC header (bad signature)
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ exam-launcher ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Chaithu\Work_Related\Eclipse_Neon_Workspace\exam-portal-server\src\test\resources
我在本地 repo 上的 tomcat 库已损坏(由于某些未知原因),因为它没有包含在 maven 包中。从我本地的maven repo中删除了相应的文件夹,让maven重新下载。现在它就像一个魅力!
【讨论】:
【参考方案4】:只需添加
spring.main.web-environment=true
在 application.properties 中并重新启动。
【讨论】:
您为我节省了很多时间,但是该属性已被弃用。使用 web-application-type: 代替。【参考方案5】:所有答案都表明它缺少tomcat。就我而言,它在我的 pom 中被标记为
<scope>provided</scope>
我认为我这样做了,因为稍后我需要在另一个应用服务器上运行我的应用程序。所以是的,这对我来说就是同一问题的另一个版本。
【讨论】:
是的,如果不是很清楚,请保留 tomcat(或在我的情况下为码头)依赖项,但请确保从依赖项声明中删除提供的范围。 我也将打包定义为war,想部署在App服务器上。但出于测试和调试目的,我想使用主类运行它。我可以删除我的情况下的范围吗??【参考方案6】:我找到了发生这种情况的原因。 我的是我没有包含任何引入 tomcat 运行时的库。 我包含了 spring-web 依赖项和 spring-boot-starter-data-jpa(如下所示),但是这些包都不会为这个应用程序带来 tomcat 运行时运行。所以,它是自动关机。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-web</artifactId>
</dependency>
解决方案是将 spring-web 更改为 spring-boot-starter-web(如下所示)。这个库引入了 tomcat 库并启动了我的应用程序。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
【讨论】:
【参考方案7】:我刚遇到同样的问题,花了一段时间寻找解决方案。我知道这个问题现在已经有几年了,但这可能会在将来节省其他人的时间。
我在运行mvn clean install
时收到此警告消息:
[WARNING] error reading /Users/fotouhm/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/8.5.16/tomcat-embed-core-8.5.16.jar; zip file is empty
由于某种原因,该文件已损坏。只需转到下面的链接并替换它。
http://central.maven.org/maven2/org/apache/tomcat/embed/tomcat-embed-core/8.5.16/tomcat-embed-core-8.5.16.jar
【讨论】:
【参考方案8】:此外,您应该检查您的应用中是否至少有一个休息控制器 (@RestController) 并确保您使用 spring-boot-starter-tomcat
进行编译
providedRuntime ('org.springframework.boot:spring-boot-starter-tomcat')
【讨论】:
【参考方案9】:在我的例子中,log4j.properties 丢失了。
【讨论】:
【参考方案10】:就我而言,application.properties
中有语法错误:
application.password.numberOfLoginAttempts=5;
最后的分号导致 Spring 以错误代码 0 静默关闭
【讨论】:
以上是关于为啥 Spring Boot web app 启动后会立即关闭?的主要内容,如果未能解决你的问题,请参考以下文章
为啥 Spring Boot 应用程序 pom 同时需要 spring-boot-starter-parent 和 spring-boot-starter-web?
Spring boot Batch 的启动原理- Configuration