1. 程式人生 > >springboot的5種讀取配置方式(3):通過application.properties讀取

springboot的5種讀取配置方式(3):通過application.properties讀取

3.通過application.properties讀取:

/**
 * 學生實體類
 * 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啟動類
 *
 */
@SpringBootApplication
//讀取resources目錄下的application.properties
@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內容:

student.name= 小康
student.age=15

測試結果:


發現了有中文亂碼問題。

測試結果為:


我的座右銘:不會,我可以學;落後,我可以追趕;跌倒,我可以站起來;我一定行。