I. Start Idea
2. Solution steps
1. Customize application.yaml
user:
name: zhangsan
age: 18
birthday: 2020-06-06
Copy the code
2. Java objects
Class with annotations, @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;
}
Copy the code
3. Start classes
Add annotations @ EnableConfigurationProperties
@EnableConfigurationProperties
@SpringBootApplication
public class HelloWorldApplication {
public static void main(String[] args) { SpringApplication.run(HelloWorldApplication.class); }}Copy the code
4. Pom increases dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
Copy the code
5, test,
@RestController
@RequestMapping(value = "/demo")
public class DemoController {
@Resource
private User user;
@GetMapping(value = "/yaml/user")
public String yamlUser(a){
return "hello springboot"+ user.toString(); }}Copy the code