项目地址:http://projects.spring.io/spring-boot/
Spring Boot使开发独立的,产品级别的基于Spring的应用变得非常简单,你只需"just run"。 我们为Spring平台及第三方库提 供开箱即用的设置,这样你就可以有条不紊地开始。多数Spring Boot应用需要很少的Spring配置。
1:创建独立的spring应用。
2:嵌入Tomcat, Jetty Undertow 而且不需要部署他们。
3:提供的“starters”poms来简化Maven配置
4:尽可能自动配置spring应用。
5:提供生产指标,健壮检查和外部化配置
6:绝对没有代码生成和XML配置要求
我们可以通过继承spring-boot-starter-parent来使用SpringBoot。
其中涵盖各类jar版本号,编码等属性配置,依赖配置,开发信息,协议,插件等管理信息。
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
如果说我们有自己的parent项目 我们应该怎么使用SpringBoot呢?
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencyManagement>
<dependencies>
<!-- Override Spring Data release train provided by Spring Boot -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Fowler-SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
SB包含了一个maven插件,可以帮你打包成一个可执行的jar文件,使用方式如下
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
除了maven以外还支持ant和Gradle具体参阅 官方文档
Staters是一组便捷的依赖描述,比如你想使用jpa功能,只需要引入spring-boot-starter-data-jpa 即可。其他组件基本一样,如spring-boot-starter-* 这种形式。
具体参阅官方文档
<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>chapter01</groupId>
<artifactId>chapter01</artifactId>
<packaging>jar</packaging>
<name>chapter01 Maven Webapp</name>
<url>http://www.example.com</url>
<!-- 引入parent依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</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>
package com.ricky;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
//开启SpringBoot自动注入配置 等价于原来的SpringBootApplication
@EnableAutoConfiguration
//开启RestController注解 含有ResponseBody 即非页面形式
@RestController
public class SpringBootApplication {
@GetMapping("/")
public String home() {
return "Hello World!";
}
/**
* 开启SpringBoot服务
* @param args
*/
public static void main(String[] args) {
//等价于 new SpringApplication(SpringBootApplication.class).run(args);
SpringApplication.run(SpringBootApplication.class,args);
}
}
java8或者9
5.0.5.RELEASE或者更高