在springboot项目中的resources目录下新建一个文件 application.yml
编写一个实体类 Dog;
package com.example.springboot02configure.pojo; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component //添加到spring组件中 public class Dog { private String name; private Integer age; }
编写一个person类
package com.example.springboot02configure.pojo; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; @Component @ConfigurationProperties(prefix = "person") public class Person { private String name; private Integer age; private Boolean happy; private Date birth; private Map<String,Object> maps; private List<Object> lists; private Dog dog; }
在yaml中写入对象
Person: name: xiaoqi age: 13 happy: false birth: 2009/01/15 maps: {k1: v1,k2: v2} lists: - code - dog dog: name: qq age: 1
在测试程序中测试
package com.example.springboot02configure; import com.example.springboot02configure.pojo.Dog; import com.example.springboot02configure.pojo.Person; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import javax.swing.*; @SpringBootTest class SpringBoot02ConfigureApplicationTests { @Autowired private Person person; @Test void contextLoads() { System.out.println(person); } }
标签:Spring,Boot,private,example,springframework,import,org,com,日记 From: https://www.cnblogs.com/bzsc/p/18044652