为什么Spring Boot Web应用程序在启动后立即关闭?

时间:2014-06-13 21:46:16

标签: spring spring-boot

使用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。通过执行以下操作,我可以轻松地重现这一点:

  1. 导入REST服务入门应用程序。
  2. 选择"作为Spring Boot Project运行"
  3. 这是输出:

    :: 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
    
  4. 如果我编辑pom并将spring-boot-starter-parent更改为1.0.1.RELEASE,它会按预期启动。

  5. 更新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
    

10 个答案:

答案 0 :(得分:22)

我有同样的问题。为了使它工作,我不得不调整pom.xml并注释掉spring-boot-starter-tomcat工件的“提供”范围并更新依赖项。

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

现在按预期启动!感谢其他一些让我走上正轨的答案。

答案 1 :(得分:7)

执行包阶段时检查mvn日志,并确保将tomcat-embed-core jar分配到您的app jar中。

答案 2 :(得分:5)

我有同样的问题,我的spring-boot应用程序立即关闭,没有任何tomcat启动的痕迹,尽管我的pom.xml中包含依赖项<artifactId>spring-boot-starter-web</artifactId>。这表明spring-boot并没有将我的应用识别为Web应用程序。

尝试使用mvn clean install实用程序再次清理和打包我的应用程序,以验证tomcat库是否已打包到我的应用程序二进制文件/可部署中。找到以下问题(查找以&#39; [警告]&#39;开头的行:

[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

我在我的本地仓库上的tomcat库已经损坏(由于某些未知原因),因为它没有被包含在maven包中。从我当地的maven repo中删除了相应的文件夹,让maven新下载。它现在就像一个魅力!!

答案 3 :(得分:3)

添加

 spring.main.web-environment=true

在application.properties中并重新启动。

答案 4 :(得分:2)

我发现了这种情况发生的原因。 我的是,我没有包含任何带来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>

解决方案是将弹簧网改为弹簧靴 - 起动器 - 网(如下所示)。这个库引入了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>

答案 5 :(得分:1)

我遇到了同样的问题,花了一些时间寻找解决方案。我知道这个问题现在已经有几年了,但这可能会节省别人的未来时间。

我在运行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

答案 6 :(得分:0)

此外,您应该检查您的应用程序中至少有一个休息控制器(@RestController),并确保使用spring-boot-starter-tomcat进行编译

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

答案 7 :(得分:0)

在我的情况下,缺少log4j.properties。

答案 8 :(得分:0)

所有答案都表明tomcat缺失了。在我的情况下,它被标记在我的pom中 <scope>provided</scope>我认为我做了它,因为我需要稍后在另一台App服务器上运行我的应用程序。所以是的,这对我来说只是问题的另一个版本。

答案 9 :(得分:0)

就我而言,我在application.properties中遇到语法错误:

application.password.numberOfLoginAttempts=5;

最后的分号导致Spring静默关闭,错误代码为0