Spring Boot 入门系列(二十五)读取配置文件的几种方式详解!
阅读原文时间:2021年09月09日阅读:1

在项目开发中经常会用到配置文件,之前介绍过Spring Boot 资源文件属性配置的方法,但是很多朋友反馈说介绍的不够详细全面。所以, 今天完整的分享Spring Boot读取配置文件的几种方式!

Spring Boot 支持多种格式的配置文件格式,目前最常用的配置文件格式是 properties和 yml。所以,这里默认是用.properties文件,其实,yml格式文件的用法也基本类似。Spring Boot 最常用的几种读取配置文件的方法:分别是@Value注解,@ConfigurationProperties注解和Environment接口。这三种注解可以配合着@PropertySource来使用。

一、使用@Value注解

使用@Value注解,默认读取的是application.properties。如果是自定义的配置文件,则需要用 @PropertySource 来指定具体要读取的配置文件。

1、application.properties 配置文件增加如下配置

# 自定义配置
com.weiz.costum.name=weiz-value
com.weiz.costum.website=www.weiz.com
com.weiz.costum.language=java

2、读取配置

@Value("${com.weiz.costum.name}")
private String name;
@Value("${com.weiz.costum.website}")
private String website;
@Value("${com.weiz.costum.language}")
private String language;

@RequestMapping("/getvalue")
public String getValue() {
System.out.println(name);
System.out.println(website);
System.out.println(language);
return "getvalue";
}

代码说明:

1、@Value 为读取配置的注解。需要配置完整的key路径。

2、@Value 默认读取application.properties 文件,如果需要自定义配置文件,需要通过@PropertySource 指定。

上面的代码,可以把@Value 的相关代码封装到单独的类中,在该类增加@Component注解,然后读取配置文件。然后在调用的类中注入该类即可。

二、使用Environment读取文件

Environment的使用非常方便,只要在使用的类中注入Environment,就能很方便就读取到相应的配置。

@Autowired  
private Environment env;

@RequestMapping("/getenv")  
public String getEnvironment() {  
    System.out.println(env.getProperty("com.weiz.resource.name"));  
    System.out.println(env.getProperty("com.weiz.resource.website"));  
    System.out.println(env.getProperty("com.weiz.resource.language"));  
    return "hello";  
}

代码说明:

  1、使用Environment无需指定配置文件,获取的是系统加载的全部配置文件中的配置。

   2、注意配置文件的编码格式。

三、使用@ConfigurationProperties注解

在实际项目中,当项目需要注入的变量值很多时,上述所述的@value 和 Environment 两种方法会比较繁琐,这时候我们通常使用基于类型安全的配置方式,将properties属性和一个Bean关联在一起,即使用注解@ConfigurationProperties读取配置文件数据。

1、增加自定义配置文件

在src\main\resources下新建website.properties配置文件:

com.weiz.resource.name=weiz
com.weiz.resource.website=www.weiz.com
com.weiz.resource.language=java

2、增加自定义配置对象类

首先创建WebSiteProperties 自定义配置对象类。然后,使用@ConfigurationProperties 注解将配置文件属性注入到自定义配置对象类中

package com.weiz.pojo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ConfigurationProperties(prefix = "com.weiz.resource")
@PropertySource(value = "classpath:website.properties")
public class WebSiteProperties {
private String name;
private String website;
private String language;

public String getName() {  
    return name;  
}

public void setName(String name) {  
    this.name = name;  
}

public String getWebsite() {  
    return website;  
}

public void setWebsite(String website) {  
    this.website = website;  
}

public String getLanguage() {  
    return language;  
}

public void setLanguage(String language) {  
    this.language = language;  
}  

}

代码说明:

  1、@ConfigurationProperties(prefix = "com.weiz.resource") 绑定属性,其中prefix表示所绑定的属性的前缀。

  2、@PropertySource(value = "classpath:website.properties") 指定读取的配置文件及其路径。

通过上面的WebSiteProperties类,即可读取全部对应的配置项。

3、使用配置

@Autowired  
private WebSiteProperties properties;

@RequestMapping("/getpro")  
public String getProperties() {  
    System.out.println(properties.getName());  
    System.out.println(properties.getWebsite());  
    System.out.println(properties.getLanguage());  
    return "hello";  
}

上面的代码可以看到,使用非常简单,只需将之前定义的WebSiteProperties 配置类注入即可。

在实际项目中,会碰到很多读取配置文件的业务场景,需要注意各种坑,否则会让你很惆怅。

  1、yml 文件注意空格和格式缩进。

  2、properties文件默认使用的是iso8859-1。容易出现乱码问题,如果有中文,如要指定编码格式。

  3、系统中 yml文件的加载顺序高于properties,但是读取配置信息的时候会读取后加载。

  4、@PropertySource注解默认只会加载 properties文件,yml 文件这不需要此注解。

  5、@PropertySource注解可以与任何一种方式联合使用。

  6、简单值推荐使用@Value,复杂对象推荐使用@ConfigurationProperties。

以上,就把Spring Boot如何资源文件属性配置介绍完了。

这个系列课程的完整源码,也会提供给大家。大家关注我的微信公众号(架构师精进),回复:springboot源码 ,获取这个系列课程的完整源码。