Spring Boot
什么是Spring Boot?
Sping Boot使得我们更加容易创建一个独立的、生产级的项目,并且只需要进行很少的配置。以下是来自Sping Boot 官网的解释:
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
特征
- Create stand-alone Spring applications
创建一个单独且标准的Sping应用。 - Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
直接嵌入Tomcat,、Jetty、Undertow服务器,而减省去手动部署的麻烦 - Provide opinionated ‘starter’ dependencies to simplify your build configuration
提供了各类的 starter 启动器来简化项目的构建配置 - Automatically configure Spring and 3rd party libraries whenever possible
尽可能自动配置Spring和第三方库 - Provide production-ready features such as metrics, health checks and externalized configuration
提供生产就绪指标,如metrics(各项指标提供度量工具的包)、健康检查和额外的配置 - Absolutely no code generation and no requirement for XML configuration
绝对没有代码生成,也不需要XML配置
入门
1、使用IntelliJ IDEA 创建第一个Spring Boot 项目,建议使用1.8及以上的JDK版本
2、填写相对应的包名、版本号、项目描述、包名等。
3、在此我们可以选择SpingBoot的版本,以及其他需要使用的第三方Jar包,如MySql、Oracle等,在此可不先勾选,在后期pom.xml文件引入即可。一般我们创建Spring Boot项目的目的为搭建web项目,因此在此勾选引入Web包。
4、填写项目名,选择项目存储路径,点击Finish即可
5、待Maven仓库下载相对应的jar包,即可启动你的Spring Boot项目
6、当控制台打印出以下类似信息则为启动完毕
项目结构
1、DemoApplication.java
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
2、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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、默认生成的application.properties是空的,在此文件我们可以配置项目的端口号、路径等等。但个人比较推荐使用yml的方式,因为yml语法相对简洁,更加灵活方便。
至此,一个简单的Spring Boot项目已经构建完成。我们可以看到无须编写传统的Spring项目 xml配置,仅需引入相对应的Spring Boot 启动器,及使用@SpringBootApplication配置启动类即可。
链接
Sprin Boot官网:https://spring.io/projects/spring-boot/