Welcome everyone

Spring Boot 配置文件篇

java 汪明鑫 712浏览 0评论

Spring Boot提供了两种常用的配置文件,分别是properties文件和yml文件。他们的作用都是修改Spring Boot自动配置的默认值。

SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用
application.properties或者application.yml(application.yaml)进行配置。
SpringBoot默认会从Resources目录下加载application.properties或application.yml(application.yaml)文件

 

application.properties  是默认生成的,

我们也比较熟悉,经常用到,配置端口,配置数据源,配置redis等等

 

还有一个yml

 

 

YML文件格式是YAML (YAML Aint Markup Language)编写的文件格式,YAML是一种直观的能够被电脑识别的的数
据数据序列化格式,并且容易被人类阅读,容易和脚本语言交互的,可以被支持YAML库的不同的编程语言程序导
入,比如: C/C++, Ruby, Python, Java, Perl, C#, PHP等。YML文件是以数据为核心的,比传统的xml方式更加简
洁。
YML文件的扩展名可以使用.yml或者.yaml
这个说实话目前来说我用的不太多,为啥有了application.properties,还要有application.yml

 

yml和xml相比,少了一些结构化的代码,使数据更直接,一目了然。

yml和json相比,没有谁好谁坏,合适才是最好的。yml的语法比json优雅,注释更标准,适合做配置文件。json作为一种机器交换格式比yml强,更适合做api调用的数据交换。

 

语法: key: value

以空格的缩进程度来控制层级关系。

空格的个数并不重要,只要左边空格对齐则视为同一个层级。

注意不能用tab代替空格。

且大小写敏感。

支持字面值,对象,数组三种数据结构,也支持复合结构。

 

举个例子:

application.yml

person:
    name: xinye
    age: 21

 

package pers.wmx.demo.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: wang ming xin
 * @create: 2019-04-03 14:42
 */

@RestController
public class ymlController {

    @Value("${person.name}")
    String name;

    @Value("${person.age}")
    int age;

    @RequestMapping("/yml")
    public String getYmlData(){
        return "name="+name+",age="+age;
    }

}

 

通过@Value 将application.yml中的值映射到字段

 

 

 

再来介绍一个注解  @ConfigurationProperties

使用 @ConfigurationProperties方式可以进行配置文件与实体字段的自动映射,但需要字段必须提供set方法才可以,

而使用@Value注解修饰的字段不需要提供set方法

 

package pers.wmx.demo.configuration;

import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: wang ming xin
 * @create: 2019-04-03 14:56
 */
@RestController
@ConfigurationProperties(prefix = "person")
@Setter
public class ymlController1 {

    String name;
    int age;

    @RequestMapping("/yml1")
    public String getYmlData(){
        return "name="+name+",age="+age;
    }

}

 

这里我用 @Setter 替代2个字段的set方法

用浏览器访问接口也是同样的效果

 

 

SpringBoot配置信息的查询
SpringBoot的配置文件,主要的目的就是对配置信息进行修改的,但在配置时的key从哪里查询
文档URL:https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#common-application-properties

 

 

SpringBoot配置文件的位置

 

可以看下官网关于配置文件的描述:

https://docs.spring.io/spring-boot/docs/2.0.1.RELEASE/reference/htmlsingle/#boot-features-external-config

 

Application Property Files

SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:

  1. A /config subdirectory of the current directory
  2. The current directory
  3. A classpath /config package
  4. The classpath root

The list is ordered by precedence (properties defined in locations higher in the list override those defined in lower locations).

 

  1. 先去项目根目录找config文件夹下找配置文件件
  2. 再去根目录下找配置文件
  3. 去resources下找cofnig文件夹下找配置文件
  4. 去resources下找配置文件

列表按优先级排序(在列表中较高位置定义的属性将覆盖在较低位置中定义的属性)。

优先级由高到低,高优先级的配置会覆盖低优先级的配置;

SpringBoot会从这个四个位置全部加载住配置文件;互补配置;

 

Config locations are searched in reverse order. By default, the configured locations are classpath:/,classpath:/config/,file:./,file:./config/. The resulting search order is the following:

  1. file:./config/
  2. file:./
  3. classpath:/config/
  4. classpath:/

 

看下这个类 org.springframework.boot.context.config.ConfigFileApplicationListener

有一个字段  默认的搜索位置
private static final String DEFAULT_SEARCH_LOCATIONS =

      "classpath:/,classpath:/config/,file:./,file:./config/";

 

还可以看到一些关键的字段:

private static final String DEFAULT_NAMES = "application";

public static final String CONFIG_NAME_PROPERTY = "spring.config.name";
    public static final String CONFIG_LOCATION_PROPERTY = "spring.config.location";
    public static final String CONFIG_ADDITIONAL_LOCATION_PROPERTY = "spring.config.additional-location";

 

项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;

指定配置文件和默认加载的这些配置文件共同起作用形成互补配置;

我们还可以通过spring.config.location来改变默认的配置文件位置,

如:

java -jar springboot-demo-0.0.1-SNAPSHOT.jar --spring.config.location=E:/application.properties

 

所有的配置也可以在命令行上进行指定

如:

java -jar springboot-demo-0.0.1-SNAPSHOT.jar --server.port=8081 --server.context-path=/abc

转载请注明:汪明鑫的个人博客 » Spring Boot 配置文件篇

喜欢 (0)

说点什么

您将是第一位评论人!

提醒
avatar
wpDiscuz