SpringBoot中Yaml语法和JSR303数据校验《第三课》

简介: SpringBoot中Yaml语法和JSR303数据校验《第三课》

第一部分 Yaml语法的格式

第一点:Yaml的定义

YAML(/ˈjæməl/,尾音类似camel骆驼)是一个可读性高,用来表达数据序列化的格式。YAML参考了其他多种语言,包括:C语言、Python、Perl,并从XML、电子邮件的数据格式(RFC 2822)中获得灵感。Clark Evans在2001年首次发表了这种语言,另外Ingy döt Net与Oren Ben-Kiki也是这语言的共同设计者。当前已经有数种编程语言或脚本语言支持(或者说解析)这种语言。


YAML是"YAML Ain't a Markup Language"(YAML不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言),但为了强调这种语言以数据做为中心,而不是以标记语言为重点,而用反向缩略语重命名。

第二点:配置文件的作用

修改SpringBoot自动配置的默认值,因为SpringBoot在底层都给我们自动配置好了;比如我们可以在配置文件中修改Tomcat 默认启动的端口号!测试一下!

第三点:yaml概述

YAML是 "YAML Ain't a Markup Language" (YAML不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)


这种语言以数据作为中心,而不是以标记语言为重点!


以前的配置文件,大多数都是使用xml来配置;比如一个简单的端口配置,我们来对比下yaml和xml

传统xml配置

<server>
    <port>8081<port>
</server>

yaml配置

server:
  prot: 8080

Yaml的基本语法

说明:Yaml的·语法要求严格!

1、空格不能省略


2、以缩进来控制层级关系,只要是左边对齐的一列数据都是同一个层级的。


3、属性和值的大小写都是十分敏感的。


字面量:普通的值 [ 数字,布尔值,字符串 ]


字面量直接写在后面就可以 , 字符串默认不用加上双引号或者单引号;


k: v 注意:

“ ” 双引号,不会转义字符串里面的特殊字符 , 特殊字符会作为本身想表示的意思;


比如 :name: "kuang \n shen" 输出 :kuang 换行 shen


'' 单引号,会转义特殊字符 , 特殊字符最终会变成和普通字符一样输出


比如 :name: ‘kuang \n shen’ 输出 :kuang \n shen


对象、Map(键值对)下面是基本的语法:

Yml基本语法

server:
  port: 8082
  servlet:
    context-path: /yml


对象

student:
          name: ok
          age: 12
          sex: 男
---
dog:
        name: xiaoma
        age:23
        sex: 女
---
duck:      
        name: xiaoma
        age:23
        sex: 女

行内写法

dog: {name: xiaowang,age: 12}
duck: {name: xiaowang,age: 12,sex: 男}
student:{name: xiaowang,age: 12,sex: 男,height: 123}

数组

pets:
  -cat
  -dog
  -pig
  -duck
---
  pet: [cat,dog,pig,duck]

案例一 端口号的改变:在资源文件中application.properties 改变服务器的端口号  server.port=1004 来判断端口号从高到底

package com.java.springboot02.springbootapplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//SpringBoot的组件
@SpringBootApplication
public class SpringbootApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }
}

