Application.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * DBSyncer Copyright 2020-2023 All Rights Reserved.
  3. */
  4. package org.dbsyncer.web;
  5. import org.dbsyncer.common.util.DateFormatUtil;
  6. import org.springframework.boot.SpringApplication;
  7. import org.springframework.boot.autoconfigure.SpringBootApplication;
  8. import org.springframework.boot.info.BuildProperties;
  9. import org.springframework.cache.annotation.EnableCaching;
  10. import org.springframework.core.io.ClassPathResource;
  11. import org.springframework.core.io.Resource;
  12. import org.springframework.core.io.support.PropertiesLoaderUtils;
  13. import org.springframework.scheduling.annotation.EnableAsync;
  14. import org.springframework.scheduling.annotation.EnableScheduling;
  15. import java.io.IOException;
  16. import java.time.ZoneId;
  17. import java.util.Properties;
  18. @EnableAsync
  19. @EnableScheduling
  20. @EnableCaching
  21. @SpringBootApplication(scanBasePackages = "org.dbsyncer")
  22. public class Application {
  23. public static void main(String[] args) throws IOException {
  24. SpringApplication application = new SpringApplication(Application.class);
  25. setProperties(application);
  26. application.run(args);
  27. }
  28. private static void setProperties(SpringApplication application) throws IOException {
  29. Resource location = new ClassPathResource("META-INF/build-info.properties");
  30. String version = "1.0.0-Release";
  31. Properties properties = new Properties();
  32. if (location.exists()) {
  33. BuildProperties build = new BuildProperties(loadFrom(location, "build"));
  34. version = build.getVersion();
  35. String buildTime = build.getTime().atZone(ZoneId.systemDefault()).format(DateFormatUtil.CHINESE_STANDARD_TIME_FORMATTER);
  36. properties.put("info.app.build.time", buildTime);
  37. }
  38. properties.put("info.app.version", version);
  39. properties.put("spring.thymeleaf.prefix", "classpath:/public/");
  40. properties.put("management.endpoints.web.base-path", "/app");
  41. properties.put("management.endpoints.web.exposure.include", "*");
  42. properties.put("management.endpoint.health.show-details", "always");
  43. properties.put("management.health.elasticsearch.enabled", false);
  44. application.setDefaultProperties(properties);
  45. }
  46. private static Properties loadFrom(Resource location, String prefix) throws IOException {
  47. String p = (prefix.endsWith(".") ? prefix : prefix + ".");
  48. Properties source = PropertiesLoaderUtils.loadProperties(location);
  49. Properties target = new Properties();
  50. for (String key : source.stringPropertyNames()) {
  51. if (key.startsWith(p)) {
  52. target.put(key.substring(p.length()), source.get(key));
  53. }
  54. }
  55. return target;
  56. }
  57. }