spring boot的啓動與配置

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;
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章