"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar=50344:D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;D:\maven-workspace\spaceVideo\demo\springboot-parent\springboot-application\target\classes;D:\maven_repository\org\springframework\boot\spring-boot-starter-web\2.7.3\spring-boot-starter-web-2.7.3.jar;D:\maven_repository\org\springframework\boot\spring-boot-starter\2.7.3\spring-boot-starter-2.7.3.jar;D:\maven_repository\org\springframework\boot\spring-boot\2.7.3\spring-boot-2.7.3.jar;D:\maven_repository\org\springframework\boot\spring-boot-autoconfigure\2.7.3\spring-boot-autoconfigure-2.7.3.jar;D:\maven_repository\org\springframework\boot\spring-boot-starter-logging\2.7.3\spring-boot-starter-logging-2.7.3.jar;D:\maven_repository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\maven_repository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;D:\maven_repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;D:\maven_repository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\maven_repository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\maven_repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\maven_repository\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;D:\maven_repository\org\springframework\boot\spring-boot-starter-json\2.7.3\spring-boot-starter-json-2.7.3.jar;D:\maven_repository\com\fasterxml\jackson\core\jackson-databind\2.13.3\jackson-databind-2.13.3.jar;D:\maven_repository\com\fasterxml\jackson\core\jackson-annotations\2.13.3\jackson-annotations-2.13.3.jar;D:\maven_repository\com\fasterxml\jackson\core\jackson-core\2.13.3\jackson-core-2.13.3.jar;D:\maven_repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.3\jackson-datatype-jdk8-2.13.3.jar;D:\maven_repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.3\jackson-datatype-jsr310-2.13.3.jar;D:\maven_repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.3\jackson-module-parameter-names-2.13.3.jar;D:\maven_repository\org\springframework\boot\spring-boot-starter-tomcat\2.7.3\spring-boot-starter-tomcat-2.7.3.jar;D:\maven_repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.65\tomcat-embed-core-9.0.65.jar;D:\maven_repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.65\tomcat-embed-el-9.0.65.jar;D:\maven_repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.65\tomcat-embed-websocket-9.0.65.jar;D:\maven_repository\org\springframework\spring-web\5.3.22\spring-web-5.3.22.jar;D:\maven_repository\org\springframework\spring-beans\5.3.22\spring-beans-5.3.22.jar;D:\maven_repository\org\springframework\spring-webmvc\5.3.22\spring-webmvc-5.3.22.jar;D:\maven_repository\org\springframework\spring-aop\5.3.22\spring-aop-5.3.22.jar;D:\maven_repository\org\springframework\spring-context\5.3.22\spring-context-5.3.22.jar;D:\maven_repository\org\springframework\spring-expression\5.3.22\spring-expression-5.3.22.jar;D:\maven_repository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\maven_repository\org\springframework\spring-core\5.3.22\spring-core-5.3.22.jar;D:\maven_repository\org\springframework\spring-jcl\5.3.22\spring-jcl-5.3.22.jar" com.java.springboot02.springbootapplication.SpringbootApplication
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.3)
2022-09-06 21:35:50.642  INFO 21116 --- [           main] c.j.s.s.SpringbootApplication            : Starting SpringbootApplication using Java 1.8.0_171 on HELLOWWORLD with PID 21116 (D:\maven-workspace\spaceVideo\demo\springboot-parent\springboot-application\target\classes started by MZFAITHDREAM in D:\maven-workspace\spaceVideo\demo\springboot-parent)
2022-09-06 21:35:50.645  INFO 21116 --- [           main] c.j.s.s.SpringbootApplication            : No active profile set, falling back to 1 default profile: "default"
2022-09-06 21:35:51.791  INFO 21116 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8085 (http)
2022-09-06 21:35:51.813  INFO 21116 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-09-06 21:35:51.814  INFO 21116 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-09-06 21:35:52.241  INFO 21116 --- [           main] o.a.c.c.C.[.[.[/config-properties]       : Initializing Spring embedded WebApplicationContext
2022-09-06 21:35:52.241  INFO 21116 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1545 ms
2022-09-06 21:35:52.621  INFO 21116 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8085 (http) with context path '/config-properties'
2022-09-06 21:35:52.631  INFO 21116 --- [           main] c.j.s.s.SpringbootApplication            : Started SpringbootApplication in 2.578 seconds (JVM running for 3.956)

"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar=50508:D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;D:\maven-workspace\spaceVideo\demo\springboot-parent\springboot-application\target\classes;D:\maven_repository\org\springframework\boot\spring-boot-starter-web\2.7.3\spring-boot-starter-web-2.7.3.jar;D:\maven_repository\org\springframework\boot\spring-boot-starter\2.7.3\spring-boot-starter-2.7.3.jar;D:\maven_repository\org\springframework\boot\spring-boot\2.7.3\spring-boot-2.7.3.jar;D:\maven_repository\org\springframework\boot\spring-boot-autoconfigure\2.7.3\spring-boot-autoconfigure-2.7.3.jar;D:\maven_repository\org\springframework\boot\spring-boot-starter-logging\2.7.3\spring-boot-starter-logging-2.7.3.jar;D:\maven_repository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\maven_repository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;D:\maven_repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;D:\maven_repository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\maven_repository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\maven_repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\maven_repository\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;D:\maven_repository\org\springframework\boot\spring-boot-starter-json\2.7.3\spring-boot-starter-json-2.7.3.jar;D:\maven_repository\com\fasterxml\jackson\core\jackson-databind\2.13.3\jackson-databind-2.13.3.jar;D:\maven_repository\com\fasterxml\jackson\core\jackson-annotations\2.13.3\jackson-annotations-2.13.3.jar;D:\maven_repository\com\fasterxml\jackson\core\jackson-core\2.13.3\jackson-core-2.13.3.jar;D:\maven_repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.3\jackson-datatype-jdk8-2.13.3.jar;D:\maven_repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.3\jackson-datatype-jsr310-2.13.3.jar;D:\maven_repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.3\jackson-module-parameter-names-2.13.3.jar;D:\maven_repository\org\springframework\boot\spring-boot-starter-tomcat\2.7.3\spring-boot-starter-tomcat-2.7.3.jar;D:\maven_repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.65\tomcat-embed-core-9.0.65.jar;D:\maven_repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.65\tomcat-embed-el-9.0.65.jar;D:\maven_repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.65\tomcat-embed-websocket-9.0.65.jar;D:\maven_repository\org\springframework\spring-web\5.3.22\spring-web-5.3.22.jar;D:\maven_repository\org\springframework\spring-beans\5.3.22\spring-beans-5.3.22.jar;D:\maven_repository\org\springframework\spring-webmvc\5.3.22\spring-webmvc-5.3.22.jar;D:\maven_repository\org\springframework\spring-aop\5.3.22\spring-aop-5.3.22.jar;D:\maven_repository\org\springframework\spring-context\5.3.22\spring-context-5.3.22.jar;D:\maven_repository\org\springframework\spring-expression\5.3.22\spring-expression-5.3.22.jar;D:\maven_repository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\maven_repository\org\springframework\spring-core\5.3.22\spring-core-5.3.22.jar;D:\maven_repository\org\springframework\spring-jcl\5.3.22\spring-jcl-5.3.22.jar" com.java.springboot02.springbootapplication.SpringbootApplication
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.3)
2022-09-06 21:43:24.069  INFO 18576 --- [           main] c.j.s.s.SpringbootApplication            : Starting SpringbootApplication using Java 1.8.0_171 on HELLOWWORLD with PID 18576 (D:\maven-workspace\spaceVideo\demo\springboot-parent\springboot-application\target\classes started by MZFAITHDREAM in D:\maven-workspace\spaceVideo\demo\springboot-parent)
2022-09-06 21:43:24.074  INFO 18576 --- [           main] c.j.s.s.SpringbootApplication            : No active profile set, falling back to 1 default profile: "default"
2022-09-06 21:43:25.181  INFO 18576 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 1004 (http)
2022-09-06 21:43:25.195  INFO 18576 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-09-06 21:43:25.195  INFO 18576 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-09-06 21:43:25.556  INFO 18576 --- [           main] o.a.c.c.C.[.[.[/config-properties]       : Initializing Spring embedded WebApplicationContext
2022-09-06 21:43:25.556  INFO 18576 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1429 ms
2022-09-06 21:43:25.892  INFO 18576 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 1004 (http) with context path '/config-properties'
2022-09-06 21:43:25.902  INFO 18576 --- [           main] c.j.s.s.SpringbootApplication            : Started SpringbootApplication in 2.321 seconds (JVM running for 3.588)

"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar=50535:D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;D:\maven-workspace\spaceVideo\demo\springboot-parent\springboot-application\target\classes;D:\maven_repository\org\springframework\boot\spring-boot-starter-web\2.7.3\spring-boot-starter-web-2.7.3.jar;D:\maven_repository\org\springframework\boot\spring-boot-starter\2.7.3\spring-boot-starter-2.7.3.jar;D:\maven_repository\org\springframework\boot\spring-boot\2.7.3\spring-boot-2.7.3.jar;D:\maven_repository\org\springframework\boot\spring-boot-autoconfigure\2.7.3\spring-boot-autoconfigure-2.7.3.jar;D:\maven_repository\org\springframework\boot\spring-boot-starter-logging\2.7.3\spring-boot-starter-logging-2.7.3.jar;D:\maven_repository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\maven_repository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;D:\maven_repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;D:\maven_repository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\maven_repository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\maven_repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\maven_repository\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;D:\maven_repository\org\springframework\boot\spring-boot-starter-json\2.7.3\spring-boot-starter-json-2.7.3.jar;D:\maven_repository\com\fasterxml\jackson\core\jackson-databind\2.13.3\jackson-databind-2.13.3.jar;D:\maven_repository\com\fasterxml\jackson\core\jackson-annotations\2.13.3\jackson-annotations-2.13.3.jar;D:\maven_repository\com\fasterxml\jackson\core\jackson-core\2.13.3\jackson-core-2.13.3.jar;D:\maven_repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.3\jackson-datatype-jdk8-2.13.3.jar;D:\maven_repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.3\jackson-datatype-jsr310-2.13.3.jar;D:\maven_repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.3\jackson-module-parameter-names-2.13.3.jar;D:\maven_repository\org\springframework\boot\spring-boot-starter-tomcat\2.7.3\spring-boot-starter-tomcat-2.7.3.jar;D:\maven_repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.65\tomcat-embed-core-9.0.65.jar;D:\maven_repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.65\tomcat-embed-el-9.0.65.jar;D:\maven_repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.65\tomcat-embed-websocket-9.0.65.jar;D:\maven_repository\org\springframework\spring-web\5.3.22\spring-web-5.3.22.jar;D:\maven_repository\org\springframework\spring-beans\5.3.22\spring-beans-5.3.22.jar;D:\maven_repository\org\springframework\spring-webmvc\5.3.22\spring-webmvc-5.3.22.jar;D:\maven_repository\org\springframework\spring-aop\5.3.22\spring-aop-5.3.22.jar;D:\maven_repository\org\springframework\spring-context\5.3.22\spring-context-5.3.22.jar;D:\maven_repository\org\springframework\spring-expression\5.3.22\spring-expression-5.3.22.jar;D:\maven_repository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\maven_repository\org\springframework\spring-core\5.3.22\spring-core-5.3.22.jar;D:\maven_repository\org\springframework\spring-jcl\5.3.22\spring-jcl-5.3.22.jar" com.java.springboot02.springbootapplication.SpringbootApplication
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.3)
2022-09-06 21:44:38.391  INFO 14980 --- [           main] c.j.s.s.SpringbootApplication            : Starting SpringbootApplication using Java 1.8.0_171 on HELLOWWORLD with PID 14980 (D:\maven-workspace\spaceVideo\demo\springboot-parent\springboot-application\target\classes started by MZFAITHDREAM in D:\maven-workspace\spaceVideo\demo\springboot-parent)
2022-09-06 21:44:38.395  INFO 14980 --- [           main] c.j.s.s.SpringbootApplication            : No active profile set, falling back to 1 default profile: "default"
2022-09-06 21:44:39.398  INFO 14980 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8084 (http)
2022-09-06 21:44:39.409  INFO 14980 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2022-09-06 21:44:39.409  INFO 14980 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-09-06 21:44:39.752  INFO 14980 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/yml]    : Initializing Spring embedded WebApplicationContext
2022-09-06 21:44:39.753  INFO 14980 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1306 ms
2022-09-06 21:44:40.128  INFO 14980 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8084 (http) with context path '/yml'
2022-09-06 21:44:40.142  INFO 14980 --- [           main] c.j.s.s.SpringbootApplication            : Started SpringbootApplication in 2.275 seconds (JVM running for 3.485)

Yaml第二个作用: 定义一个实体 回顾

package com.bing.springboot0906.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Dog {
    @Value("小王")
    private  String name;
    @Value("3")
    private  Integer age;
    public Dog() {
    }
    public Dog(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
package com.bing.springboot0906;
import com.bing.springboot0906.pojo.Animal;
import com.bing.springboot0906.pojo.Dog;
import com.bing.springboot0906.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot0906ApplicationTests {
    /**
     * 自动注入值
     */
    @Autowired
//  1  private Person person;
//  2  private Dog dog;
//    private Animal animal;
    private Dog dog;
    @Test
    void contextLoads() {
       System.out.println(dog);
//        System.out.println(person);
//            System.out.println(animal);
    }
}
"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar=50813:D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\junit\platform\junit-platform-launcher\1.8.2\junit-platform-launcher-1.8.2.jar;D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\plugins\junit\lib\junit5-rt.jar;D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;D:\maven-workspace\spaceVideospringboot\springboot0906\target\test-classes;D:\maven-workspace\spaceVideospringboot\springboot0906\target\classes;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.7.3\spring-boot-starter-web-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-starter\2.7.3\spring-boot-starter-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot\2.7.3\spring-boot-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.7.3\spring-boot-autoconfigure-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.7.3\spring-boot-starter-logging-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;C:\Users\MZFAITHDREAM\.m2\repository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;C:\Users\MZFAITHDREAM\.m2\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.7.3\spring-boot-starter-json-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.13.3\jackson-databind-2.13.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.13.3\jackson-annotations-2.13.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.13.3\jackson-core-2.13.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.3\jackson-datatype-jdk8-2.13.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.3\jackson-datatype-jsr310-2.13.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.3\jackson-module-parameter-names-2.13.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.7.3\spring-boot-starter-tomcat-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.65\tomcat-embed-core-9.0.65.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.65\tomcat-embed-el-9.0.65.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.65\tomcat-embed-websocket-9.0.65.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-web\5.3.22\spring-web-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-beans\5.3.22\spring-beans-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-webmvc\5.3.22\spring-webmvc-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-aop\5.3.22\spring-aop-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-context\5.3.22\spring-context-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-expression\5.3.22\spring-expression-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-configuration-processor\2.7.3\spring-boot-configuration-processor-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-starter-test\2.7.3\spring-boot-starter-test-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-test\2.7.3\spring-boot-test-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-test-autoconfigure\2.7.3\spring-boot-test-autoconfigure-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\jayway\jsonpath\json-path\2.7.0\json-path-2.7.0.jar;C:\Users\MZFAITHDREAM\.m2\repository\net\minidev\json-smart\2.4.8\json-smart-2.4.8.jar;C:\Users\MZFAITHDREAM\.m2\repository\net\minidev\accessors-smart\2.4.8\accessors-smart-2.4.8.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\ow2\asm\asm\9.1\asm-9.1.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;C:\Users\MZFAITHDREAM\.m2\repository\jakarta\xml\bind\jakarta.xml.bind-api\2.3.3\jakarta.xml.bind-api-2.3.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\jakarta\activation\jakarta.activation-api\1.2.2\jakarta.activation-api-1.2.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\assertj\assertj-core\3.22.0\assertj-core-3.22.0.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\hamcrest\hamcrest\2.2\hamcrest-2.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\junit\jupiter\junit-jupiter\5.8.2\junit-jupiter-5.8.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\junit\jupiter\junit-jupiter-api\5.8.2\junit-jupiter-api-5.8.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\junit\platform\junit-platform-commons\1.8.2\junit-platform-commons-1.8.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\apiguardian\apiguardian-api\1.1.2\apiguardian-api-1.1.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\junit\jupiter\junit-jupiter-params\5.8.2\junit-jupiter-params-5.8.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\junit\jupiter\junit-jupiter-engine\5.8.2\junit-jupiter-engine-5.8.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\junit\platform\junit-platform-engine\1.8.2\junit-platform-engine-1.8.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\mockito\mockito-core\4.5.1\mockito-core-4.5.1.jar;C:\Users\MZFAITHDREAM\.m2\repository\net\bytebuddy\byte-buddy\1.12.13\byte-buddy-1.12.13.jar;C:\Users\MZFAITHDREAM\.m2\repository\net\bytebuddy\byte-buddy-agent\1.12.13\byte-buddy-agent-1.12.13.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\objenesis\objenesis\3.2\objenesis-3.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\mockito\mockito-junit-jupiter\4.5.1\mockito-junit-jupiter-4.5.1.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\skyscreamer\jsonassert\1.5.1\jsonassert-1.5.1.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-core\5.3.22\spring-core-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-jcl\5.3.22\spring-jcl-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-test\5.3.22\spring-test-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\xmlunit\xmlunit-core\2.9.0\xmlunit-core-2.9.0.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit5 com.bing.springboot0906.Springboot0906ApplicationTests,contextLoads
21:59:57.434 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
21:59:57.434 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
21:59:57.481 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.bing.springboot0906.Springboot0906ApplicationTests] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
21:59:57.481 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.bing.springboot0906.Springboot0906ApplicationTests], using SpringBootContextLoader
21:59:57.497 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.bing.springboot0906.Springboot0906ApplicationTests]: class path resource [com/bing/springboot0906/Springboot0906ApplicationTests-context.xml] does not exist
21:59:57.497 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.bing.springboot0906.Springboot0906ApplicationTests]: class path resource [com/bing/springboot0906/Springboot0906ApplicationTestsContext.groovy] does not exist
21:59:57.497 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.bing.springboot0906.Springboot0906ApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
21:59:57.497 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.bing.springboot0906.Springboot0906ApplicationTests]: Springboot0906ApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
21:59:57.544 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.bing.springboot0906.Springboot0906ApplicationTests]
21:59:57.606 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [D:\maven-workspace\spaceVideospringboot\springboot0906\target\classes\com\bing\springboot0906\Springboot0906Application.class]
21:59:57.606 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.bing.springboot0906.Springboot0906Application for test class com.bing.springboot0906.Springboot0906ApplicationTests
21:59:57.700 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.bing.springboot0906.Springboot0906ApplicationTests]: using defaults.
21:59:57.700 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
21:59:57.715 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
21:59:57.715 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
21:59:57.715 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@1c5920df, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@17f9d882, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@79e4c792, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@196a42c3, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@4c60d6e9, org.springframework.test.context.support.DirtiesContextTestExecutionListener@15043a2f, org.springframework.test.context.event.EventPublishingTestExecutionListener@4a83a74a, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@1349883, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@4b29d1d2, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@7f485fda, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@28261e8e, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@d737b89, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@8519cb4]
21:59:57.731 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@5b218417 testClass = Springboot0906ApplicationTests, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@645aa696 testClass = Springboot0906ApplicationTests, locations = '{}', classes = '{class com.bing.springboot0906.Springboot0906Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@7334aada, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@52e6fdee, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@4f6ee6e4, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@79924b, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@38c6f217, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@7cef4e59], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null].
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.3)
2022-09-06 21:59:58.184  INFO 8032 --- [           main] c.b.s.Springboot0906ApplicationTests     : Starting Springboot0906ApplicationTests using Java 1.8.0_171 on HELLOWWORLD with PID 8032 (started by MZFAITHDREAM in D:\maven-workspace\spaceVideospringboot\springboot0906)
2022-09-06 21:59:58.184  INFO 8032 --- [           main] c.b.s.Springboot0906ApplicationTests     : No active profile set, falling back to 1 default profile: "default"
这里是无惨构造
2022-09-06 21:59:59.949  INFO 8032 --- [           main] c.b.s.Springboot0906ApplicationTests     : Started Springboot0906ApplicationTests in 2.181 seconds (JVM running for 3.486)
dog{name='小王', age=3}
Process finished with exit code 0

