Springboot应用不能用main方法启动

使用mvn spring-boot:run可以正常启动
但是通过main方法(即intellij IDEA的小三角启动)
进程会直接关掉:
log信息如下:

 :: Spring Boot ::        (v1.4.2.RELEASE)

2016-12-22 14:31:56.663  INFO 6525 --- [           main] c.t.SpringbootMybatisApplication         : Starting SpringbootMybatisApplication on lijiechudeMacBook-Pro.local with PID 6525 (/Users/lijiechu/Documents/playonthecloud-backend/target/classes started by lijiechu in /Users/lijiechu/Documents/playonthecloud-backend)
2016-12-22 14:31:56.665  INFO 6525 --- [           main] c.t.SpringbootMybatisApplication         : No active profile set, falling back to default profiles: default
2016-12-22 14:31:56.714  INFO 6525 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@243c4f91: startup date [Thu Dec 22 14:31:56 CST 2016]; root of context hierarchy
2016-12-22 14:31:57.867  INFO 6525 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2016-12-22 14:31:57.878  INFO 6525 --- [           main] c.t.SpringbootMybatisApplication         : Started SpringbootMybatisApplication in 2.076 seconds (JVM running for 2.403)
2016-12-22 14:31:57.879  INFO 6525 --- [       Thread-1] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@243c4f91: startup date [Thu Dec 22 14:31:56 CST 2016]; root of context hierarchy
2016-12-22 14:31:57.880  INFO 6525 --- [       Thread-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

Process finished with exit code 0

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.tongji409</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>springboot-mybatis</name>
    <description>Intergrated Spring-Boot Framework with mybatis</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.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.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--alibaba fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.41</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

同时也配置了以下这个类:

package com.tongji409;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(SpringbootMybatisApplication.class);
    }

}
阅读 19.8k
7 个回答

main方法的类上有个注解!注解!

你应该在类上加上 @SpringBootApplication 注解并且写一个main 方法 内容:

 SpringApplication.run(ServletInitializer.cless,args);

我遇到了同样的问题。
只需要把spring-boot-starter-tomcat这个包去掉就可以了。springboot有内置的tomcat

@任意666

依赖里面有一个tomcat的依赖
providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
providedRuntime 只会在runtime才会依赖进来,改成编译时依赖就好了
改成下面这样:
compile('org.springframework.boot:spring-boot-starter-tomcat')
当然,还至少添加一个controller才可以
maven的 应该去掉依赖的scop就可以了吧

新手上路,请多包涵

我遇到了同样的问题,将tomcat依赖项的scope注释掉就可以了
`<dependency>

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

</dependency>`

你好 请问你解决了吗 我也出现这个问题了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