@Value
@Value("${student.age:18}")
@ConfigurationProperties
class:
classroom: 501
student:
name: murphy
age: 25
@Data
public class Student {
private String name;
private Integer age;
}
@Data
@Component
@ConfigurationProperties(prefix = "class")
public class StudentProperties {
private String classroom;
private Student student = new Student();
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestController {
@Autowired
private StudentProperties studentProperties;
@org.junit.Test
public void test() {
System.out.println(studentProperties.getStudent().getAge());
System.out.println(studentProperties.getStudent().getName());
System.out.println(studentProperties.getClassroom());
}
}