首页 > 编程笔记 > Java笔记

Spring Boot自定义配置属性详解

在项目开发的过程中,经常需要自定义系统业务方面的配置文件及配置项,Spring Boot 如何实现自定义属性配置呢?

其实非常简单,Spring Boot 提供了 @Value 注解、@ConfigurationProperties 注解和 Environment 接口等 3 种方式自定义配置项。

@Value注解

在实际项目中,经常需要在配置文件中定义一些简单的配置项,Spring Boot 提供 @Value 注解来设置简单的配置项,默认读取 application.properties 文件中的配置属性。

下面通过示例来演示使用 @Value 注解添加自定义配置项。

首先,在 application.properties 配置文件中添加自定义配置项:
com.weiz.costum.firstname=Zhang
com.weiz.costum.secondname=Weiz
在上面的示例中,我们添加了 firstname 和 secondname 两个自定义配置项。

然后,在使用的位置调用 @Value 注解来获取配置项的值:
@Value("${com.weiz.costum.firstname}")
private String firstName;
@Value("${com.weiz.costum.secondname}")
private String secondName;
在上面的示例中,通过 @Value 注解获取了配置文件中对应的配置项的值。

需要注意的是:

Environment接口

Environment 是 Spring 为运行环境提供的高度抽象的接口,它会自动获取系统加载的全部配置项,包括命令行参数,系统属性,系统环境,随机数,配置文件等。

使用 Environment 接口时,无须其他的额外配置,只要在使用的类中注入 Environment 即可。

下面通过示例演示 Environment 读取系统自定义的配置项。

首先,在 application.properties 配置文件中增加如下的配置项:
com.weiz.costum.firstname=Zhang
com.weiz.costum.secondname=Weiz
在上面的示例中,我们在 application.properties 中配置了 firstname 和 secondname 两个自定义配置项。

Environment 读取的是系统中所有的配置。我们既可以在 application.properties 中设置自定义的配置项,又可以在自定义配置文件中添加配置项。

然后,创建单元测试方法,并注入 Environment 读取系统配置。示例代码如下:
@Autowired
private Environment env;

@Test
void getEnv() {
    System.out.println(env.getProperty("com.weiz.costum.firstname"));
    System.out.println(env.getProperty("com.weiz.costum.secondname"));
}
上面就是 Environment 使用的示例代码,非常简单。不过,使用 Environment 时还需要注意以下两点:

@ConfigurationProperties

在实际项目开发中,需要注入的配置项非常多时,前面所讲的 @value 和 Environment 两种方法就会比较烦琐。这时可以使用注解 @ConfigurationProperties 将配置项和实体 Bean 关联起来,实现配置项和实体类字段的关联,读取配置文件数据。

下面通过示例演示 @ConfigurationProperties 注解如何读取配置文件。

1) 创建自定义配置文件

在 resources 下创建自定义的 website.properties 配置文件,示例代码如下:
com.weiz.resource.name=weiz
com.weiz.resource.website=www.weiz.com
com.weiz.resource.language=java
在上面的示例中,创建了自定义的 website.properties 配置文件。增加了 name、website、language 等三个配置项,这些配置项的名称的前缀都是 com.weiz.resource。

2) 创建实体类

创建 WebSiteProperties 自定义配置对象类,然后使用 @ConfigurationProperties 注解将配置文件中的配置项注入到自定义配置对象类中,示例代码如下:
@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;
    }
}
从上面的示例代码可以看到,我们使用了 @Configuration 注解、@ConfigurationProperties 和 @PropertySource 三个注解来定义 WebSiteProperties 实体类:
通过上面的 WebSiteProperties 类即可读取全部对应的配置项。

3) 调用配置项

使用配置实体类中的方式也非常简单,只需将 WebSiteProperties 注入到需要使用的类中,示例代码如下:
@Autowired
private WebSiteProperties website;

@Test
void getProperties() {
    System.out.println(website.getName());
    System.out.println(website.getWebsite());
    System.out.println(website.getLanguage());
}

使用配置文件注意事项

在实际项目中会碰到很多读取配置文件的应用场景,需要注意各种坑,否则会让你很惆怅。所以,我总结了一些使用配置文件时需要注意的事项:
声明:《Java系列教程》为本站“54笨鸟”官方原创,由国家机构和地方版权局所签发的权威证书所保护。