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/
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值