PropertiesUtils.java 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package com.tzld.piaoquan.api.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.commons.lang3.StringUtils;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.Properties;
  7. @Slf4j
  8. public class PropertiesUtils {
  9. public static Properties properties;
  10. static {
  11. Properties mainProperties = new Properties();
  12. properties = new Properties();
  13. try {
  14. InputStream mainStream = PropertiesUtils.class.getClassLoader().getResourceAsStream("application.properties");
  15. mainProperties.load(mainStream);
  16. // 先通过虚拟机参数 -Denv=value去取配置文件的值
  17. // 如果没有设置-Denv的虚拟机参数则说明是本地开发环境,则读取application.properties文件中spring.profiles.active的值
  18. String env = System.getProperty("env");
  19. if (StringUtils.isEmpty(env)) {
  20. env = mainProperties.getProperty("spring.profiles.active");
  21. }
  22. if (StringUtils.isEmpty(env)) {
  23. log.error("设置spring.profiles.active or 设置虚拟机启动参数 -Denv错误!!!");
  24. System.exit(1);
  25. }
  26. InputStream activeStream = PropertiesUtils.class.getClassLoader().getResourceAsStream("application-" + env + ".properties");
  27. properties.load(activeStream);
  28. properties.put("spring.profiles.active", env);
  29. log.info("开发环境为: " + env);
  30. } catch (IOException e) {
  31. log.error(e.getMessage());
  32. System.exit(1);
  33. }
  34. }
  35. public static Properties getProperties() {
  36. return properties;
  37. }
  38. public static String getProjectEnv() {
  39. return getProperties().getProperty("spring.profiles.active");
  40. }
  41. public static String getValue(String key) {
  42. return PropertiesUtils.getProperties().getProperty(key);
  43. }
  44. public static boolean getSwaggerEnabled() {
  45. return Boolean.parseBoolean(getProperties().getProperty("swagger.enabled"));
  46. }
  47. public static String getSwaggerBasePath() {
  48. return getProperties().getProperty("swagger.basePath");
  49. }
  50. public static String getSwaggerProtocols() {
  51. return getProperties().getProperty("swagger.protocols");
  52. }
  53. public static String getVideoBucket() {
  54. return getProperties().getProperty("oss.longvideo.bucket");
  55. }
  56. public static String getDownloadDomain() {
  57. return getProperties().getProperty("oss.longvideo.cdnDomain");
  58. }
  59. public static String getTranscodeLocation() {
  60. return getProperties().getProperty("oss.longvideo.transcode.location");
  61. }
  62. public static String getTranscodePipelineId() {
  63. return getProperties().getProperty("oss.longvideo.transcode.PipelineId");
  64. }
  65. public static String getReadOnlyAccessKeyId() {
  66. return getProperties().getProperty("oss.video.readonly.accessKey");
  67. }
  68. public static String getReadOnlyAccessKeySecret() {
  69. return getProperties().getProperty("oss.video.readonly.secretKey");
  70. }
  71. }