序言
Spring Boot的目的是更容易的创建基于Spring的应用程序。传统Spring应用程序有大量的XML配置,而Spring Boot仅需很少的配置。并且Spring Boot应用支持打包成独立jar,内嵌servlet容器,通过java -jar简单命令即可启动,因此方便传输和部署。
系统要求
Spring Boot 2.2.2.RELEASE版本使用要求:
- java 8
- Spring Framework 5.2.2.RELEASE
- Maven 3.3+
- Gradle 5.x and 6.x
- Tomcat 9.0
- Jetty 9.4
安装Spring Boot
maven:
<?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 https://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>2.2.2.RELEASE</version> </parent> <!-- Override inherited settings --> <description/> <developers> <developer/> </developers> <licenses> <license/> </licenses> <scm> <url/> </scm> <url/> <!-- 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>
开发第一个Spring Boot应用
本节介绍如何开发一个基于Spring Boot的简单的hello world应用程序,我们采用Maven来构建我们的应用程序。下面是具体的步骤:
创建POM
<?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 https://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>2.2.2.RELEASE</version> </parent> <description/> <developers> <developer/> </developers> <licenses> <license/> </licenses> <scm> <url/> </scm> <url/> <!-- Additional lines to be added here... --> </project>
添加依赖
spring boot提供了一系列的starter包,用于开发特定类型的应用程序。上面创建的pom继承了spring-boot-starter-parent。spring-boot-starter-parent是一个特殊的starter,它提供了许多默认配置,包括dependency-management片段,该片断可以确保依赖的一致性。
因为我们示例是一个web程序,所以添加依赖spring-boot-starter-web。
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
书写代码
建立一个类文件src/main/java/Example.java,并编写控制器代码。
import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(Example.class, args); } }
在这个类里我们使用到了三个注解@RestController、@EnableAutoConfiguration和@RequestMapping,最后一个注解@RequestMapping都很熟悉了,它用于标注请求处理方法。第一个注解@RestController,见名知意,它用于标注一个Controller类的,特殊的是它支持RestFull风格的API,它直接渲染方法的返回值,可想而知,如果你使用JSON,这个注解是相当方便的。第二个注解@EnableAutoConfiguration用于标注Spring配置,它会基于你的依赖,智能的猜测您的应用配置,这个注解可以说是@SpringBoot的核心,正式这个注解的存在,才使得大大简化了Spring的配置。
你可能注意到一个奇怪的地方,就是在conroller类里竟然有一个main方法。其实这个方法是我们的应用程序的入口点,由代码可知,程序的启动是委托给SpringApplication的,它先启动web容器(tomcat、jetty等),然后再加载SpringBoot的配置类,就是@EnableAutoConfiguration标注的类。
运行示例
$ mvn spring-boot:run . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.2.2.RELEASE) ....... . . . ....... . . . (log output here) ....... . . . ........ Started Example in 2.222 seconds (JVM running for 6.514)
接着打开浏览器访问localhost:8080,你会得到如下响应:
hello world!
创建可执行jar包
为了创建可执行jar包,需要添加spring-boot-maven-plugin插件到pom.xml。
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
最后执行mvn package命令,即可生成单一可执行jar包。
尝试使用java -jar命令启动。