基础框架中提供EnvironmentComponent类,可以获取系统的配置信息
基本方法
提供一些简单的环境信息获取方法
- String getEnv() 获取当前环境 dev,test,uat,staging,pro,task,sandbox
- String getProperty(String name) 获取配置的属性值
- boolean isLocal() 是否是lcoal环境
- boolean isDev() 是否是dev环境
- boolean isTest() 是否是test环境
- boolean isUat() 是否是uat环境
- boolean isStaging() 是否是staging环境
- boolean isPro() 是否是pro环境
- boolean isTask() 是否是task环境
- boolean isSandBox() 是否是sandBox环境
- String getApplicationId() 系统Id
- String getApplicationName() 系统名称
- Environment getEnvironment()
代码实现
/**
* <p>description</p >
*
* @author li
* @version 1.0
* @date 2020/5/31 14:56
*/
public class EnvironmentComponent implements EnvironmentAware {
private Environment environment;
private static final String LOCAL = "local";
private static final String DEV = "dev";
private static final String TEST = "test";
private static final String UAT = "uat";
private static final String STAGING = "staging";
private static final String TASK = "task";
private static final String PRO = "pro";
private static final String SAND_BOX = "sandBox";
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
public String getProperty(String name) {
String property = environment.getProperty(name);
return property;
}
public String getEnv() {
String activeProfile = environment.getActiveProfiles()[0];
return activeProfile;
}
public boolean isLocal() {
return LOCAL.equalsIgnoreCase(getEnv());
}
public boolean isDev() {
return DEV.equalsIgnoreCase(getEnv());
}
public boolean isTest() {
return TEST.equalsIgnoreCase(getEnv());
}
public boolean isTask() {
return TASK.equalsIgnoreCase(getEnv());
}
public boolean isUat() {
return UAT.equalsIgnoreCase(getEnv());
}
public boolean isStaging() {
return STAGING.equalsIgnoreCase(getEnv());
}
public boolean isPro() {
return PRO.equalsIgnoreCase(getEnv());
}
public boolean isSandBox() {
return SAND_BOX.equalsIgnoreCase(getEnv());
}
public String getApplicationName() {
String application = "spring.application.name";
return environment.getProperty(application);
}
public String getPort() {
String property = environment.getProperty("server.port");
return property;
}
public String getApplicationId() {
String applicationId = "spring.application.id";
return environment.getProperty(applicationId);
}
public Environment getEnvironment() {
return environment;
}
public String getDesc() {
if (isLocal()) {
return "本地环境";
} else if (isDev()) {
return "开发环境";
} else if (isTest()) {
return "测试环境";
} else if (isUat()) {
return "UAT环境";
} else if (isStaging()) {
return "预发环境";
} else if (isSandBox()) {
return "沙箱环境";
} else if (isTask() || isPro()) {
return "生产环境";
} else {
return "未知环境";
}
}
}