SpringBoot加载配置⽂件源码解析
易语言教程图文SpringBoot通过事件⼴播机制通知ConfigFileApplicationListener这个来加载properties和yaml⽂件。关于SpringBoot事件编程模型可参考:。
SpringBoot⼀站式启动过程中,会经过环境准备阶段:
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
该过程中,会向监听者⼴播环境准备完毕的事件:
其中ConfigFileApplicationListener监听者接收到该事件后, 会委托EnvionmentPostProcesor环境后置处理器来加载配置⽂件,这⾥的ConfigFileApplicationListener的另外⼀个⾓⾊正好是环境后置处理器,所以最终⽂件加载还是在ConfigFileApplicationListener 中完成的。
//继承了环境后置处理器和应⽤程序
even integer什么意思
public class ConfigFileApplicationListener
implements EnvironmentPostProcessor, SmartApplicationListener, Ordered {
...
}
properties是什么文件ConfigFileApplicationListener中加载配置⽂件总领代码如下:
protected void addPropertySources(ConfigurableEnvironment environment,
ResourceLoader resourceLoader) {
RandomValuePropertySource.addToEnvironment(environment);
new Loader(environment, resourceLoader).load();
}
加载规则如下:先看命令⾏中是否指定fig.location,如果有,则加载指定地址配置⽂件,如果没有,则按
classpath:/,classpath:/config/,file:./,file:./config/优先级加载该路径下的配置⽂件,可配置spring.profiles.active或者
spring.profiles.include加载application-xxx.properties⽂件,当遇到相同属性时,application-xxx.properties会覆盖
application.properties中的该属性,也可配置fig.additional-location加载其他路径下的配置⽂件。
property和yaml具体的资源加载器分别是:PropertiesPropertySourceLoader、YamlPropertySourceLoader,将配置⽂件读取到内存其实就是JAVA IO的过程,以classpath路径下配置⽂件加载为例:
public class PropertiesPropertySourceLoader implements PropertySourceLoader {
ssh免密码登录配置private static final String XML_FILE_EXTENSION = ".xml";
@Override
public String[] getFileExtensions() {
return new String[] { "properties", "xml" };
}
queue加ing要去e吗@Override
public List<PropertySource<?>> load(String name, Resource resource)
throws IOException {
Map<String, ?> properties = loadProperties(resource);
if (properties.isEmpty()) {
ptyList();html友情链接代码
}
return Collections
.singletonList(new OriginTrackedMapPropertySource(name, properties));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private Map<String, ?> loadProperties(Resource resource) throws IOException {
String filename = Filename();
if (filename != null && dsWith(XML_FILE_EXTENSION)) {
return (Map) PropertiesLoaderUtils.loadProperties(resource);
}
//加载
return new OriginTrackedPropertiesLoader(resource).load();
}
}
load过程如下:
public Map<String, OriginTrackedValue> load(boolean expandLists) throws IOException { //通过ClasspathResource中获取字符流
try (CharacterReader reader = new source)) {
Map<String, OriginTrackedValue> result = new LinkedHashMap<>();
StringBuilder buffer = new StringBuilder();
while (ad()) {
String key = loadKey(buffer, reader).trim();
if (expandLists && dsWith("[]")) {
key = key.substring(0, key.length() - 2);
int index = 0;
do {
OriginTrackedValue value = loadValue(buffer, reader, true);
put(result, key + "[" + (index++) + "]", value);
if (!reader.isEndOfLine()) {
}
}
while (!reader.isEndOfLine());
}
else {
OriginTrackedValue value = loadValue(buffer, reader, false);
put(result, key, value);
}
}
return result;
}
}
从⽂件到输⼊流:
@Override
public InputStream getInputStream() throws IOException {
InputStream is;
if (this.clazz != null) {
is = ResourceAsStream(this.path);
}
else if (this.classLoader != null) {
is = ResourceAsStream(this.path);
}
else {
is = SystemResourceAsStream(this.path);
}
if (is == null) {
throw new FileNotFoundException(getDescription() + " cannot be opened because it does not exist"); }
return is;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论