Commit a979ed8e123ce0c764f924b54e49516d36f19271
1 parent
a7d7c917
1.修改配置加载(以前@Value setter实现在特别情况下会出现问题)
Showing
2 changed files
with
37 additions
and
17 deletions
src/main/java/com/bsth/Application.java
| ... | ... | @@ -2,23 +2,26 @@ package com.bsth; |
| 2 | 2 | |
| 3 | 3 | import com.bsth.util.AppProperties; |
| 4 | 4 | import org.springframework.beans.factory.annotation.Autowired; |
| 5 | +import org.springframework.beans.factory.annotation.Value; | |
| 6 | +import org.springframework.boot.CommandLineRunner; | |
| 5 | 7 | import org.springframework.boot.SpringApplication; |
| 6 | 8 | import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 7 | 9 | import org.springframework.boot.builder.SpringApplicationBuilder; |
| 8 | 10 | import org.springframework.boot.web.support.SpringBootServletInitializer; |
| 11 | +import org.springframework.core.env.ConfigurableEnvironment; | |
| 9 | 12 | |
| 13 | +import java.io.IOException; | |
| 14 | +import java.io.InputStream; | |
| 10 | 15 | import java.lang.management.ManagementFactory; |
| 11 | 16 | import java.lang.management.RuntimeMXBean; |
| 12 | 17 | import java.util.List; |
| 18 | +import java.util.Properties; | |
| 13 | 19 | import java.util.concurrent.Executors; |
| 14 | 20 | import java.util.concurrent.ScheduledExecutorService; |
| 15 | 21 | |
| 16 | 22 | @SpringBootApplication |
| 17 | 23 | public class Application extends SpringBootServletInitializer { |
| 18 | 24 | |
| 19 | - @Autowired | |
| 20 | - private AppProperties appProperties; | |
| 21 | - | |
| 22 | 25 | public static ScheduledExecutorService mainServices = Executors.newScheduledThreadPool(10); |
| 23 | 26 | |
| 24 | 27 | @Override |
| ... | ... | @@ -38,8 +41,29 @@ public class Application extends SpringBootServletInitializer { |
| 38 | 41 | } |
| 39 | 42 | |
| 40 | 43 | if (normal) { |
| 44 | + loadProperties(); | |
| 41 | 45 | SpringApplication.run(Application.class, args); |
| 42 | 46 | } |
| 43 | 47 | } |
| 44 | 48 | |
| 49 | + private static void loadProperties() { | |
| 50 | + Properties prop = new Properties(); | |
| 51 | + try (InputStream input = Application.class.getClassLoader().getResourceAsStream("application.properties")) { | |
| 52 | + prop.load(input); | |
| 53 | + String activeProfile = prop.getProperty("spring.profiles.active", ""); | |
| 54 | + String cacheDays = prop.getProperty("cache.days", "15"); | |
| 55 | + InputStream input1 = Application.class.getClassLoader().getResourceAsStream(String.format("application-%s.properties", activeProfile)); | |
| 56 | + prop.load(input1); | |
| 57 | + String controlUrl = prop.getProperty("http.control.service_data_url", ""); | |
| 58 | + String secretKey = prop.getProperty("http.control.secret.key", ""); | |
| 59 | + String gpsUrl = prop.getProperty("http.gps.real.url", ""); | |
| 60 | + | |
| 61 | + AppProperties.setCacheDays(Integer.parseInt(cacheDays)); | |
| 62 | + AppProperties.setControlUrl(controlUrl); | |
| 63 | + AppProperties.setSecretKey(secretKey); | |
| 64 | + AppProperties.setGpsUrl(gpsUrl); | |
| 65 | + } catch (IOException e) { | |
| 66 | + throw new RuntimeException(e); | |
| 67 | + } | |
| 68 | + } | |
| 45 | 69 | } |
| 46 | 70 | \ No newline at end of file | ... | ... |
src/main/java/com/bsth/util/AppProperties.java
| ... | ... | @@ -17,39 +17,35 @@ public class AppProperties { |
| 17 | 17 | |
| 18 | 18 | private static String gpsUrl; |
| 19 | 19 | |
| 20 | - @Value("${cache.days}") | |
| 21 | - public void setCacheDays(int cacheDays) { | |
| 22 | - AppProperties.cacheDays = cacheDays; | |
| 23 | - } | |
| 24 | - | |
| 25 | 20 | public static int getCacheDays() { |
| 26 | 21 | return cacheDays; |
| 27 | 22 | } |
| 28 | 23 | |
| 29 | - @Value("${http.control.service_data_url}") | |
| 30 | - public void setControlUrl(String url) { | |
| 31 | - AppProperties.controlUrl = url; | |
| 24 | + public static void setCacheDays(int cacheDays) { | |
| 25 | + AppProperties.cacheDays = cacheDays; | |
| 32 | 26 | } |
| 33 | 27 | |
| 34 | 28 | public static String getControlUrl() { |
| 35 | 29 | return controlUrl; |
| 36 | 30 | } |
| 37 | 31 | |
| 38 | - @Value("${http.control.secret.key}") | |
| 39 | - public void setSecretKey(String secretKey) { | |
| 40 | - AppProperties.secretKey = secretKey; | |
| 32 | + public static void setControlUrl(String controlUrl) { | |
| 33 | + AppProperties.controlUrl = controlUrl; | |
| 41 | 34 | } |
| 42 | 35 | |
| 43 | 36 | public static String getSecretKey() { |
| 44 | 37 | return secretKey; |
| 45 | 38 | } |
| 46 | 39 | |
| 47 | - @Value("${http.gps.real.url}") | |
| 48 | - public void setGpsUrl(String url) { | |
| 49 | - AppProperties.gpsUrl = url; | |
| 40 | + public static void setSecretKey(String secretKey) { | |
| 41 | + AppProperties.secretKey = secretKey; | |
| 50 | 42 | } |
| 51 | 43 | |
| 52 | 44 | public static String getGpsUrl() { |
| 53 | 45 | return gpsUrl; |
| 54 | 46 | } |
| 47 | + | |
| 48 | + public static void setGpsUrl(String gpsUrl) { | |
| 49 | + AppProperties.gpsUrl = gpsUrl; | |
| 50 | + } | |
| 55 | 51 | } | ... | ... |