第三个作用:

package com.bing.springboot0906.pojo;
import com.sun.istack.internal.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.EnableLoadTimeWeaving;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import java.util.Date;
import java.util.List;
import java.util.Map;
//报红不影响  将其绑定
//@ConfigurationProperties(prefix = "person")
//指定配置文件 方式二
//@PropertySource(value = "classpath:application.properties")
@Component
@ConfigurationProperties(prefix = "person")
//@Validated
public class Person {
    //Spring EL表达式
//    @Value("${name}")
    private  String lastname;
//    @Value("${age}")
    private  Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String, Object> map;
    private List<Object>lists;
    private Dog dog;
    public Person() {
    }
    public Person(String name, Integer age, Boolean happy, Date birth, Map<String, Object> map, List<Object> lists, com.bing.springboot0906.pojo.Dog dog) {
        this.lastname = name;
        this.age = age;
        this.happy = happy;
        this.birth = birth;
        this.map = map;
        this.lists = lists;
        this.dog = dog;
    }
    public String getName() {
        return lastname;
    }
    public void setName(String name) {
        this.lastname = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Boolean getHappy() {
        return happy;
    }
    public void setHappy(Boolean happy) {
        this.happy = happy;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
    public Map<String, Object> getMap() {
        return map;
    }
    public void setMap(Map<String, Object> map) {
        this.map = map;
    }
    public List<Object> getLists() {
        return lists;
    }
    public void setLists(List<Object> lists) {
        this.lists = lists;
    }
    public com.bing.springboot0906.pojo.Dog getDog() {
        return dog;
    }
    public void setDog(com.bing.springboot0906.pojo.Dog dog) {
        this.dog = dog;
    }
    @Override
    public String toString() {
        return "person{" +
                "name='" + lastname + '\'' +
                ", age=" + age +
                ", happy=" + happy +
                ", birth=" + birth +
                ", map=" + map +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}
  #  dog:
  #  name: 旺财
  #  age: 123
  person:
    lastname: xiaowang
    age: 3
    happy: false
    birth: 2000/01/01
    map: {k1: v1,k2: v2}
package com.bing.springboot0906;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Springboot0906Application {
    public static void main(String[] args) {
        SpringApplication.run(Springboot0906Application.class, args);
    }
}
package com.bing.springboot0906;
import com.bing.springboot0906.pojo.Dog;
import com.bing.springboot0906.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot0906ApplicationTests {
    /**
     * 自动注入值
     */
//  1  private Person person;
//  2  private Dog dog;
//    private Animal animal;
    @Autowired
//    private Dog dog;
    private Person person;
    @Test
    void contextLoads() {
       System.out.println(person);
//        System.out.println(person);
//            System.out.println(animal);
    }
}
"C:\Program Files\Java\jdk1.8.0_171\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar=51823:D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\bin" -Dfile.encoding=UTF-8 -classpath "D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\lib\idea_rt.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\junit\platform\junit-platform-launcher\1.8.2\junit-platform-launcher-1.8.2.jar;D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\plugins\junit\lib\junit5-rt.jar;D:\IDEA\com.text\IntelliJ IDEA 2020.1.2\plugins\junit\lib\junit-rt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_171\jre\lib\rt.jar;D:\maven-workspace\spaceVideospringboot\springboot0906\target\test-classes;D:\maven-workspace\spaceVideospringboot\springboot0906\target\classes;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-starter-web\2.7.3\spring-boot-starter-web-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-starter\2.7.3\spring-boot-starter-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot\2.7.3\spring-boot-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\2.7.3\spring-boot-autoconfigure-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-starter-logging\2.7.3\spring-boot-starter-logging-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;C:\Users\MZFAITHDREAM\.m2\repository\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;C:\Users\MZFAITHDREAM\.m2\repository\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-starter-json\2.7.3\spring-boot-starter-json-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.13.3\jackson-databind-2.13.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.13.3\jackson-annotations-2.13.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.13.3\jackson-core-2.13.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.3\jackson-datatype-jdk8-2.13.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.3\jackson-datatype-jsr310-2.13.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.3\jackson-module-parameter-names-2.13.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\2.7.3\spring-boot-starter-tomcat-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.65\tomcat-embed-core-9.0.65.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.65\tomcat-embed-el-9.0.65.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.65\tomcat-embed-websocket-9.0.65.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-web\5.3.22\spring-web-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-beans\5.3.22\spring-beans-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-webmvc\5.3.22\spring-webmvc-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-aop\5.3.22\spring-aop-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-context\5.3.22\spring-context-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-expression\5.3.22\spring-expression-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-configuration-processor\2.7.3\spring-boot-configuration-processor-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-starter-test\2.7.3\spring-boot-starter-test-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-test\2.7.3\spring-boot-test-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\boot\spring-boot-test-autoconfigure\2.7.3\spring-boot-test-autoconfigure-2.7.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\jayway\jsonpath\json-path\2.7.0\json-path-2.7.0.jar;C:\Users\MZFAITHDREAM\.m2\repository\net\minidev\json-smart\2.4.8\json-smart-2.4.8.jar;C:\Users\MZFAITHDREAM\.m2\repository\net\minidev\accessors-smart\2.4.8\accessors-smart-2.4.8.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\ow2\asm\asm\9.1\asm-9.1.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;C:\Users\MZFAITHDREAM\.m2\repository\jakarta\xml\bind\jakarta.xml.bind-api\2.3.3\jakarta.xml.bind-api-2.3.3.jar;C:\Users\MZFAITHDREAM\.m2\repository\jakarta\activation\jakarta.activation-api\1.2.2\jakarta.activation-api-1.2.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\assertj\assertj-core\3.22.0\assertj-core-3.22.0.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\hamcrest\hamcrest\2.2\hamcrest-2.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\junit\jupiter\junit-jupiter\5.8.2\junit-jupiter-5.8.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\junit\jupiter\junit-jupiter-api\5.8.2\junit-jupiter-api-5.8.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\opentest4j\opentest4j\1.2.0\opentest4j-1.2.0.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\junit\platform\junit-platform-commons\1.8.2\junit-platform-commons-1.8.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\apiguardian\apiguardian-api\1.1.2\apiguardian-api-1.1.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\junit\jupiter\junit-jupiter-params\5.8.2\junit-jupiter-params-5.8.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\junit\jupiter\junit-jupiter-engine\5.8.2\junit-jupiter-engine-5.8.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\junit\platform\junit-platform-engine\1.8.2\junit-platform-engine-1.8.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\mockito\mockito-core\4.5.1\mockito-core-4.5.1.jar;C:\Users\MZFAITHDREAM\.m2\repository\net\bytebuddy\byte-buddy\1.12.13\byte-buddy-1.12.13.jar;C:\Users\MZFAITHDREAM\.m2\repository\net\bytebuddy\byte-buddy-agent\1.12.13\byte-buddy-agent-1.12.13.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\objenesis\objenesis\3.2\objenesis-3.2.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\mockito\mockito-junit-jupiter\4.5.1\mockito-junit-jupiter-4.5.1.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\skyscreamer\jsonassert\1.5.1\jsonassert-1.5.1.jar;C:\Users\MZFAITHDREAM\.m2\repository\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-core\5.3.22\spring-core-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-jcl\5.3.22\spring-jcl-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\springframework\spring-test\5.3.22\spring-test-5.3.22.jar;C:\Users\MZFAITHDREAM\.m2\repository\org\xmlunit\xmlunit-core\2.9.0\xmlunit-core-2.9.0.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit5 com.bing.springboot0906.Springboot0906ApplicationTests,contextLoads
22:22:15.799 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
22:22:15.799 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
22:22:15.830 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.bing.springboot0906.Springboot0906ApplicationTests] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
22:22:15.846 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.bing.springboot0906.Springboot0906ApplicationTests], using SpringBootContextLoader
22:22:15.846 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.bing.springboot0906.Springboot0906ApplicationTests]: class path resource [com/bing/springboot0906/Springboot0906ApplicationTests-context.xml] does not exist
22:22:15.846 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.bing.springboot0906.Springboot0906ApplicationTests]: class path resource [com/bing/springboot0906/Springboot0906ApplicationTestsContext.groovy] does not exist
22:22:15.846 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.bing.springboot0906.Springboot0906ApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
22:22:15.846 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.bing.springboot0906.Springboot0906ApplicationTests]: Springboot0906ApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
22:22:15.893 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.bing.springboot0906.Springboot0906ApplicationTests]
22:22:15.955 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [D:\maven-workspace\spaceVideospringboot\springboot0906\target\classes\com\bing\springboot0906\Springboot0906Application.class]
22:22:15.955 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.bing.springboot0906.Springboot0906Application for test class com.bing.springboot0906.Springboot0906ApplicationTests
22:22:16.080 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.bing.springboot0906.Springboot0906ApplicationTests]: using defaults.
22:22:16.080 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
22:22:16.096 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
22:22:16.096 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
22:22:16.096 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@1c5920df, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@17f9d882, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@79e4c792, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@196a42c3, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@4c60d6e9, org.springframework.test.context.support.DirtiesContextTestExecutionListener@15043a2f, org.springframework.test.context.event.EventPublishingTestExecutionListener@4a83a74a, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@1349883, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@4b29d1d2, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@7f485fda, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@28261e8e, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@d737b89, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@8519cb4]
22:22:16.096 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@5b218417 testClass = Springboot0906ApplicationTests, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@645aa696 testClass = Springboot0906ApplicationTests, locations = '{}', classes = '{class com.bing.springboot0906.Springboot0906Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@7334aada, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@52e6fdee, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@4f6ee6e4, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@79924b, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@38c6f217, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@7cef4e59], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null].
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.3)
2022-09-06 22:22:16.455  INFO 21972 --- [           main] c.b.s.Springboot0906ApplicationTests     : Starting Springboot0906ApplicationTests using Java 1.8.0_171 on HELLOWWORLD with PID 21972 (started by MZFAITHDREAM in D:\maven-workspace\spaceVideospringboot\springboot0906)
2022-09-06 22:22:16.455  INFO 21972 --- [           main] c.b.s.Springboot0906ApplicationTests     : No active profile set, falling back to 1 default profile: "default"
2022-09-06 22:22:18.095  INFO 21972 --- [           main] c.b.s.Springboot0906ApplicationTests     : Started Springboot0906ApplicationTests in 1.947 seconds (JVM running for 3.193)
person{name='null', age=3, happy=false, birth=Sat Jan 01 00:00:00 CST 2000, map={k1=v1, k2=v2}, lists=null, dog=null}
Process finished with exit code 0
package com.bing.springboot0906.pojo;
import com.sun.istack.internal.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.EnableLoadTimeWeaving;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import java.util.Date;
import java.util.List;
import java.util.Map;
//报红不影响  将其绑定
//@ConfigurationProperties(prefix = "person")
//指定配置文件 方式二
//@PropertySource(value = "classpath:application.properties")
@Component
@ConfigurationProperties(prefix = "person")
//@Validated
public class Person {
    //Spring EL表达式
//    @Value("${name}")
    private  String name;
//    @Value("${age}")
    private  Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String, Object> map;
    private List<Object>lists;
    private Dog dog;
   }


