4. Read through application.yml:
/** * 学生实体类 * Created by ASUS on 2018/5/4 */ @Component("Student") public class Student { @Value("${student.name}") private String name; @Value("${student.age}") private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Student(String name, int age) { this.name = name; this.age = age; } public Student() { } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
/** * springboot startup class * */ @SpringBootApplication //Read application.yml in the resources directory @PropertySource ( "classpath:application.yml" ) public class Application { public static void main( String[] args ) { ApplicationContext applicationContext= SpringApplication.run(Application.class,args); Student student= (Student) applicationContext.getBean(Student.class); System.out.println("message:"+student.toString()); } }
application.yml Contents:
The configuration format of the #yml file is key:value student: name: Xiaoming age: 25
Test Results:
My motto: No, I can learn; I fall behind, I can catch up; I fall, I can stand up; I will do it.