前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot动态配置加载

SpringBoot动态配置加载

作者头像
别先生
发布2019-01-03 15:14:37
4.8K1
发布2019-01-03 15:14:37
举报
文章被收录于专栏:别先生别先生

1、SpringBoot对配置文件集中化进行管理,方便进行管理,也可以使用HttpClient进行对远程的配置文件进行获取。

创建一个类实现EnvironmentPostProcessor 接口,然后可以对配置文件获取或者添加等等操作。

代码语言:javascript
复制
 1 package com.bie.springboot;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.InputStream;
 5 import java.util.Properties;
 6 
 7 import org.springframework.boot.SpringApplication;
 8 import org.springframework.boot.env.EnvironmentPostProcessor;
 9 import org.springframework.core.env.ConfigurableEnvironment;
10 import org.springframework.core.env.PropertiesPropertySource;
11 import org.springframework.stereotype.Component;
12 
13 /**
14  * 
15  * @Description TODO
16  * @author biehl
17  * @Date 2018年12月30日 下午3:43:55 1、动态获取到配置文件信息
18  */
19 @Component
20 public class DynamicEnvironmentPostProcessor implements EnvironmentPostProcessor {
21 
22     public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
23         try {
24             InputStream is = new FileInputStream("E:\\springboot.properties");
25             Properties properties = new Properties();
26             properties.load(is);
27             PropertiesPropertySource propertySource = new PropertiesPropertySource("dynamic", properties);
28             environment.getPropertySources().addLast(propertySource);
29         } catch (Exception e) {
30             e.printStackTrace();
31         }
32         
33         
34     }
35 
36 }

2、配置文件的名称叫做springboot.properties。然后配置文件的内容如下所示:

代码语言:javascript
复制
1 springboot.name=SpringBoot

需要注意的是,需要创建一个META-INF的文件夹,然后spring.factories文件里面的内容如下所示:

spring.factories是EnvironmentPostProcessor接口的详细路径=自己的实现类的详细路径:

代码语言:javascript
复制
org.springframework.boot.env.EnvironmentPostProcessor=com.bie.springboot.DynamicEnvironmentPostProcessor

3、然后可以使用主类获取到动态配置文件里面的配置信息:

代码语言:javascript
复制
 1 package com.bie;
 2 
 3 import org.springframework.beans.BeansException;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.context.ConfigurableApplicationContext;
 7 
 8 import com.bie.springboot.DataSourceProperties;
 9 import com.bie.springboot.DynamicEnvironmentPostProcessor;
10 import com.bie.springboot.JdbcConfig;
11 import com.bie.springboot.TomcatProperties;
12 import com.bie.springboot.UserConfig;
13 
14 /**
15  * 
16  * @Description TODO
17  * @author biehl
18  * @Date 2018年12月30日 上午10:44:35
19  *
20  */
21 @SpringBootApplication
22 public class Application {
23 
24     public static void main(String[] args) {
25         ConfigurableApplicationContext run = SpringApplication.run(Application.class, args);
26         try {
27             System.out.println("SpringBoot name is " + run.getEnvironment().getProperty("springboot.name"));
28         } catch (Exception e) {
29             e.printStackTrace();
30         }    
31         
32         System.out.println("===================================================");
33         
34         // 运行结束进行关闭操作
35         run.close();
36     }
37 
38 }

运行效果如下所示:

待续......

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-12-30 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档