java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringBoot配置文件取值方法有哪些

SpringBoot配置文件取值方法有哪些?5种实战技巧详解

作者:嘉禾嘉宁papa

还在为SpringBoot获取配置值犯愁?本文手把手教你5种方法,从@Value、Environment到@ConfigurationProperties和@PropertySource,每种方式都带实例演示,帮你轻松实现配置管理,避免踩坑,强烈推荐面向对象的配置映射方式

一、简介

实际开发过程中,我们经常要获取配置文件的值,来实现我们程序的可配置化,今天我们就来看看Spring Boot里获取配置文件值的几种方式。

二、依赖和配置

2.1、maven依赖

本文中Spring Boot的版本为当前最新版(当前最新为2.5.2)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/>
    </parent>
    <groupId>com.alian</groupId>
    <artifactId>properties</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>properties</name>
    <description>Spring Boot读取配置文件的值</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${parent.version}</version>
        </dependency>
        <!--@PropertySource使用到-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>${parent.version}</version>
            <optional>true</optional>
        </dependency>
		<!--本项目也可以不引用-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${parent.version}</version>
        </dependency>
		<!--@Data或日志使用到-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.14</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

大家可以根据自己项目的需要进行选择。

2.2、application.properties配置

application.properties

#如果引用了
#<dependency>
#    <groupId>org.springframework.boot</groupId>
#    <artifactId>spring-boot-starter-web</artifactId>
#    <version>${parent.version}</version>
#</dependency>
#则配置项目名和端口
server.port=8897
server.servlet.context-path=/properties

因为我本地是建了一个web项目

三、实践(后面的实例全部采用@PostConstruct测试)

3.1、@Value方式获取

语法如下:

@Value(${配置项key:默认值})
private String configValue;

注意:@Value是org.springframework.beans.factory.annotation.Value

我们在配置文件application.properties中新增如下配置,用于测试@Value方式获取配置文件的值

#定义@Value的变量测试
#请求地址
value.request.url=https://openapi.alipay.com/gateway.do
#请求应用id
value.request.app.id=2019052960295228
#请求签名key
value.request.sign.key=FFFFEA911121494981C5FEBA599C3A99
#请邱加密key
value.request.encrypted.key=EEEE9CA14DE545768A0BD9F1435EE507

@Value测试源程序

ValueService.java

package com.alian.properties.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
@Service
public class ValueService {
    /**
     * 请求地址
     */
    @Value("${value.request.url}")
    private String url;
    /**
     * 请求应用id
     */
    @Value("${value.request.app.id}")
    private String appId;
    /**
     * 请求签名key
     */
    @Value("${value.request.sign.key}")
    private String signKey;
    /**
     * 请求加密key
     */
    @Value("${value.request.encrypted.key}")
    private String encryptedKey;
    /**
     * 超时时间(默认值设置,当没有获取到配置值时,用冒号分隔,写在冒号后面)
     */
    @Value("${value.request.timeout:20}")
    private int timeOut;
    @PostConstruct
    public void testValue() {
        System.out.println("-------------------@Value测试开始-------------------");
        System.out.println("@Value测试获取的请求地址:" + url);
        System.out.println("@Value测试获取的请求应用id:" + appId);
        System.out.println("@Value测试获取的请求签名key:" + signKey);
        System.out.println("@Value测试获取的请求加密key:" + encryptedKey);
        //如果配置文件未设置该key的值,则使用默认值
        System.out.println("@Value测试获取的默认值设置:" + timeOut);
        System.out.println("-------------------@Value测试结束-------------------");
    }
}

运行结果:

-------------------@Value测试开始-------------------
@Value测试获取的请求地址:https://openapi.alipay.com/gateway.do
@Value测试获取的请求应用id:2019052960295228
@Value测试获取的请求签名key:FFFFEA911121494981C5FEBA599C3A99
@Value测试获取的请求加密key:EEEE9CA14DE545768A0BD9F1435EE507
@Value测试获取的默认值设置:20
-------------------@Value测试结束-------------------

注意:

