这是我的第一个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

请建议。

谢谢

注:构建。是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]]

解决方案:应用程序不是一个webapp,因为它没有在类路径上嵌入容器(例如Tomcat)。增加一个可以解决这个问题。如果你正在使用Maven,那么在pom.xml中添加这个:

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

对于Gradle (build.gradle)来说

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

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

不管怎样,我只是补充了我的想法。


我也有同样的问题,但当我切除

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

它又开始工作了。


在我的案例中,当我修复一个静态分析错误时,没有使用方法的返回值,问题就出现了。

在我的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资源块将在启动应用程序后关闭上下文,这将导致应用程序以状态0退出。这是一个由snarqube静态分析报告的资源泄漏错误应该被忽略的情况。


以下是解决方法:

检查pom.xml文件中是否没有spring-boot-start -web依赖项。要获得正确的pom.xml文件,请使用这个链接start.spring.io 如果您有上述依赖关系,但仍然面临这个问题,则很可能存在您的嵌入式tomcat jar。 要确认这一点,请在调试模式下运行maven build -

MVN spring-boot:run——debug

寻找像-这样的信息

读取错误 /用户/ sparrowmac1 / .m2 /仓库/ org/apache/tomcat/embed/tomcat-embed-core/8.5.20/tomcat-embed-core-8.5.20.jar; 无效的LOC报头(坏签名)[警告]读取错误 /用户/ sparrowmac1 / .m2 /仓库/ com/fasterxml/jackson/core/jackson-core/2.8.10/jackson-core-2.8.10.jar; 无效的LOC头(错误的签名)

如果存在此类消息,请清除本地maven repo并重试-

mvn依赖性:purge-local-repository


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

@SpringBootApplication
public class SpringBootApacheKafkaApplication {

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

然后只需删除close()方法。这解决了我的问题!也许我能帮上忙


这适用于spring boot 2.0.0

取代

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

with

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

在我的情况下,我修复了这个问题如下:-

First I removed (apache) C:\Users\myuserId\.m2\repository\org\apache I added below dependencies in my pom.xml file <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> I have changed the default socket by adding below lines in resource file ..\yourprojectfolder\src\main\resourcesand\application.properties(I manually created this file) server.port=8099 spring.profiles.active=@spring.profiles.active@ for that I have added below block in my pom.xml under <build> section. <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>

只是另一种可能性,

我更换了

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

一开始没有任何问题


如果你不想让你的spring成为一个web应用程序,那么只需添加@EnableAsync或@ enablesscheduling到你的Starter

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

}

在gradle中,我在build.gradle.kts文件中替换了这一行

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

用这个

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

工作正常。


在我的情况下,我已经有maven依赖'spring-boot-starter-web'和项目将开始很好,没有自动停止时,我运行它作为springboot应用程序从IDE。然而,当我把它部署到K8s时,应用程序会立即启动和自动停止。所以我修改了我的主应用类来扩展SpringBootServletInitializer,这似乎已经修复了自动停止。

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

我检查了这里和其他地方的答案,仍然有问题。它变成了(像操作),我在kubernetes中运行,应用程序很好,但kubernetes有一个问题。

我忘记让我的应用程序启动基于我的端口环境变量。所以,kubernetes/helm告诉它在8443上运行,并对其进行活体探测,但它在我的application.properties中的其他端口(如8123)上运行。

kubernetes事件(get事件)显示活性探测失败,所以kubernetes每30秒左右就会杀死pod/应用程序。


我也遇到过类似的问题。我只有以下入门网页包。

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

但这还不够。您还需要添加一个父类以获得其他所需的依赖项。

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

如果您有一个注入了循环弹簧的依赖项,它将在没有警告的情况下失败,这取决于日志级别和其他一些因素。

类A注入类B,类B注入类A,在这个特殊情况下,通过构造函数。


我用SPring boot开发工具在IntelliJIdea中初始化了一个新的SPring启动项目,但是在pom.xml中我只有依赖项

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

您还需要神器spring-boot-starter-web。只需将这个依赖项添加到pom.xml

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

我的应用程序是Spring启动批处理,并在应用程序的下面注释行。属性解决了问题

spring.main.web-application-type=none

我也有过这样的问题,就我而言,这是由一个简单的笨拙错误引起的:我的班级有:

@SpringBootApplication
public class MyApplication implements ApplicationRunner {

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

public void run(ApplicationArguments args) throws Exception {

}

}

我删除了“implements ApplicationRunner”和run方法(由于复制粘贴错误而存在),然后它就工作了


删除Maven m2文件夹(在linux ~/.m2上)并重新构建应用程序


如果你正在使用Java 9 JPMS,添加spring-boot-start -web作为依赖是不够的,因为tomcat-embed lib在运行时不可用,spring引导不会将应用程序检测为web应用程序。

要解决这个问题,通过以下指令添加到主应用程序的模块-info.java中,使tomcat嵌入可用

 requires org.apache.tomcat.embed.core;
 

在我的情况下,当我试图将5000条记录插入数据库时发生了问题(它可以处理更少的记录) 对我有效的方法:取消pom.xml文件中devtools依赖项的注释:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

以下是解决方法:

检查pom.xml文件中是否没有spring-boot-start -web依赖项。要获得正确的pom.xml文件,请使用这个链接start.spring.io

如果您有上述依赖关系,但仍然面临这个问题,则很可能存在您的嵌入式tomcat jar。要确认这一点,请在调试模式下运行maven build -

mvn spring-boot:run --debug

如果你想查看详细的日志,那么你可以使用下面的命令调试它

 java  -Ddebug -jar .\app.jar

对我来说,这是由于以下例外

 org.apache.logging.log4j.core.appender.AppenderLoggingException: java.lang.NoSuchMethodError: org.apache.logging.log4j.util.StackLocatorUtil.getCurrentStackTrace()Ljava/ut