springboot配置文件详解

springboot配置文件详解

Spring Boot弱化配置的特性让属性配置文件的使用也更加便捷,它默认支持对application.properties或application.yml属性配置文件处理,即在application.properties或application.yml文件中添加属性配置,可以使用@Value注解将属性值注入到beans中,或使用@ConfigurationProperties注解将属性值绑定到结构化的beans中

来个测试

依赖


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.0.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>org.apdoergroupId>
    <artifactId>configartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>configname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.10version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>
project>

创建一个 application.yml,这个创建项目的时候会默认给一个application.properties,个人习惯改成yml格式,两种都可以用

首先是application.yml

### 这个配置文件里面是通用配置
server:
  port: 9999
spring:
  profiles:
    active: dev

然后添加不同环境的配置文件dev,testpro

application-dev.yml

spring:
  application:
    name: pro-config-dev
myproject:
  upload:
    maxSize: 10
    allowTypes:
      - image/jpeg-dev
      - image/jpg-dev
      - image/png-dev
      - image/gif-dev
      - image/bmp-dev
  test: testParmfor@Value-dev

application-test.yml

server:
  port: 9999
myproject:
  upload:
    maxSize: 10
    allowTypes:
      - image/jpeg-test
      - image/jpg-test
      - image/png-test
      - image/gif-test
      - image/bmp-test
  test: testParmfor@Value-test
spring:
  application:
    name: pro-config-test

application-pro.yml

server:
  port: 9999
myproject:
  upload:
    maxSize: 10
    allowTypes:
      - image/jpeg-pro
      - image/jpg-pro
      - image/png-pro
      - image/gif-pro
      - image/bmp-pro
  test: testParmfor@Value-pro
spring:
  application:
    name: pro-config-pro

写一个测试类,这里包含要读取的配置

/**
 * @author apdoer
 * @version 1.0
 * @date 2019/10/22 19:07
 */
@Data
@ToString
@ConfigurationProperties(prefix = "myproject.upload")
public class UploadConfiguration {

    private int maxSize;
    /**
     * 允许的格式
     */
    private List<String> allowTypes;
}

controller里注入使用,这里提供@value注解引入和spring的依赖注入都可以

/**
 * @author apdoer
 * @version 1.0
 * @date 2019/10/22 19:05
 */
@RestController
@RequestMapping("/demo")
public class DemoController {
    private UploadConfiguration uploadConfiguration;

    @Value("${myproject.test}")
    private String testParam;


    @Autowired(required = false)
    public void setUploadConfiguration(UploadConfiguration uploadConfiguration) {
        this.uploadConfiguration = uploadConfiguration;
    }

    @GetMapping("/anno")
    public String testParmaByAnnotation(){
        return testParam;
    }

    @GetMapping("/config")
    public String testParamByConfig(){
        return uploadConfiguration.toString();
    }
}

测试

访问localhost:9999/demo/anno

springboot配置文件详解_第1张图片

访问 localhost:9999/demo/config

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Y6Cakoec-1571745437744)(99D568E9EDB34B858A14B08AEE919695)]

可以看到指定的application-dev.yml配置文件被读取成功

优先级

我们在下图的四个地方分别创建application.yml

springboot配置文件详解_第2张图片

分别改变这四个配置文件的指定环境,

比如第一个我把1里面的配置改成如下

server:
  port: 9999
spring:
  profiles:
    active: pro

访问可以看到这个指定是优先级最高的

springboot配置文件详解_第3张图片

可以依次改变各个配置文件的内容,来测试优先级,这里就直接给出来

1 > 4 > 2 >3

就是说如果优先级更高的指定了配置,优先级低的配置就不会生效

欢迎大家加入qq群 859759121,大量免费vip学习资源,一起成长,一起进步

你可能感兴趣的:(springboot)