3.2、Environment对象获取

使用很简单,直接使用spring的注解@Autowired引入即可

    @Autowired
    private Environment environment;

注意:Environment 是org.springframework.core.env.Environment

我们继续在配置文件application.properties中新增如下配置,用于测试Environment 方式获取配置文件的值

#定义Environment的变量测试
#系统组
envir.system.group=Alian
#系统组
envir.system.level=1
#系统名称
envir.system.name=administrator
#系统密码
envir.system.password=e6fa5927cc37437ac6cbe5e988288f80

Environment测试源程序

EnvironmentService.java

package com.alian.properties.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
public class EnvironmentService {

    @Autowired
    private Environment environment;

    @PostConstruct
    public void testEnvironment() {
        System.out.println("-------------------Environment测试开始-------------------");
        System.out.println("Environment测试获取的系统组:" + environment.getProperty("envir.system.group"));
        System.out.println("Environment测试获取的系统级别:" + environment.getProperty("envir.system.level"));
        System.out.println("Environment测试获取的系统名:" + environment.getProperty("envir.system.name"));
        System.out.println("Environment测试获取的系统密码:" + environment.getProperty("envir.system.password"));
        //如果配置文件未设置该key的值,则使用默认值
        System.out.println("Environment测试获取的默认值设置:" + environment.getProperty("envir.system.init", "未设置初始化参数"));
        System.out.println("-------------------Environment测试结束-------------------");
    }
}

运行结果:

-------------------Environment测试开始-------------------
Environment测试获取的系统组:Alian
Environment测试获取的系统级别:1
Environment测试获取的系统名:administrator
Environment测试获取的系统密码:e6fa5927cc37437ac6cbe5e988288f80
Environment测试获取的默认值设置:未设置初始化参数
-------------------Environment测试结束-------------------

注意:

3.3、@ConfigurationProperties方式获取(强烈推荐)

为了更契合java的面向对象,我们采用自动配置的方式映射配置文件属性,配置完成后直接当做java对象即可使用。

我们继续在配置文件application.properties中新增如下配置,用于测试@ConfigurationProperties方式获取配置文件的值

##定义Properties的变量测试
#作者
app.author-name=Alian
#博客网站
app.web-url=https://blog.csdn.net/Alian_1223
#小程序应用id
app.micro-applet.app-id=wx4etd7e3803c6c555
#小程序应用secretId
app.micro-applet.secret-id=e6fa5627cc57437ac8cbe5e988288f80
#小程序超时时间
app.micro-applet.expire-time=3600

废话不多少,直接新建一个配置类AppProperties.java

AppProperties.java

package com.alian.properties.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(value = "app")
public class AppProperties {

    /**
     * 作者名称
     */
    private String authorName = "";

    /**
     * 博客网站
     */
    private String webUrl = "https://blog.csdn.net/Alian_1223";

    /**
     * 小程序配置
     */
    private MicroApplet microApplet;

    @Data
    public static class MicroApplet {
        /**
         * 应用id
         */
        private String appId = "";
        /**
         * secretId
         */
        private String secretId = "";
        /**
         * 过期时间
         */
        private int expireTime = 30;
    }
}

注意:

使用方法也很简单,直接使用spring的注解@Autowired引入即可

    @Autowired
    private AppProperties appProperties;

AppProperties测试源程序

PropertiesService.java

package com.alian.properties.service;

import com.alian.properties.config.AppProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
public class PropertiesService {

    @Autowired
    private AppProperties appProperties;

    @PostConstruct
    public void testProperties() {
        System.out.println("-------------------Properties测试开始-------------------");
        System.out.println("Properties测试获取的作者:" + appProperties.getAuthorName());
        System.out.println("Properties测试获取的博客地址:" + appProperties.getWebUrl());
        System.out.println("Properties测试获取的小程序应用id:" + appProperties.getMicroApplet().getAppId());
        System.out.println("Properties测试获取的小程序SecretId:" + appProperties.getMicroApplet().getSecretId());
        System.out.println("Properties测试获取的小程序超时时间:" + appProperties.getMicroApplet().getExpireTime());
        System.out.println("-------------------Properties测试结束-------------------");
    }
}

