一、Idea开启
二、解决步骤
1、自定义application.yaml
user:
name: zhangsan
age: 18
birthday: 2020-06-06
2、java对象
类加注解,@ConfigurationProperties(prefix = "user")
@Component
@ConfigurationProperties(prefix = "user")
@Data
public class User {
private String name;
private Integer age;
private String birthday;
private Map<String, Integer> fruit;
private Map<String, Integer> fruit2;
private List<String> animales;
private List<String> animales2;
}
3、启动类
增加注解 @EnableConfigurationProperties
@EnableConfigurationProperties
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldApplication.class);
}
}
4、pom 增加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
5、测试
@RestController
@RequestMapping(value = "/demo")
public class DemoController {
@Resource
private User user;
@GetMapping(value = "/yaml/user")
public String yamlUser(){
return "hello springboot"+ user.toString();
}
}