privatevoidload(String location, String name, Profile profile) throws IOException { Stringgroup="profile=" + (profile == null ? "" : profile); if (!StringUtils.hasText(name)) { // Try to load directly from the location loadIntoGroup(group, location, profile); } else { // Search for a file with the given name for (String ext : this.propertiesLoader.getAllFileExtensions()) { ① if (profile != null) { // Try the profile-specific file loadIntoGroup(group, location + name + "-" + profile + "." + ext,null); for (Profile processedProfile : this.processedProfiles) { if (processedProfile != null) { loadIntoGroup(group, location + name + "-" + processedProfile + "." + ext, profile); ② } } // Sometimes people put "spring.profiles: dev" in // application-dev.yml (gh-340). Arguably we should try and error // out on that, but we can be kind and load it anyway. loadIntoGroup(group, location + name + "-" + profile + "." + ext, profile); } // Also try the profile-specific section (if any) of the normal file loadIntoGroup(group, location + name + "." + ext, profile); } } }
我们可以从 ① 发现Spring所支持的后缀名:“properties”,“xml”,“yml”,“yaml” ,而在②处我们发现就是按照 - 逻辑给拼接起来的。
@RequestMapping("/{name}/{profiles}/{label:.*}") public Environment labelled(@PathVariable String name, @PathVariable String profiles, @PathVariable String label) { if (label != null && label.contains("(_)")) { // "(_)" is uncommon in a git branch name, but "/" cannot be matched // by Spring MVC label = label.replace("(_)", "/"); } Environmentenvironment=this.repository.findOne(name, profiles, label); ① return environment; }
public org.springframework.core.env.PropertySource<?> locate( org.springframework.core.env.Environment environment) { ConfigClientPropertiesproperties=this.defaultProperties.override(environment); CompositePropertySourcecomposite=newCompositePropertySource("configService"); RestTemplaterestTemplate=this.restTemplate == null ? getSecureRestTemplate(properties): this.restTemplate; Exceptionerror=null; StringerrorBody=null; logger.info("Fetching config from server at: " + properties.getRawUri()); try { String[] labels = newString[] { "" }; if (StringUtils.hasText(properties.getLabel())) { labels = StringUtils.commaDelimitedListToStringArray(properties.getLabel()); }
Stringstate= ConfigClientStateHolder.getState();
// Try all the labels until one works for (String label : labels) { Environmentresult= getRemoteEnvironment(restTemplate,properties, label.trim(), state); ① if (result != null) { logger.info(String.format("Located environment: name=%s, profiles=%s, label=%s, version=%s, state=%s", result.getName(), result.getProfiles() == null ? "" : Arrays.asList(result.getProfiles()), result.getLabel(), result.getVersion(), result.getState())); if (result.getPropertySources() != null) { // result.getPropertySources() can be null if using xml for (PropertySource source : result.getPropertySources()) { @SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) source .getSource(); composite.addPropertySource(newMapPropertySource(source .getName(), map)); } }
从 ① 处我们看出来这最后是一个Http的请求。 从 ② 处,我们直接看出最终访问的 HTTP的地址就是 “/{name}/{profile}”, 从 ③ 处我们又发现最后得到的就是 Environment.class 这个类型,和我们在Server上看见的代码是一致,这样我们的整个逻辑就串联起来了。