运行结果:

-------------------Properties测试开始-------------------
Properties测试获取的作者:Alian
Properties测试获取的博客地址:https://blog.csdn.net/Alian_1223
Properties测试获取的小程序应用id:wx4etd7e3803c6c555
Properties测试获取的小程序SecretId:e6fa5627cc57437ac8cbe5e988288f80
Properties测试获取的小程序超时时间:3600
-------------------Properties测试结束-------------------

注意:

3.4、@PropertySource方式获取

有时候我们会有一些特殊意义的配置,会单独用一个配置文件存储,比如数据库配置连接参数,同样我们在application.properties同级目录新建一个配置文件alian.properties,内容如下:

alian.properties

#博客用户
csdn.user-name=Alian
#博客密码
csdn.password=123456
#博客地址
csdn.blog-url=https://blog.csdn.net/Alian_1223
#博客描叙
csdn.blog-desp=来来来,一起聊干货!!!

新建一个配置类ALianProperties.java来完成映射

ALianProperties.java

package com.alian.properties.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "csdn")
@PropertySource(value = "classpath:alian.properties", encoding = "UTF-8", ignoreResourceNotFound = true) 
public class ALianProperties {

    private String userName;

    private String password;

    private String blogUrl;

    private String blogDesp;
}

注意:

使用方法也很简单,直接使用spring的注解@Autowired引入即可

	@Autowired
    private ALianProperties ALianProperties;

@PlaceSource测试源程序

PlaceSourceService .java

package com.alian.properties.service;

import com.alian.properties.config.ALianProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
public class PlaceSourceService {

    @Autowired
    private ALianProperties ALianProperties;

    @PostConstruct
    public void testPlaceSource() {
        System.out.println("-------------------PlaceSource测试开始-------------------");
        System.out.println("Properties测试获取的数据库地址:" + ALianProperties.getBlogUrl());
        System.out.println("Properties测试获取的数据库用户名:" + ALianProperties.getUserName());
        System.out.println("Properties测试获取的数据库密码:" + ALianProperties.getPassword());
        System.out.println("Properties测试获取的数据库连接参数:" + ALianProperties.getBlogDesp());
        System.out.println("-------------------PlaceSource测试结束-------------------");
    }
}

运行结果:

-------------------PlaceSource测试开始-------------------
Properties测试获取的数据库地址:https://blog.csdn.net/Alian_1223
Properties测试获取的数据库用户名:Alian
Properties测试获取的数据库密码:123456
Properties测试获取的数据库连接参数:来来来,一起聊干货!!!
-------------------PlaceSource测试结束-------------------

3.5、原生方式获取

原生方式大家都懂,就不过多介绍了,直接上代码

LoadPropertiesService.java

package com.alian.properties.service;

import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.Properties;

@Service
public class LoadPropertiesService {

    @PostConstruct
    public void testLoadProperties() {
        System.out.println("-------------------LoadProperties测试开始-------------------");
        Properties props = new Properties();
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(Objects.requireNonNull(this.getClass().getClassLoader().getResourceAsStream("load.properties")), StandardCharsets.UTF_8);
            props.load(inputStreamReader);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        System.out.println("LoadProperties测试获取的功能名称:" + props.getProperty("function.name"));
        System.out.println("LoadProperties测试获取的功能描述:" + props.getProperty("function.desp"));
        System.out.println("-------------------LoadProperties测试开始-------------------");
    }
}

运行结果:

-------------------LoadProperties测试开始-------------------
LoadProperties测试获取的功能名称:loadProperties
LoadProperties测试获取的功能描述:原生获取配置文件的值
-------------------LoadProperties测试开始-------------------

注意:

结语

本文主要介绍了几种读取配置文件的方式,实际工作中,前面三种方式用得比较多,当然以后有了spring cloud配置中心,使用我推荐的方式结合@RefreshScope即可完成配置文件动态刷新。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文