1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package org.springframework.cloud.bootstrap;
import org.springframework.boot.BootstrapContext; import org.springframework.boot.BootstrapRegistry; import org.springframework.boot.BootstrapRegistryInitializer; import org.springframework.cloud.context.refresh.ConfigDataContextRefresher;
public class RefreshBootstrapRegistryInitializer implements BootstrapRegistryInitializer {
@Override public void initialize(BootstrapRegistry registry) { 加一个监听 ApplicationListener 当 BootstrapContextClosedEvent 时,将 bootstrapContext bean 注册到 applicationContext 中 即 bootstrapContext 结束,applicationContext Prepare 开始的时候 registry.addCloseListener(event -> { BootstrapContext bootstrapContext = event.getBootstrapContext(); event.getApplicationContext().getBeanFactory().registerSingleton("bootstrapContext", bootstrapContext); }); }
}
|