基础框架中提供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()

代码实现

  1. /**
  2. * <p>description</p >
  3. *
  4. * @author li
  5. * @version 1.0
  6. * @date 2020/5/31 14:56
  7. */
  8. public class EnvironmentComponent implements EnvironmentAware {
  9. private Environment environment;
  10. private static final String LOCAL = "local";
  11. private static final String DEV = "dev";
  12. private static final String TEST = "test";
  13. private static final String UAT = "uat";
  14. private static final String STAGING = "staging";
  15. private static final String TASK = "task";
  16. private static final String PRO = "pro";
  17. private static final String SAND_BOX = "sandBox";
  18. @Override
  19. public void setEnvironment(Environment environment) {
  20. this.environment = environment;
  21. }
  22. public String getProperty(String name) {
  23. String property = environment.getProperty(name);
  24. return property;
  25. }
  26. public String getEnv() {
  27. String activeProfile = environment.getActiveProfiles()[0];
  28. return activeProfile;
  29. }
  30. public boolean isLocal() {
  31. return LOCAL.equalsIgnoreCase(getEnv());
  32. }
  33. public boolean isDev() {
  34. return DEV.equalsIgnoreCase(getEnv());
  35. }
  36. public boolean isTest() {
  37. return TEST.equalsIgnoreCase(getEnv());
  38. }
  39. public boolean isTask() {
  40. return TASK.equalsIgnoreCase(getEnv());
  41. }
  42. public boolean isUat() {
  43. return UAT.equalsIgnoreCase(getEnv());
  44. }
  45. public boolean isStaging() {
  46. return STAGING.equalsIgnoreCase(getEnv());
  47. }
  48. public boolean isPro() {
  49. return PRO.equalsIgnoreCase(getEnv());
  50. }
  51. public boolean isSandBox() {
  52. return SAND_BOX.equalsIgnoreCase(getEnv());
  53. }
  54. public String getApplicationName() {
  55. String application = "spring.application.name";
  56. return environment.getProperty(application);
  57. }
  58. public String getPort() {
  59. String property = environment.getProperty("server.port");
  60. return property;
  61. }
  62. public String getApplicationId() {
  63. String applicationId = "spring.application.id";
  64. return environment.getProperty(applicationId);
  65. }
  66. public Environment getEnvironment() {
  67. return environment;
  68. }
  69. public String getDesc() {
  70. if (isLocal()) {
  71. return "本地环境";
  72. } else if (isDev()) {
  73. return "开发环境";
  74. } else if (isTest()) {
  75. return "测试环境";
  76. } else if (isUat()) {
  77. return "UAT环境";
  78. } else if (isStaging()) {
  79. return "预发环境";
  80. } else if (isSandBox()) {
  81. return "沙箱环境";
  82. } else if (isTask() || isPro()) {
  83. return "生产环境";
  84. } else {
  85. return "未知环境";
  86. }
  87. }
  88. }