SpringBoot
jdk的要求
使用 SpringBoot 必须使用 JDK1.8 以上版本。
引入pom依赖
- 父模块: spring-boot-starter-parent
- 启动依赖: spring-boot-starter-web
- 测试依赖: spring-boot-starter-test
- 热部署依赖: spring-boot-devtools
<?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.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>
<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>
打包
1.打包需要引进必要的依赖,而且确认项目运行环境是jdk而非其他
2.打好的 jar 包在项目中的 target 文件夹下
3.运行jar包
运行jar包前需要确认,电脑已经配置jdk1.8以上。才能运行。首先将项目中的 demo.jar 复制到某个文件夹下,然后在该文件夹处按 Shif键右击打开 DOC 命令提示框,输入 java -jar demo.jar 命令回车即可,出现下图的界面说明 jar 包运行成功。
配置文件的优先级
经过测试, SpringBoot 项目中配置文件的读取顺序为:
- config/application.properties(项目根目录中 config 目录下)
- application.properties(项目根目录下)
- resources/config/application.properties(项目 resources 目录中 config 目录下)
- resources/application.properties(项目的 resources 目录下)
配置文件
主配置文件,配置了这个会优先读取里面的属性覆盖主配置文件的属性
spring.profiles.active=dev dev就是配置文件名中间的名称
项目会应用开发环境中的服务器端口号 8081
tomcat 服务器端口(默认)
server.port=8080
绑定属性值
定义一个 Student 类需要添加 @Component 注解,让 spring 在启动的时候扫描到该类,并添加到 spring 容器中。接下来为各个属性绑定值,通常有两种方法可以实现:一种是 @Value ,另一种 @ConfigurationProperties 。
@PropertySource 用于指定配置文件的位置,需要注意的是只能指定后缀为 properties 的配置文件,
不能指定 yaml 文件。
@value的使用
。。。。。自认为不适用,省略介绍
@ConfigurationProperties 的使用
@ConfigurationProperties 用于批量的为属性绑定值,可以用于任何数据类型,使用时需指定配置文件属性
的前缀。 首先需要导入依赖 spring-boot-configuration-processor;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
@ConfigurationProperties( prefix =“student”) prefix是指定前缀,比如student.name
@ConfigurationProperties 用于批量注入值,可以和@Value 一块使用,两者可以互补,
@ConfigurationProperties 的优先级高于@Value。