1.問題的拋出
SpringBoot已經成爲當前java應用開發的首選框架,它將程序員從以往繁瑣的spring mvc配置文件中解脫出來,使用自動配置方式構建spring應用的IOC容器,十分方便。
SpringBoot的使用者往往將需要自動配置的類的屬性放入application.yml文件中,SpringBoot應用在啓動時會按一定規則讀取應用配置文件,並把配置文件中的屬性放入應用的環境變量中,應用中部署的自動配置類會從應用的環境變量中獲取這些屬性作爲自動配置的參數完成對象的注入。
在實際應用中,配置在application.yml的屬性參數往往需要動態獲取,比較典型的如使用spring cloud config和k8s的configmap從配置中心處獲取配置數據。
如何在SpringBoot應用啓動後動態獲取屬性參數並且還讓自動配置類來使用這些屬性參數,網上有很多文章。本文介紹一種比較簡單的方法。
2.SpringBoot的自動配置過程分析
首先我們來分析一下SpringBoot的應用啓動過程,看看IOC容器是如何初始化的,在初始化的過程中可供程序員實現哪些擴展點。
SpringApplication的run方法如下所示:
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
這個過程中使用prepareEnvironment函數初始化環境變量environment,並在prepareContext函數中爲context中的bean注入做準備,在prepareContext函數中:
private void prepareContext(ConfigurableApplicationContext context,
ConfigurableEnvironment environment, SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments, Banner printedBanner) {
context.setEnvironment(environment);
postProcessApplicationContext(context);
applyInitializers(context);
listeners.contextPrepared(context);
if (this.logStartupInfo) {
logStartupInfo(context.getParent() == null);
logStartupProfileInfo(context);
}
// Add boot specific singleton beans
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
if (printedBanner != null) {
beanFactory.registerSingleton("springBootBanner", printedBanner);
}
if (beanFactory instanceof DefaultListableBeanFactory) {
((DefaultListableBeanFactory) beanFactory)
.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
}
// Load the sources
Set<Object> sources = getAllSources();
Assert.notEmpty(sources, "Sources must not be empty");
load(context, sources.toArray(new Object[0]));
listeners.contextLoaded(context);
}
applyInitializers(context)函數會執行用戶自定義的ApplicationContextInitializer,我們可以在用戶自定義的ApplicationContextInitializer中加入從遠程獲取的配置參數放入環境變量中覆蓋掉從application.yml中讀取的配置參數,這樣自動配置類從環境變量中獲取的配置參數就是我們從遠程獲取的配置參數。
3 代碼實例
假設配置文件application.yml如下:
server:
port: 9000
spring:
application:
name: zk-test1
datasource:
password: zhangkai
url: jdbc:mysql://172.16.249.205:3306/zkdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
mvc:
view:
prefix: /WEB-INF/
suffix: .jsp
我們希望從遠程獲取datasource的url地址,首先我們定義一個ApplicationContextInitializer接口的實現類ZkApplicationContextInitializer並加入到SpringApplication的Initializers中,代碼如下:
public class ZktestApplication implements ApplicationRunner {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(ZktestApplication.class);
app.addInitializers(new ZkApplicationContextInitializer());
ApplicationContext ctx = app.run( args);
}
}
ZkApplicationContextInitializer定義如下:
@Slf4j
public class ZkApplicationContextInitializer implements ApplicationContextInitializer {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
ConfigurableEnvironment ce = applicationContext.getEnvironment();
log.info("spring.datasource.url before:{}", ce.getProperty("spring.datasource.url"));
Properties properties = new Properties();
properties.put("spring.datasource.url", getRemoteConfigedDbUrl("spring.datasource.url"));
PropertiesPropertySource propertiesPropertySource = new PropertiesPropertySource("remote", properties);
ce.getPropertySources().addFirst(propertiesPropertySource);
PropertySource ps1 = ce.getPropertySources().get("applicationConfig: [classpath:/application.yml]");
log.info("applicationConfig:{}", ps1.getProperty("spring.datasource.url").toString());
PropertySource ps2 = ce.getPropertySources().get("remote");
log.info("remote:{}", ps2.getProperty("spring.datasource.url").toString());
log.info("spring.datasource.url after:{}", ce.getProperty("spring.datasource.url"));
}
private String getRemoteConfigedDbUrl(String propertname) {
return "jdbc:mysql://localhost:3306/zkdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
}
}
在這段代碼中,我們使用getRemoteConfigedDbUrl模擬向遠程獲取datasource的操作。在 initialize函數中,首先使用applicationContext.getEnvironment()獲取當前應用的環境ce,當前環境中有多個PropertySource,其中名爲“applicationConfig: [classpath:/application.yml]”的PropertySource中存放的配置參數就是從application.yml中獲取的配置參數。這裏我們建立一個名爲"remote"的PropertySource,並放在ce的PropertySource列表中。注意這裏我們使用addFirst將"remote"的PropertySource放在列表的最前面,是因爲如果多個PropertySource中如果有相同的屬性,則ce.getProperty()函數獲取的是最前面的PropertySource中的屬性值。
4 運行結果
運行ZktestApplication,可從運行結果中看到數據庫的Datasource對象使用的自動配置參數爲使用getRemoteConfigedDbUrl()函數獲取的url。其中initialize函數的執行結果如下:
2020-01-17 15:48:47.004 INFO 7672 --- [ restartedMain] c.k.z.ZkApplicationContextInitializer : spring.datasource.url before:jdbc:mysql://172.16.249.205:3306/zkdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
2020-01-17 15:48:47.008 INFO 7672 --- [ restartedMain] c.k.z.ZkApplicationContextInitializer : applicationConfig:jdbc:mysql://172.16.249.205:3306/zkdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
2020-01-17 15:48:47.008 INFO 7672 --- [ restartedMain] c.k.z.ZkApplicationContextInitializer : remote:jdbc:mysql://localhost:3306/zkdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
2020-01-17 15:48:47.008 INFO 7672 --- [ restartedMain] c.k.z.ZkApplicationContextInitializer : spring.datasource.url after:jdbc:mysql://localhost:3306/zkdb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
2020-01-17 15:48:47.012 INFO 7672 --- [ restartedMain] com.kedacom.zktest.ZktestApplication : Starting ZktestApplication on zhangkai_kedacom with PID 7672 (E:\javacode\springworkspace\zktest\target\classes started by Administrator in E:\javacode\springworkspace\zktest)