Spring Boot快速构建项目

目的

如何使用Sprint Boot快速构建项目。

简介

Spring Boot可以简单快速的构建基于Spring的应用系统。

  • 构建独立运行的应用系统,不用额外配置应用服务器,开箱即用。
  • 内置Tomcat,Jetty和Undertow应用服务器,并且不需要部署war文件,默认使用服务器Tomcat
  • Spring自动化配置,提供一站式服务,彻底摆脱XML配置。
  • 项目将会被打包为jar文件,使用java -jar即可运行。

搭建项目

  1. 使用Maven进行项目管理,创建普通Java应用项目,不是Java web项目。本教程基于Spring Boot 1.5.6 Release开发。
    Spring Boot依赖groupIdorg.springframework.boot相关的项目,项目默认都会依赖spring-boot-starter-parent这个项目,该项目提供了构建Spring应用的基本配置和包引用。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    <?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>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

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

    <!-- Additional lines to be added here... -->

    </project>
  2. Spring Boot提供了一站式的配置Starter,Starters里面引用了构建项目必要的依赖和配置,示例代码使用SpringMVC搭建web应用,只需要在pom.xml里面加入spring-boot-starter-web的这个Starter依赖即可。

    1
    2
    3
    4
    5
    6
    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    </dependencies>
  3. 在Maven项目创建目录src/main/java,在该目录下创建Java类Example.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    import org.springframework.boot.*;
    import org.springframework.boot.autoconfigure.*;
    import org.springframework.stereotype.*;
    import org.springframework.web.bind.annotation.*;

    @RestController
    @EnableAutoConfiguration
    public class Example {

    @RequestMapping("/")
    String home() {
    return "Hello World!";
    }

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

    }
  4. 可以在IDE里面运行Main.java,然后在浏览器访问http://localhost:8080,你就会看到应用程序返回Hello World!

  5. 创建可执行jar文件,Spring Boot提供了spring-boot-maven-plugin该插件。

    1
    2
    3
    4
    5
    6
    7
    8
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>
  6. 保存pom.xml文件,执行命令mvn package进行项目打包,你可以在targe目录下找到myproject-0.0.1-SNAPSHOT.jar文件,以下是完整的pom.xml配置。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    <?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>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
    <plugins>
    <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
    </build>

    </project>
  7. 使用命令即可运行打包好的项目java -jar target/myproject-0.0.1-SNAPSHOT.jar,无需繁琐的XML配置即可构建基于SpringMVC的Web应用程序。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    $ java -jar target/myproject-0.0.1-SNAPSHOT.jar

    . ____ _ __ _ _
    /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
    \\/ ___)| |_)| | | | | || (_| | ) ) ) )
    ' |____| .__|_| |_|_| |_\__, | / / / /
    =========|_|==============|___/=/_/_/_/
    :: Spring Boot :: (v1.5.6.RELEASE)
    ....... . . .
    ....... . . . (log output here)
    ....... . . .
    ........ Started Example in 2.536 seconds (JVM running for 2.864)

参考资料
Spring Boot Demo
Spring Boot Reference Guide
Spring Starter