package com.bing.springboot0906.pojo;
import com.sun.istack.internal.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
//自动注入
@ConfigurationProperties(prefix = "animal")
//@Validated
//@Email(message="邮箱格式错误") //数据校验
public class Animal {
    private  String name;
    private  Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String, Object> map;
    private List<Object> lists;
    private Person person;
    public Animal() {
        System.out.println("这里是无惨构造");
    }


package com.bing.springboot0906;
import com.bing.springboot0906.pojo.Animal;
import com.bing.springboot0906.pojo.Dog;
import com.bing.springboot0906.pojo.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot0906ApplicationTests {
    /**
     * 自动注入值
     */
//  1  private Person person;
//  2  private Dog dog;
    @Autowired
//    private Dog dog;
    private Person person;
    @Test
    void contextLoads() {
        System.out.println(person);
//            System.out.println(animal);
    }
}

总结:

对比小结** @Value这个使用起来并不友好!我们需要为每个属性单独注解赋值,比较麻烦;我们来看个功能对比图

1、@ConfigurationProperties只需要写一次即可 , @Value则需要每个字段都添加

2、松散绑定:这个什么意思呢? 比如我的yml中写的last-name,这个和lastName是一样的, - 后面跟着的字母默认是大写的。这就是松散绑定。可以测试一下

3、JSR303数据校验 , 这个就是我们可以在字段是增加一层过滤器验证 , 可以保证数据的合法性

4、复杂类型封装,yml中可以封装对象 , 使用value就不支持

结论:

配置yml和配置properties都可以获取到值 , 强烈推荐 yml;

如果我们在某个业务中,只需要获取配置文件中的某个值,可以使用一下 @value;

如果说,我们专门编写了一个JavaBean来和配置文件进行一一映射,就直接@configurationProperties。

确认以上配置都OK之后,我们去测试类中测试一下:

**@SpringBootTest**
**class DemoApplicationTests {**
@Autowired
Person person; //将person自动注入进来
@Test
public void contextLoads() {
    System.out.println(person); //打印person信息
}
**结果:所有值全部注入成功!**

yaml配置注入到实体类完全OK!

1、将配置文件的key 值 和 属性的值设置为不一样,则结果输出为null,注入失败


2、在配置一个person2,然后将 @ConfigurationProperties(prefix = "person2") 指向我们的person2;


加载指定的配置文件 @PropertySource :加载指定的配置文件;


@configurationProperties:默认从全局配置文件中获取值;


1、我们去在resources目录下新建一个person.properties文件


name=kuangshen 2、然后在我们的代码中指定加载person.properties文件


@PropertySource(value = "classpath:person.properties") @Component //注册bean public class Person {


@Value("${name}")

private String name;

再次输出测试一下:指定配置文件绑定成功!

配置文件占位符配置文件还可以编写占位符生成随机数

person:**
   name: qinjiang${random.uuid} # 随机uuid
   age: ${random.int}  # 随机int
   happy: false
   birth: 2000/01/01
   maps: {k1: v1,k2: v2}
   lists:
     - code
     - girl
     - music
   dog:**
    name: ${person.hello:other}_旺财
     age: 1

回顾properties配置 我们上面采用的yaml方法都是最简单的方式,开发中最常用的;也是springboot所推荐的!那我们来唠唠其他的实现方式,道理都是相同的;写还是那样写;配置文件除了yml还有我们之前常用的properties , 我们没有讲,我们来唠唠!


【注意】properties配置文件在写中文的时候,会有乱码 , 我们需要去IDEA中设置编码格式为UTF-8;


settings-->FileEncodings 中配置;

第二部分 JSR303数据校验:

Springboot中可以用@validated来校验数据,如果数据异常则会统一抛出异常,方便异常中心统一处理。我们这里来写个注解让我们的name只能支持Email格式;

@Component //注册bean @ConfigurationProperties(prefix = "person") @Validated //数据校验 public class Person {
@Email(message="邮箱格式错误") //name必须是邮箱格式
private String name;
} 运行结果 :default message [不是一个合法的电子邮件地址];

使用数据校验,可以保证数据的正确性;

常见参数 @NotNull(message="名字不能为空") private String userName; @Max(value=120,message="年龄最大不能查过120") private int age; @Email(message="邮箱格式错误") private String email;


空检查 @Null 验证对象是否为null @NotNull 验证对象是否不为null, 无法查检长度为0的字符串 @NotBlank 检查约束字符串是不是Null还有被Trim的长度是否大于0,只对字符串,且会去掉前后空格. @NotEmpty 检查约束元素是否为NULL或者是EMPTY. Booelan检查 @AssertTrue 验证 Boolean 对象是否为 true @AssertFalse 验证 Boolean 对象是否为 false 长度检查 @Size(min=, max=) 验证对象(Array,Collection,Map,String)长度是否在给定的范围之内 @Length(min=, max=) string is between min and max included.


日期检查 @Past 验证 Date 和 Calendar 对象是否在当前时间之前 @Future 验证 Date 和 Calendar 对象是否在当前时间之后 @Pattern 验证 String 对象是否符合正则表达式的规则


.......等等 除此以外,我们还可以自定义一些数据校验规则 多环境切换


profile是Spring对不同环境提供不同配置功能的支持,可以通过激活不同的环境版本,实现快速切换环境;


多配置文件 我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml , 用来指定多个环境版本;

例如:

application-test.properties 代表测试环境配置


application-dev.properties 代表开发环境配置


但是Springboot并不会直接启动这些配置文件,它默认使用application.properties主配置文件;


我们需要通过一个配置来选择需要激活的环境:


#比如在配置文件中指定使用dev环境,我们可以通过设置不同的端口号进行测试; #我们启动SpringBoot,就可以看到已经切换到dev下的配置了; spring.profiles.active=dev yaml的多文档块 和properties配置文件中一样,但是使用yml去实现不需要创建多个配置文件,更加方便了 !


server: port: 8081 #选择要激活那个环境块 spring: profiles: active: prod


server: port: 8083 spring: profiles: dev #配置环境的名称


server: port: 8084 spring: profiles: prod #配置环境的名称


注意:如果yml和properties同时都配置了端口,并且没有激活其他环境 , 默认会使用properties配置文件的!


配置文件加载位置 外部加载配置文件的方式十分多,我们选择最常用的即可,在开发的资源文件中进行配置!


官方外部配置文件说明参考文档


springboot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件:

优先级1:项目路径下的config文件夹配置文件 优先级2:项目路径下配置文件 优先级3:资源路径下的config文件夹配置文件 优先级4:资源路径下配置文件 优先级由高到底,高优先级的配置会覆盖低优先级的配置;


SpringBoot会从这四个位置全部加载主配置文件;互补配置;


我们在最低级的配置文件中设置一个项目访问路径的配置来测试互补问题;


#配置项目的访问路径 server.servlet.context-path=/kuang 拓展,运维小技巧 指定位置加载配置文件

我们还可以通过spring.config.location来改变默认的配置文件位置


项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;这种情况,一般是后期运维做的多,相同配置,外部指定的配置文件优先级最高


java -jar spring-boot-config.jar --spring.config.location=F:/application.properties


相关文章
|
1月前
|
XML C语言 数据格式
yaml基本语法
yaml基本语法
22 0
|
1月前
|
Java
SpringBoot之yaml
SpringBoot之yaml
|
2月前
|
XML Java 数据格式
SpringBoot中yaml格式、语法规则及数据读取方式(3种)
SpringBoot中yaml格式、语法规则及数据读取方式(3种)
73 0
|
4月前
|
前端开发 Java 数据库连接
笔记(springboot、里面有部分前端(th语法)
笔记(springboot、里面有部分前端(th语法)
|
1月前
|
Java C++
SpringBoot中的properties,yml,yaml的区别
SpringBoot中的properties,yml,yaml的区别
25 0
|
4月前
|
Kubernetes Java 容器
k8s部署springboot项目yaml
k8s部署springboot项目yaml
88 0
|
6月前
|
XML JSON Java
SpringBoot入门(三) 之springboot的配置配置文件以及yaml的使用
SpringBoot入门(三) 之springboot的配置配置文件以及yaml的使用
125 0
SpringBoot入门(三) 之springboot的配置配置文件以及yaml的使用
|
20天前
|
存储 JSON JavaScript
【YAML语法规范指南】从入门到精通,揭秘神秘语法,引领配置文件解析指南(基础结构篇)
"YAML Ain't Markup Language"(简称YAML)是一种专为人类设计的数据序列化语言,适用于多种现代编程语言,可广泛应用于各类日常任务。它是一种以人类可读形式呈现的、适用于多种语言的Unicode数据序列化标准。它基于敏捷编程中常见的本地数据结构,广泛应用于配置文件、互联网消息传递、对象持久化以及数据审计等多个领域。遵循Unicode标准、
37 8
【YAML语法规范指南】从入门到精通,揭秘神秘语法,引领配置文件解析指南(基础结构篇)
|
6月前
|
前端开发 Java 数据库
SpringBoot解析指定Yaml配置文件
最近在看某个开源项目代码并准备参与其中,代码过了一遍后发现多个自定义的配置文件用来装载业务配置代替数据库查询,直接响应给前端,这里简单记录一下实现过程。
219 0
|
6月前
|
Java
Springboot的两种配置文件语法详细介绍
Springboot的两种配置文件语法详细介绍
31 0

相关产品

  • 云迁移中心