2016-09-07 180 views
7

我想构建一个简单的SpringBoot应用程序。当我开始运行后,我的春天启动应用程序就立即关机,下面是控制台日志:启动后弹出启动应用程序立即启动

. ____   _   __ _ _ 
/\\/___'_ __ _ _(_)_ __ __ _ \ \ \ \ 
(()\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 
\\/ ___)| |_)| | | | | || (_| | )))) 
    ' |____| .__|_| |_|_| |_\__, |//// 
=========|_|==============|___/=/_/_/_/ 
:: 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.spring[email protected]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.spring[email protected]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 

下面是我的代码:

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

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

我想让服务器保持运行,以便客户端能够响应它。 请建议。

+0

难道您可以尝试使用适当的发行版本?1.4.0.RELEASE用于spring引导启动器? –

+1

您也可以尝试执行命令“mvn dependency:tree”并验证项目中是否存在tomcat依赖项。 –

+0

您是如何运行应用程序? – kuhajeyan

回答

5

我能想到的唯一可能的解释是tomcat嵌入式jar没有包含在dependencies/jar中。既然你已经定义了“spring-boot-starter-web”的依赖关系,它也应该有传递性地拉动嵌入的tomcat依赖关系。但不知何故,它被排除在外。

要尝试的东西。

  1. 执行“MVN依赖:树”,并检查是否tomcat的依赖关系存在,并在“编译”范围
  2. 改变弹簧开机启动版本1.4.0.RELEASE。
+0

执行“mvn dependency:tree”命令之前和之后的tomcat依赖关系 –

+0

更改后spring启动器版本为1.4.0.RELEASE,它开始显示编译错误“无法解析类ComponentScan,组件,配置”。 –

+0

执行“mvn clean install”,以便Maven为新版本下载所需的jar包。如果错误仍然存​​在,您是否可以将导入语句粘贴到您的类中。 –

0

如果我们不使用.RELEASE版本的spring,我们需要在我们的pom.xml文件中添加下面的仓库。

我们可以使用命令“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> 
0

什么解决了我在更新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> 

0

它工作时,我改变了弹簧引导版本如下:

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.4.0.RELEASE</version> 
    </parent> 
3

尝试在位于application.properties文件中添加server.port=someAvailablePortNumber “资源”文件夹。

我也面临同样的问题。尝试了很多pom.xml文件中提出的修改,但没有为我工作。在我的情况下,端口8080不可用,所以它的应用程序无法使用默认端口启动tomcat(即:8080),导致它立即关闭。

更改端口号有助于启动tomcat和应用程序开始工作。希望它有帮助:)

0

在你的主函数“SpringApplication.run(Main.class,args.close());”不应该靠近,它应该像“SpringApplication.run(Main。类,参数);”

例:

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

检查你的日志的配置,也许你想将日志保存到文件夹,无法创建

<appender name="allFileAppender" class="ch.qos.logback.core.rolling.RollingFileAppender"> 
     <file>/var/log/app/all.log</file> 
     ... 
</appender>