项目部署到不通服务器时,对应的配置文件可能不相同,比如:读入节目文件的访问地址等。故需要将项目某些配置文件与原有项目解耦,使得部署项目更加灵活。
maven依赖配置文件内容org.springframework.boot spring-boot-configuration-processor true
# [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/



