3. Read through application.properties:
4 ways to read configuration through springboot (2): Read the specified file through config to know that beans can be centrally managed, but still feel that there are too many beans, then you can choose to write the information in the configuration file
/** * 学生实体类 * 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.properties in the resources directory @PropertySource ( "classpath:application.properties" ) 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.properties content:
student.name = well student.age = 15
Test Results:
Found a Chinese garbled problem.
Solve the problem of Chinese garbled characters by solving the problem of Chinese garbled characters when springboot reads the configuration file
The test results are:
My motto: No, I can learn; I fall behind, I can catch up; I fall, I can stand up; I will do it.