properties配置文件配置

背景说明

项目部署到不通服务器时,对应的配置文件可能不相同,比如:读入节目文件的访问地址等。故需要将项目某些配置文件与原有项目解耦,使得部署项目更加灵活。

maven依赖

<!-- configuration -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

配置文件内容

# [server]
device.base_url=v1/api/
device.device_set_attribute=/setDeviceAttribute
device.device_get_attribute=/getDeviceAttribute
device.device_get_device_list_type=/getDeviceListByType
device.device_get_device_info=/getDevice
device.device_add_invoke=/insertInvoke
device.device_count_attribute_data=/getDeviceAttribute/list
device.device_get_last_invoke=/getLastDeviceInvoke

配置文件读取类

注意事项 config.path是配置在application.properties的配置文件绝对路劲

package com.dm.core.config;

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

@Configuration
@EnableConfigurationProperties(SystemDeviceApi.class)
@ConfigurationProperties(prefix = SystemDeviceApi.SYSTEM_BASE_CONFIG_PREFIX,ignoreInvalidFields = true)
// 第一种配置方案,配置文件存放在配置文件夹resources目录下,config是配置文件夹下面的子目录
@PropertySource(value = {"classpath:config/SystemDeviceApi.properties"},
// 第二中配置方案,${config.path}为配置文件绝对路径,可以使用项目以外目录                
//@PropertySource(value = {"file:${config.path}SystemDeviceApi.properties"},
        ignoreResourceNotFound = false, encoding = "UTF-8", name = "SystemDeviceApi.properties")
@Data
public class SystemDeviceApi {
    public static final String SYSTEM_BASE_CONFIG_PREFIX = "device";
    private String baseUrl;
    private String deviceSetAttribute;
    private String deviceGetAttribute;
    /**
     * 分页设备列表【类型】
     */
    private String deviceGetDeviceListType;
    private String deviceGetDeviceInfo;
    /**
     * 添加设备方法
     */
    private String deviceAddInvoke;
    private String deviceCountAttributeData;
    /**
     * 查询设备方法调用详情
     */
    private String deviceGetLastInvoke;

}
  • application.properties配置文件内容
config.path=F:/properties/

版权声明:本文为qq_29013723原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。