南阳网站制作怎么样,自建站怎么做,专业的网页制作服务好,tp3.2.3网站开发实例文章目录 自定义 banner自定义 SpringApplicationFluentBuilder APIProfiles指定环境环境激活环境包含Profile 分组Profile 配置文件 外部化配置配置优先级 外部配置导入配置属性占位符 单元测试-JUnit5测试组件测试注解断言嵌套测试参数化测试 自定义 banner
banner 就是启动… 文章目录 自定义 banner自定义 SpringApplicationFluentBuilder APIProfiles指定环境环境激活环境包含Profile 分组Profile 配置文件 外部化配置配置优先级 外部配置导入配置属性占位符 单元测试-JUnit5测试组件测试注解断言嵌套测试参数化测试 自定义 banner
banner 就是启动的时候打印的东西
类路径添加banner.txt或设置spring.banner.location就可以定制 banner推荐网站Spring Boot banner 在线生成工具制作下载英文 banner.txt修改替换 banner.txt 文字实现自定义个性化启动 banner-bootschool.net https://www.bootschool.net/ascii
自定义 SpringApplication
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
public class MyApplication {public static void main(String[] args) {SpringApplication application new SpringApplication(MyApplication.class);application.setBannerMode(Banner.Mode.OFF);application.run(args);}}FluentBuilder API
new SpringApplicationBuilder().sources(Parent.class).child(Application.class).bannerMode(Banner.Mode.OFF).run(args);Profiles 环境隔离能力快速切换开发、测试、生产环境 步骤 标识环境指定哪些组件、配置在哪个环境生效切换环境这个环境对应的所有组件和配置就应该生效 指定环境
● Spring Profiles 提供一种隔离配置的方式使其仅在特定环境生效 ● 任何Component, Configuration 或 ConfigurationProperties 可以使用 Profile 标记来指定何时被加载。【容器中的组件都可以被 Profile标记】
环境激活
配置激活指定环境 配置文件
spring.profiles.activeproduction,hsqldb也可以使用命令行激活。–spring.profiles.activedev,hsqldb还可以配置默认环境 不标注Profile 的组件永远都存在。 a. 以前默认环境叫default b. spring.profiles.defaulttest推荐使用激活方式激活指定环境
环境包含
注意
spring.profiles.active和spring.profiles.default 只能用到 无 profile 的文件中如果在application-dev.yaml中编写就是无效的也可以额外添加生效文件而不是激活替换。比如
spring.profiles.include[0]common
spring.profiles.include[1]local最佳实战 ● 生效的环境 激活的环境/默认环境 包含的环境 ● 项目里面这么用
基础的配置mybatis、log、xxx写到包含环境中需要动态切换变化的 db、redis写到激活的环境中
Profile 分组
创建prod组指定包含db和mq配置
spring.profiles.group.prod[0]db
spring.profiles.group.prod[1]mq使用--spring.profiles.activeprod 就会激活proddbmq配置文件
Profile 配置文件 pplication-{profile}.properties可以作为指定环境的配置文件。 激活这个环境配置就会生效。最终生效的所有配置是 application.properties主配置文件任意时候都生效application-{profile}.properties指定环境配置文件激活指定环境生效
profile优先级 application
外部化配置
场景线上应用如何快速修改配置并应用最新配置 ● SpringBoot 使用 配置优先级 外部配置 简化配置更新、简化运维。 ● 只需要给jar应用所在的文件夹放一个application.properties最新配置文件重启项目就能自动应用最新配置
配置优先级
Spring Boot 允许将配置外部化以便可以在不同的环境中使用相同的应用程序代码。 我们可以使用各种外部配置源包括Java Properties文件、YAML文件、环境变量和命令行参数。 Value可以获取值也可以用ConfigurationProperties将所有属性绑定到java object中 以下是 SpringBoot 属性源加载顺序。后面的会覆盖前面的值。由低到高高优先级配置覆盖低优先级
以下是 SpringBoot 属性源加载顺序。后面的会覆盖前面的值。由低到高高优先级配置覆盖低优先级
默认属性通过SpringApplication.setDefaultProperties指定的PropertySource指定加载的配置需要写在Configuration类上才可生效配置文件application.properties/yml等RandomValuePropertySource支持的random.*配置如Value(“${random.int}”)OS 环境变量Java 系统属性System.getProperties()JNDI 属性来自java:comp/envServletContext 初始化参数ServletConfig 初始化参数SPRING_APPLICATION_JSON属性内置在环境变量或系统属性中的 JSON命令行参数测试属性。(SpringBootTest进行测试时指定的属性)测试类TestPropertySource注解Devtools 设置的全局属性。($HOME/.config/spring-boot) 结论配置可以写到很多位置常见的优先级顺序 ● 命令行 配置文件 springapplication配置
配置文件优先级如下(后面覆盖前面)
jar 包内的application.properties/ymljar 包内的application-{profile}.properties/ymljar 包外的application.properties/ymljar 包外的application-{profile}.properties/yml 建议用一种格式的配置文件。如果.properties和.yml同时存在,则.properties优先 结论包外 包内 同级情况profile配置 application配置 所有参数均可由命令行传入使用–参数项参数值将会被添加到环境变量中并优先于配置文件。 比如java -jar app.jar --name“Spring”,可以使用Value(“${name}”)获取
演示场景 ● 包内 application.properties server.port8000 ● 包内 application-dev.properties server.port9000 ● 包外 application.properties server.port8001 ● 包外 application-dev.properties server.port9001 启动端口命令行 9001 8001 9000 8000
外部配置
SpringBoot 应用启动时会自动寻找application.properties和application.yaml位置进行加载。顺序如下后面覆盖前面
类路径: 内部 a. 类根路径 b. 类下/config包当前路径项目所在的位置 a. 当前路径 b. 当前下/config子目录 c. /config目录的直接子目录
最终效果优先级由高到低前面覆盖后面 ● 命令行 包外config直接子目录 包外config目录 包外根目录 包内目录 ● 同级比较 ○ profile配置 默认配置 ○ properties配置 yaml配置 规律最外层的最优先。 ● 命令行 所有 ● 包外 包内 ● config目录 根目录 ● profile application 配置不同就都生效互补配置相同高优先级覆盖低优先级
导入配置
使用spring.config.import可以导入额外配置
spring.config.importmy.properties
my.propertyvalue无论以上写法的先后顺序my.properties的值总是优先于直接在文件中编写的my.property。
属性占位符
配置文件中可以使用 ${name:default}形式取出之前配置过的值。
app.nameMyApp
app.description${app.name} is a Spring Boot application written by ${username:Unknown}单元测试-JUnit5
SpringBoot 提供一系列测试工具集及注解方便我们进行测试。 spring-boot-test提供核心测试能力spring-boot-test-autoconfigure 提供测试的一些自动配置。 我们只需要导入spring-boot-starter-test 即可整合测试
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope
/dependencyspring-boot-starter-test 默认提供了以下库供我们测试使用 ● JUnit 5 ● Spring Test ● AssertJ ● Hamcrest ● Mockito ● JSONassert ● JsonPath
测试
组件测试
直接Autowired容器中的组件进行测试
注解
JUnit5的注解与JUnit4的注解有所变化 https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations ● Test :表示方法是测试方法。但是与JUnit4的Test不同他的职责非常单一不能声明任何属性拓展的测试将会由Jupiter提供额外测试 ● ParameterizedTest :表示方法是参数化测试下方会有详细介绍 ● RepeatedTest :表示方法可重复执行下方会有详细介绍 ● DisplayName :为测试类或者测试方法设置展示名称 ● BeforeEach :表示在每个单元测试之前执行 ● AfterEach :表示在每个单元测试之后执行 ● BeforeAll :表示在所有单元测试之前执行 ● AfterAll :表示在所有单元测试之后执行 ● Tag :表示单元测试类别类似于JUnit4中的Categories ● Disabled :表示测试类或测试方法不执行类似于JUnit4中的Ignore ● Timeout :表示测试方法运行如果超过了指定时间将会返回错误 ● ExtendWith :为测试类或测试方法提供扩展类引用
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assumptions.assumeTrue;import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;class StandardTests {BeforeAllstatic void initAll() {}BeforeEachvoid init() {}DisplayName()Testvoid succeedingTest() {}Testvoid failingTest() {fail(a failing test);}TestDisabled(for demonstration purposes)void skippedTest() {// not executed}Testvoid abortedTest() {assumeTrue(abc.contains(Z));fail(test should have been aborted);}AfterEachvoid tearDown() {}AfterAllstatic void tearDownAll() {}}断言 嵌套测试
JUnit 5 可以通过 Java 中的内部类和Nested 注解实现嵌套测试从而可以更好的把相关的测试方法组织在一起。在内部类中可以使用BeforeEach 和AfterEach 注解而且嵌套的层次没有限制。
DisplayName(A stack)
class TestingAStackDemo {StackObject stack;TestDisplayName(is instantiated with new Stack())void isInstantiatedWithNew() {new Stack();}NestedDisplayName(when new)class WhenNew {BeforeEachvoid createNewStack() {stack new Stack();}TestDisplayName(is empty)void isEmpty() {assertTrue(stack.isEmpty());}TestDisplayName(throws EmptyStackException when popped)void throwsExceptionWhenPopped() {assertThrows(EmptyStackException.class, stack::pop);}TestDisplayName(throws EmptyStackException when peeked)void throwsExceptionWhenPeeked() {assertThrows(EmptyStackException.class, stack::peek);}NestedDisplayName(after pushing an element)class AfterPushing {String anElement an element;BeforeEachvoid pushAnElement() {stack.push(anElement);}TestDisplayName(it is no longer empty)void isNotEmpty() {assertFalse(stack.isEmpty());}TestDisplayName(returns the element when popped and is empty)void returnElementWhenPopped() {assertEquals(anElement, stack.pop());assertTrue(stack.isEmpty());}TestDisplayName(returns the element when peeked but remains not empty)void returnElementWhenPeeked() {assertEquals(anElement, stack.peek());assertFalse(stack.isEmpty());}}}
}参数化测试
参数化测试是JUnit5很重要的一个新特性它使得用不同的参数多次运行测试成为了可能也为我们的单元测试带来许多便利。
利用ValueSource等注解指定入参我们将可以使用不同的参数进行多次单元测试而不需要每新增一个参数就新增一个单元测试省去了很多冗余代码。
ValueSource: 为参数化测试指定入参来源支持八大基础类以及String类型,Class类型 NullSource: 表示为参数化测试提供一个null的入参 EnumSource: 表示为参数化测试提供一个枚举入参 CsvFileSource表示读取指定CSV文件内容作为参数化测试入参 MethodSource表示读取指定方法的返回值作为参数化测试入参(注意方法返回需要是一个流)
ParameterizedTest
ValueSource(strings {one, two, three})
DisplayName(参数化测试1)
public void parameterizedTest1(String string) {System.out.println(string);Assertions.assertTrue(StringUtils.isNotBlank(string));
}ParameterizedTest
MethodSource(method) //指定方法名
DisplayName(方法来源参数)
public void testWithExplicitLocalMethodSource(String name) {System.out.println(name);Assertions.assertNotNull(name);
}static StreamString method() {return Stream.of(apple, banana);
}