SpringBoot
SpringBoot入门及配置
SpringBoot 的主要特点
⚫ 创建独立的 Spring 应用程序
⚫ 嵌入到 Tomcat,无需部署 WAR 文件
⚫ 简化 Maven 配置
⚫ 自动配置 Spring
⚫ 提供生产就绪型功能,如指标,健康检查和外部配置
⚫ 绝对没有代码生成并且对 XML 也没有配置要求
总体上来讲,可以将 SpringBoot 概括为:是一个快速开发的框架,能够帮助我们 快速整合第三方框
开发环境的搭建
- JDK 的要求
使用 SpringBoot 必须使用 JDK1.8 以上版本。 - 开发工具
本课程选用的是Spring Tool Suite工具。
Spring Tool Suite 简称 STS,下载地址为: http://spring.io/tools/sts/all/,我们选 择 Windows 版本下载即可。
STS 为绿色版,解压之后可以直接使用。 - Maven 的要求
项目管理工具推荐使用 apache-maven-3.3.9 及以上版本。
引人 pom 依赖
打开项目中的 pom.xml 文件,依次引入以下依赖和设置编码格式。 通过添加依赖可以快速地整合第三方框架,这主要通过 Maven 的依赖关系(Maven 继承)完成。 具体的内容如下:
父模块: spring-boot-starter-parent
启动依赖: spring-boot-starter-web
测试依赖: spring-boot-starter-test
热部署依赖: spring-boot-devtools
<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.ysd.demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<url>http://maven.apache.org</url>
<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 项目设置:编码格式UTF-8 -->
<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>junit</groupId>7
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring Boot SpringMVC框架依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
<!--
optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用
devtools,需要重新引入 -->
<scope>true</scope><!-- 热部署 -->
</dependency>
</dependencies>
</project>
创建控制器
在主模块包(com.ysd.demo.demo)或者其子包下创建控制器类(如: HelloController 类)并注解为
@RestController,该注解的作用相当于我们原来使用的 @Controller+@ResponseBody 合到一起的作用,在该类
中写一个处理请求的方式并通过注解@RequestMapping 指定访问的路径。
发布运行项目
-
运行项目
在 STS 开发工具中,可以通过如下两种方式运行项目:
⚫ 首先找到项目中的入口函数(App 类的 main 方法),然后右键 Run As> Spring Boot App 即可。
⚫ 在 Boot Dashboard 仪表板上,选中要启动的项目 demo,右击选择(Re)start 即可。 -
打 jar 包发布项目
第一步: 在 pom.xml 文件中添加一个 SpringBoot 插件依赖,如下:
<plugins>
<!-- SpringBoot插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<!-- SpringBoot项目打包jar名称 -->
<finalName>demo</finalName>
</build>
第二步: 在打包之前需要先确定项目的 java 环境是 jdk 而非 jre
第三步: 打 jar 包
选中项目右键>Run As >Maven bulid… 在 Goals 框中输入 clean package ,点击 Run 即可在项目中的 target
目录中生中 demo.jar,如下图:
第四步: 运行 jar 包
将项目中的 demo.jar 复制到某个文件夹下,在该文件夹处按 Shift 键右击打开 DOC 命令提示框,输入 java
-jar demo.jar 命令回车即可,出现下图的界面说明 jar 包运行成功。
SpringBoot 配置文件
配置文件的路径
SpringBoot 使用了一个全局的配置文件 application.properties,该配置文件可以放置在以下几个位置:
⚫ 根目录下
⚫ 项目根目录中 config 目录下
⚫ 项目的 resources 目录下
⚫ 项目 resources 目录中 config 目录下
配置文件的优先级
SpringBoot 项目中配置文件的读取顺序为:
- config/application.properties(项目根目录中 config 目录下)
- application.properties(项目根目录下)
- resources/config/application.properties(项目 resources 目录中 config 目录下)
- resources/application.properties(项目的 resources 目录下)
配置文件的两种文件格式
SpringBoot 项目默认加载的配置文件除了 application.properties 文件,还支持 application.yml(也可以写成application.yaml)文件
多环境配置
多环境配置文件可以和 application.properties 文件一样放置在项目中四个位置的任何一处。本实例中,将
以上四个配置文件都放置在项目的 src /main/resources 目录下面,如果没有 resources 目录,需手动新建一个。
整体结构如下图:
绑定属性值
定义一个 Student 类需要添加@Component 注解,让 spring 在启动的时候扫描到该类,并添加到 spring 容
器中。接下来为各个属性绑定值,通常有两种方法可以实现:一种是@Value,另一种是@ConfigurationProperties。
@Value 的使用
@Value 注解可以绑定属性值,但是只能绑定简单类型的属性值,通常通过 SqEL 方式绑定。 具体用法:
@Value 属性名,在属性名上添加该注解,如:
@Value("${my.name}") private String myName;
注意: 使用@Value 绑定属性值时,要求该实体类必须使用@Component 配置成 Bean,并确保能够 spring
在启动的时候扫描到该类,并添加到 spring 容器中。
@ConfigurationProperties 的使用
@ConfigurationProperties 用于批量的为属性绑定值,可以用于任何数据类型,使用时需指定配置文件属性的前缀。 首先需要导入依赖 spring-boot-configuration-processor;
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>```
在实体类中配置@ConfigurationProperties,并使用@PropertySource 读取前面的 stu.properties 属性文件,
具体代码如下:
```**
* 学生类
* @author wwp
*
*/
@Component22
@ConfigurationProperties(prefix="student") //该注解放在类上面
@PropertySource({"classpath:stu.properties"})
public class Student {
@Value("Marry")
private String name; //姓名
private int age; //年龄
private boolean sex; //性别
private Date birthday; //生日
private String[] hobbies; //爱好
private List<String> skills; //技能
private Map<String,Object> address; //住址
//getter和setter方法
...
}
小结: @ConfigurationProperties 用于批量注入值,可以和@Value 一块使用,两者可以互补,
@ConfigurationProperties 的优先级高于@Value。
定义随机数
在项目的 resources/config 目录下添加 random.properties 文件,添加以下内容:
user.random.secret=${random.value}
#随机 int 数字
user.random.intNumber=${random.int}
#随机 long 数字
user.random.longNumber=${random.long}
#随机 uuid
user.random.uuid=${random.uuid}
#随机 10 以内的数字
user.random.lessTen=${random.int(10)}
#随机 1024~65536 之内的数字
user.random.range=${random.int[1024,65536]}
需要注意的是:
⚫ 占位符的值必须是完整路径
⚫ 占位符设置默认值,冒号后面不能有空格