基本语法:
k: [v1,v2,v3]
k1:
- v1
- v2
- v3
k3: {a: v1,b: v2, c: v3}
注意,这里逗号为英文逗号
person:
name: 张三
age: 20
date: 2002/11/30
interests: [编程,小说,漫画,年代剧,弓箭,骑马]
animals:
- 舔狗
- 小丑
- 沸羊羊
org.springframework.boot
spring-boot-configuration-processor
true
如果依赖没有导入,会在加载资源类的时候提醒你
@ConfigurationProperties(prefix="person")
@Configuration
@Component
public class YkConfigProperty {
private String name;
private Integer age;
private Date date;
private List interests;
private String[] animals;
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;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public List getInterests() {
return interests;
}
public void setInterests(List interests) {
this.interests = interests;
}
public String[] getAnimals() {
return animals;
}
public void setAnimals(String[] animals) {
this.animals = animals;
}
@Bean
public void load(){
System.out.println("name='" + name + '\'' +
", age=" + age +
", date=" + date +
", interests=" + interests +
", animals=" + Arrays.toString(animals));
}
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.15)
2023-08-30 13:45:35.009 INFO 10716 --- [ main] com.example.yk.YkApplication : Starting YkApplication using Java 1.8.0_352 on DESKTOP-480FVQ7 with PID 10716 (D:\javaxiangmu\yk\target\classes started by yk in D:\javaxiangmu\yk)
2023-08-30 13:45:35.014 INFO 10716 --- [ main] com.example.yk.YkApplication : No active profile set, falling back to 1 default profile: "default"
2023-08-30 13:45:36.159 INFO 10716 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2023-08-30 13:45:36.167 INFO 10716 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2023-08-30 13:45:36.167 INFO 10716 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.79]
2023-08-30 13:45:36.261 INFO 10716 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2023-08-30 13:45:36.262 INFO 10716 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1183 ms
name='张三', age=20, date=Sat Nov 30 00:00:00 CST 2002, interests=[编程, 小说, 漫画, 年代剧, 弓箭, 骑马], animals=[舔狗, 小丑, 沸羊羊]
2023-08-30 13:45:36.594 INFO 10716 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2023-08-30 13:45:36.604 INFO 10716 --- [ main] com.example.yk.YkApplication : Started YkApplication in 2.201 seconds (JVM running for 3.515)
Description:
Failed to bind properties under 'person.date' to javax.xml.crypto.Data:
Property: person.date
Value: "2002/11/30"
Origin: class path resource [application.yml] - 4:9
Reason: org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [javax.xml.crypto.Data]
Action:
Update your application's configuration
这里告诉我们person.date存在问题,在application.yml文件里找4-9行, 这里主要是导入的类导错了,没有把data和date搞清楚,正确的应该是java.util.Date
同理,如果以后遇到类似问题:要么去查看导入的类是否正确,或者查看yml文件对应的错误位置格式是否书写正确,是少了字母,还是大小写符号有误
@ConfigurationProperties(prefix="person")
@Configuration
@Component
@ConfigurationProperties(prefix=" person"),它的意思是读取properties中所有以person开头的属性,并且与@Bean中的字段进行匹配
@Configuration注解标识的类中声明一个或者多个Bean方法
@Component注解:标识Spring管理的Bean,使用@Component注解在一个类上,表示将此类标示为Spring容器的一个Bean