一篇文章带你掌握主流办公框架——SpringBoot( 六 )

  1. SpringBoot配置文件中引入Maven属性
# 设置启用的环境# 采用${}引用Maven中的属性spring:profiles:active: ${profile.active}---#开发spring:profiles: devserver:port: 80---#生产spring:profiles: proserver:port: 81---#测试spring:profiles: testserver:port: 82---
  1. 打包并开启服务器后,查看端口号
端口号为81那么关于Maven的测试就到这里结束
SpringBoot配置文件分类我们的环境配置可以写于许多位置,由此我们大致分为四类:
  1. classpath:application.yml[最低](Resources的一层配置中)

一篇文章带你掌握主流办公框架——SpringBoot

文章插图
  1. classpath:config/application.yml(Resources的二层配置中)

一篇文章带你掌握主流办公框架——SpringBoot

文章插图
  1. classpath:config/application.yml(package后jar包同目录下的配置文件)

一篇文章带你掌握主流办公框架——SpringBoot

文章插图
  1. file:config/application.yml[最高]

一篇文章带你掌握主流办公框架——SpringBoot

文章插图
我们将这些位置进行分类并排出优先级:
  • 1级:file:config/application.yml[最高]
  • 2级:file:application.yml
  • 3级:classpath:config/application.yml
  • 4级:classpath:application.yml[最低]
不同位置环境配置作用:
  • 1级与2级留作系统打包后设置通用属性
  • 3级与4级用于系统开发阶段设置通用属性
SpringBoot整合第三方技术在基本介绍了SpringBoot之后 , 我们介绍最重要的一部分——整合第三方技术
下面我们以三个小案例来展现SpringBoot的整合
整合JUnitSpringBoot是用于简化Spring的工具,所以我们分别从Spring和SpringBoot的视角进行整合
Spring整合JUnit我们先给出Spring整合JUnit的代码:
// 设置运行器@RunWith(SpringJUnit4ClassRunner.class)// 加载环境@ContextConfiguration(classes = SpringConfig.class)public class UserServiceTesst{// 自动装配测试对象@Autowiredprivate BookService bookService;// 测试方法@Testpublic void testSave(){bookService.save();}}SpringBoot整合JUnit我们从头说起:
  1. 创建新项目(这次我们只整合JUnit , 所以我们的技术选择选择空白)

一篇文章带你掌握主流办公框架——SpringBoot

文章插图
  1. 我们首先查看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 https://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.5.0</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.itheima</groupId><artifactId>springboot_07_test</artifactId><version>0.0.1-SNAPSHOT</version><properties><java.version>1.8</java.version></properties><dependencies><!--我们提供了spring-boot-starter来做依赖传递(web时用的是spring-boot-starter-web)--><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>
  1. 项目自带有一个测试Java类
// 这里就是包,倘若为com.itheima1 , classes需要设置为启动类.classpackage com.itheima;import com.itheima.Springboot07TestApplication;import com.itheima.service.BookService;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;// 设置JUnit加载的SpringBoot启动类(类似于@RunWith和@ContextConfiguration的整合)@SpringBootTestclass Springboot07TestApplicationTests {// 自动装配测试对象(未发生变化)@Autowiredprivate BookService bookService;// 测试方法(未发生变化)@Testpublic void save() {bookService.save();}}/*名称:@SpringBootTest类型:测试类注解位置:测试类定义上方作用:设置JUnit加载的SpringBoot启动类相关属性: classes:设置SpringBoot启动类注意点: 如果该测试类在SpringBoot启动类的包或子包中 , 可以省略启动类的设置,也就是省略classes的设定 当该测试类与启动主Java类不属于同一目录名称下时,需要设置classes属性为启动类 @SpringBootTest(classes = Springboot07TestApplication.class)*/

推荐阅读