java Spring boot 应用程序启动后立即关闭
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39363570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot application shutdown immediate after starting
提问by RaT
I am trying to build a simple SpringBoot application. When I run my spring boot application it shutdown immediate after starting, below is the console log:
我正在尝试构建一个简单的 SpringBoot 应用程序。当我运行 spring boot 应用程序时,它在启动后立即关闭,下面是控制台日志:
. ____ _ __ _ _
/\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.1.BUILD-SNAPSHOT)
2016-09-06 18:02:35.152 INFO 22216 --- [ main] com.example.SpringBootDemo1Application : Starting SpringBootDemo1Application on IN-FMCN882 with PID 22216 (E:\workspace\springBoot\SpringBootDemo1\target\classes started by Rahul.Tyagi in E:\workspace\springBoot\SpringBootDemo1)
2016-09-06 18:02:35.158 INFO 22216 --- [ main] com.example.SpringBootDemo1Application : No active profile set, falling back to default profiles: default
2016-09-06 18:02:35.244 INFO 22216 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@14dd9eb7: startup date [Tue Sep 06 18:02:35 IST 2016]; root of context hierarchy
2016-09-06 18:02:36.527 INFO 22216 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-09-06 18:02:36.546 INFO 22216 --- [ main] com.example.SpringBootDemo1Application : Started SpringBootDemo1Application in 1.781 seconds (JVM running for 2.376)
2016-09-06 18:02:36.548 INFO 22216 --- [ Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@14dd9eb7: startup date [Tue Sep 06 18:02:35 IST 2016]; root of context hierarchy
2016-09-06 18:02:36.550 INFO 22216 --- [ Thread-1] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
Below is my code:
下面是我的代码:
SpringBootDemo1Application.java
SpringBootDemo1Application.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Configuration
@EnableAutoConfiguration
@ComponentScan
@Controller
public class SpringBootDemo1Application {
@ResponseBody
@RequestMapping("/")
public String entry(){
return "My spring boot application";
}
public static void main(String[] args) {
SpringApplication.run(SpringBootDemo1Application.class, args);
}
}
pom.xml
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.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.BUILD-SNAPSHOT</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I want to keep the server running so that client can hit it for response. Please Suggest.
我想让服务器保持运行,以便客户端可以点击它进行响应。请建议。
回答by Ameen.M
The only possible explanation i can think of is that tomcat embedded jar is not included in the dependencies/jar. Since you have already defined "spring-boot-starter-web" dependency, it should have transitively pulled the embedded tomcat dependencies as well. But somehow it is excluded.
我能想到的唯一可能的解释是,tomcat 嵌入式 jar 不包含在依赖项/jar 中。由于您已经定义了“spring-boot-starter-web”依赖项,它也应该传递性地拉取嵌入的 tomcat 依赖项。但不知何故它被排除在外。
Things to try out.
可以尝试的东西。
- Execute "mvn dependency:tree" and check whether the tomcat dependencies exist and in "compile" scope
- Change the spring boot starter version to 1.4.0.RELEASE.
- 执行“mvn dependency:tree”并检查tomcat依赖是否存在并在“compile”范围内
- 将 spring boot starter 版本更改为 1.4.0.RELEASE。
回答by Enayat
It worked when I changed spring boot version as follows:
当我按如下方式更改 spring boot 版本时,它起作用了:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
回答by Maria
Try adding server.port=someAvailablePortNumber
in the application.properties
file located in "resources" folder.
试着增加server.port=someAvailablePortNumber
了在application.properties
位于“资源”文件夹中的文件。
I was also facing same problem. tried a lot of changes suggested in pom.xml file but nothing worked for me. In my case the port 8080 wasn't available so it application wasn't able to start tomcat using default port(i.e.: 8080) causing it to shutdown immediately.
我也面临同样的问题。尝试了 pom.xml 文件中建议的很多更改,但没有任何效果对我有用。在我的情况下,端口 8080 不可用,因此应用程序无法使用默认端口(即:8080)启动 tomcat,导致它立即关闭。
Changing the port number helped to start tomcat and application started working. Hope it helps :)
更改端口号有助于启动 tomcat 和应用程序开始工作。希望能帮助到你 :)
回答by Nehemiah Clement
In your main function "SpringApplication.run(Main.class, args.close());" should not be in close , it shuld be like "SpringApplication.run(Main.class, args);"
在你的主函数“SpringApplication.run(Main.class, args.close());”中 不应该在 close ,它应该像“SpringApplication.run(Main.class, args);”
Example :
例子 :
@SpringBootApplication
public class Main{
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
回答by arctica
Check your log config, maybe you're trying to save logs into the folder, that can't be created.
检查您的日志配置,也许您正在尝试将日志保存到无法创建的文件夹中。
<appender name="allFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/var/log/app/all.log</file>
...
</appender>
回答by y119777
i encountered the same problem, my os is win7, my ide is eclipse, after i changed spring boot version to 1.5.7.release, erverything is ok.
我遇到了同样的问题,我的操作系统是win7,我的ide是eclipse,在我将spring boot版本更改为1.5.7.release后,一切正常。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
i have tried spring boot 2.0.2.release, 1.5.13.release, 1.5.1.release, they all cant run on my win7 os using eclipse, but they can run my ubuntu16.04 using command line and mvn compile with the same code.
我已经尝试过 spring boot 2.0.2.release、1.5.13.release、1.5.1.release,它们都不能使用 eclipse 在我的 win7 操作系统上运行,但它们可以使用命令行运行我的 ubuntu16.04,并使用 mvn 编译相同的代码。
回答by KC Baltz
Clearing my local Maven repo solved this issue for me. Easiest way is to remove ~/.m2/repository
清除我的本地 Maven 仓库为我解决了这个问题。最简单的方法就是去掉~/.m2/repository
回答by SUBHASH KUMAR RAY
removed spring-boot-starter-tomcat dependency and it worked.
删除了 spring-boot-starter-tomcat 依赖项并且它起作用了。
回答by RaT
If we are not using a .RELEASE version of spring we need to add below repositories in our pom.xml file.
如果我们没有使用 .RELEASE 版本的 spring,我们需要在我们的 pom.xml 文件中添加以下存储库。
And we can use command "mvn spring-boot:run" to run our spring application from command line.
我们可以使用命令“mvn spring-boot:run”从命令行运行我们的 spring 应用程序。
<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
回答by Dror
What solved it for me was updating the "parent" reference in the pom.xml.
为我解决的是更新 pom.xml 中的“父”引用。
this is my working pom.xml :
这是我的工作 pom.xml :
<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.boot</groupId>
<artifactId>project-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<name>project-boot</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>