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

猜你喜欢

转载自blog.csdn.net/qqqq0199181/article/details/81355068