2 changed files with 76 additions and 6 deletions
@ -0,0 +1,67 @@ |
|||
package com.tduck.cloud.common.util; |
|||
|
|||
import org.springframework.core.env.Environment; |
|||
|
|||
/** |
|||
* 系统环境变量枚举类 |
|||
* dev|test|prod |
|||
* |
|||
* @author jianjun liu |
|||
* @date 2020-07-03 11:14 |
|||
**/ |
|||
public enum EnvEnum { |
|||
/** |
|||
* 环境变量枚举 |
|||
*/ |
|||
DEV("dev", "开发环境"), |
|||
TEST("test", "体验环境"), |
|||
PROD("prod", "生产环境") |
|||
; |
|||
|
|||
private String code; |
|||
private String name; |
|||
private String url; |
|||
|
|||
|
|||
|
|||
EnvEnum(String code, String name) { |
|||
this.code = code; |
|||
this.name = name; |
|||
} |
|||
|
|||
public static EnvEnum getEnum(String code) { |
|||
EnvEnum[] values = EnvEnum.values(); |
|||
for (EnvEnum value : values) { |
|||
if (value.getCode().equals(code)) { |
|||
return value; |
|||
} |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
public static EnvEnum getCurrentEnv(){ |
|||
try { |
|||
Environment environment = SpringContextUtils.getBean(Environment.class); |
|||
String[] activeProfiles = environment.getActiveProfiles(); |
|||
if (activeProfiles.length > 0) { |
|||
return getEnum(activeProfiles[0]); |
|||
} |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
return null; |
|||
} |
|||
|
|||
|
|||
public String getCode() { |
|||
return code; |
|||
} |
|||
|
|||
public String getName() { |
|||
return name; |
|||
} |
|||
|
|||
public String getUrl(){ |
|||
return url; |
|||
} |
|||
} |
Loading…
Reference in new issue