一、背景需求:
在项目中遇到多个环境配置的问题,yml可以配置springboot的配置,自己想自定义properties文件来配置自定义的内容,避免频繁改环境引起配置文件频繁修改,可以实现不同的环境加载不同的properties自定义的配置文件。
二、问题解决:
采用springboot自带的@Profile注解来加载不同的properties配置文件,实现不同的环境加载不同的properties配置文件;
目录结构如下:
image.png
application-${env}.yml是springboot各个环境的具体配置,config_dev.properties和config_prod.properties对应开发环境和生产环境的自定义配置,spring.profile.active指定了采用了那个环境,根据采用的环境来加载自定义配置文件;
三、代码配置
config_dev.properties文件内容:
### 配置client网关调用本地测试API的地址
yjs.client.access.ip = 172.23.17.178
### 配置client网关调用本地测试API的端口
yjs.client.access.port = 9080
### 配置client网关调用Remote API的公共前缀
yjs.client.access.prefix = /api/yjs/v1/index/
config_prod.properties文件内容:
### 配置client网关调用Remote API的地址
yjs.client.access.ip = api.ok.un-net.com
### 配置client网关调用Remote API的端口
yjs.client.access.port = 80
### 配置client网关调用Remote API的公共前缀
yjs.client.access.prefix = /api/yjs/v1/index/
开发环境的自定义配置文件加载
package com.unnet.yjs.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
/**
* Email: love1208tt@foxmail.com
* Copyright (c) 2019. missbe
* @author lyg 19-7-10 下午7:48
*
*
**/
@Profile("dev")
@Configuration
@PropertySource(value = "classpath:conf/config-dev.properties",ignoreResourceNotFound = true,encoding = "UTF-8")
public class PropertyDevelopmentConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
return new PropertySourcesPlaceholderConfigurer();
}
}
生产环境的自定义配置文件加载
package com.unnet.yjs.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
/**
* Email: love1208tt@foxmail.com
* Copyright (c) 2019. missbe
* @author lyg 19-7-10 下午7:48
*
*
**/
@Profile("prod")
@Configuration
@PropertySource(value = "classpath:conf/config-prod.properties",ignoreResourceNotFound = true,encoding = "UTF-8")
public class PropertyProductConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
return new PropertySourcesPlaceholderConfigurer();
}
}
自定义配置文件的配置读取类:
package com.unnet.yjs.base;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@SuppressWarnings("all")
public class PropertyFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(PropertyFactory.class);
/**调用API服务的公共访问前缀*/
private static String ACCESS_PREFIX;
/**调用API服务的IP地址*/
private static String ACCESS_IP;
/**调用API服务的端口*/
private static String ACCESS_PORT;
/**
* 文件上传类型
*/
private static String FILE_UPLOAD_TYPE;
/**
* 本地服务器地址和端口地址
*/
private static String LOCAL_IP_PORT;
/**
* 本地文件上传后需要访问服务器地址和端口地址
*/
private static String LOCAL_HREF_CONTEXT;
/**
* 对象存储端点
*/
private static String OSS_END_POINT;
/**
* 对象存储访问密钥
*/
private static String OSS_ACCESS_KEY;
/**
* 对象存储秘钥
*/
private static String OSS_SECRET_KEY;
/**
* 对象存储桶名称
*/
private static String OSS_BUCKET_NAME;
public static String getAccessPrefix() {
return ACCESS_PREFIX;
}
/**
* 如果java代码里的属性值是静态的,用上面的方式获取不到值,要使用set方法
* set方法不能是静态的,否则取不到值;要取属性值的对象必须注入到spring中,否则也取不到值
*/
@Value("${yjs.client.access.prefix}")
public void setAccessPrefix(String accessPrefix) {
ACCESS_PREFIX = accessPrefix;
}
public static String getAccessIp() {
return ACCESS_IP;
}
public static String getLocalIpPort() {
return LOCAL_IP_PORT;
}
@Value("${yjs.client.access.local.href}")
public void setLocalIpPort(String localIpPort) {
LOCAL_IP_PORT = localIpPort;
}
@Value("${yjs.client.access.ip}")
public void setAccessIp(String accessIp) {
ACCESS_IP = accessIp;
}
public static String getAccessPort() {
return ACCESS_PORT;
}
@Value("${yjs.client.access.port}")
public void setAccessPort(String accessPort) {
ACCESS_PORT = accessPort;
}
public static String getFileUploadType() {
return FILE_UPLOAD_TYPE;
}
@Value("${yjs.file.upload.type}")
public void setFileUploadType(String fileUploadType) {
FILE_UPLOAD_TYPE = fileUploadType;
}
public static String getOssEndPoint() {
return OSS_END_POINT;
}
@Value("${yjs.file.upload.oss.endpoint}")
public void setOssEndPoint(String ossEndPoint) {
OSS_END_POINT = ossEndPoint;
}
public static String getOssAccessKey() {
return OSS_ACCESS_KEY;
}
@Value("${yjs.file.upload.oss.access.key}")
public void setOssAccessKey(String ossAccessKey) {
OSS_ACCESS_KEY = ossAccessKey;
}
public static String getOssSecretKey() {
return OSS_SECRET_KEY;
}
@Value("${yjs.file.upload.oss.secret.key}")
public void setOssSecretKey(String ossSecretKey) {
OSS_SECRET_KEY = ossSecretKey;
}
public static String getOssBucketName() {
return OSS_BUCKET_NAME;
}
@Value("${yjs.file.upload.oss.bucket.name}")
public void setOssBucketName(String ossBucketName) {
OSS_BUCKET_NAME = ossBucketName;
}
public static String getLocalHrefContext() {
return LOCAL_HREF_CONTEXT;
}
@Value("${yjs.file.upload.local.http.context}")
public void setLocalHrefContext(String localHrefContext) {
LOCAL_HREF_CONTEXT = localHrefContext;
}
}
网友评论