Method 1: @value annotation
school:
name: shcool1
student:
zyk: zyk
@RestController
public class Demo1Controller {
@Value("${school.student.zyk}")
public String name;
@RequestMapping("/hello")
public String getName() {
return name;
}
}
Method 2:
Use the @configurationproperites annotation
What I want to get is school.name and school.student
@configurationproperites + class variable name, you can get multi-level yaml attributes
school:
name: school1
student: zyk
@Data
@ConfigurationProperties("school")
@Component
public class Demo2pro {
private String name;
private String student;
}
@RestController
public class Demo2Controller {
@Autowired
Demo2pro demo2pro;
@RequestMapping("/hello2")
public String getName() {
return demo2pro.getName()+demo2pro.getStudent();
}
}