Browse Source

修改accountconfig配置获取方式

wangyc 7 years ago
parent
commit
80e2f97af0
1 changed files with 23 additions and 29 deletions
  1. 23 29
      src/main/java/com/uas/ps/message/Application.java

+ 23 - 29
src/main/java/com/uas/ps/message/Application.java

@@ -7,7 +7,6 @@ import com.uas.ps.core.util.ContextUtils;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.boot.context.event.ApplicationPreparedEvent;
-import org.springframework.context.ApplicationContext;
 import org.springframework.context.ApplicationListener;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Profile;
@@ -48,39 +47,34 @@ public class Application {
     }
 
     @Bean(name = "accountConfigurer")
-    @Profile(value = {"dev"})
-    public AccountConfigurer devAccountConfigurer(ApplicationContext applicationContext) {
-        return accountConfigurer(applicationContext, "classpath:config/account-dev.properties");
+    @Profile(value = {"test", "dev"})
+    public AccountConfigurer devAccountConfigurer() {
+        return accountConfigurer("dev");
     }
 
     @Bean(name = "accountConfigurer")
-    @Profile(value = {"test", "prod"})
-    public AccountConfigurer prodAccountConfigurer(ApplicationContext applicationContext) {
-        return accountConfigurer(applicationContext, "classpath:account-prod.properties");
+    @Profile(value = {"prod"})
+    public AccountConfigurer prodAccountConfigurer() {
+        return accountConfigurer("prod");
     }
 
-    private AccountConfigurer accountConfigurer(ApplicationContext applicationContext, String location) {
-        AccountConfigurer configurer = new AccountConfigurer();
-        // 解决Spring Boot应用不支持Resource.getFile方式读取配置文件的问题
-        Properties prop = getProperties(applicationContext, location);
-
-        if (prop != null) {
-            configurer.initProperties(prop);
-        } else {
-            logger.severe("Initializing is not available AccountConfigLocation on the classpath");
-        }
-        return configurer;
-    }
-
-    private Properties getProperties(ApplicationContext applicationContext, String location) {
-        Properties prop = null;
-        try (InputStream stream = applicationContext.getResource(location).getInputStream()) {
-            prop = new Properties();
-            prop.load(stream);
-        } catch (IOException e) {
-            e.printStackTrace();
-            logger.severe(" account read config file error. \n" + e.toString());
+    /**
+     * 获取当前环境下的配置
+     * @param profile
+     * @return
+     */
+    private AccountConfigurer accountConfigurer(String profile) {
+        AccountConfigurer accountConfigurer = new AccountConfigurer();
+        accountConfigurer.setApplicationContext(ContextUtils.getApplicationContext());
+        Properties properties = new Properties();
+        String configPath = "config/account-" + profile + ".properties";
+        logger.info("***: " + configPath);
+        try {
+            properties.load(this.getClass().getClassLoader().getResourceAsStream(configPath));
+        } catch (Throwable e) {
+            throw new IllegalStateException("配置加载失败" + configPath);
         }
-        return prop;
+        accountConfigurer.initProperties(properties);
+        return accountConfigurer;
     }
 }