spring boot 命令行啓動
mvn spring-boot:run
屬性配置
//端口
server.port=8081
//上下文
server.servlet.context-path=/wei (這是2.x的配置,1.x是server.context-path)
程序中獲取配置文件中的配置
單個獲取
在application.properties 文件中添加配置
score=A
在類中定義字段並且加上@Value註解
@Value("${score}")
private String score;
批量獲取
在application.properties 文件中添加多個擁有相同前綴的配置
girl.age=19
girl.name=meilu
創建一個配置類保存這 些配置
@Component
@ConfigurationProperties(prefix = "girl")
public class GirlProperties {
private String name;
private Integer age;
public String getName() {
return name;
}
public GirlProperties setName(String name) {
this.name = name;
return this;
}
public Integer getAge() {
return age;
}
public GirlProperties setAge(Integer age) {
this.age = age;
return this;
}
}