Support multi-profile YAML in application-* files
Update ConfigFileEnvironmentPostProcessor to load profile specific sections for all previously processed profiles. Prior to this commit multi-profile YAML files were only loaded from the root `application.yml` file. With the updated logic, an `application-test.yml` file containing the following: someTestProperty: xyz --- spring: profiles: profile1 specificProperty: one Can have the profile sub-document loaded using: -Dspring.profiles.active=test,profile1 Fixes gh-4132
This commit is contained in:
		
							parent
							
								
									a899058ac3
								
							
						
					
					
						commit
						c6bf13c0a1
					
				| 
						 | 
				
			
			@ -277,6 +277,8 @@ public class ConfigFileEnvironmentPostProcessor implements EnvironmentPostProces
 | 
			
		|||
 | 
			
		||||
		private Queue<String> profiles;
 | 
			
		||||
 | 
			
		||||
		private List<String> processedProfiles;
 | 
			
		||||
 | 
			
		||||
		private boolean activatedProfiles;
 | 
			
		||||
 | 
			
		||||
		Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
 | 
			
		||||
| 
						 | 
				
			
			@ -288,6 +290,7 @@ public class ConfigFileEnvironmentPostProcessor implements EnvironmentPostProces
 | 
			
		|||
		public void load() throws IOException {
 | 
			
		||||
			this.propertiesLoader = new PropertySourcesLoader();
 | 
			
		||||
			this.profiles = Collections.asLifoQueue(new LinkedList<String>());
 | 
			
		||||
			this.processedProfiles = new LinkedList<String>();
 | 
			
		||||
			this.activatedProfiles = false;
 | 
			
		||||
			if (this.environment.containsProperty(ACTIVE_PROFILES_PROPERTY)) {
 | 
			
		||||
				// Any pre-existing active profiles set via property sources (e.g. System
 | 
			
		||||
| 
						 | 
				
			
			@ -334,6 +337,7 @@ public class ConfigFileEnvironmentPostProcessor implements EnvironmentPostProces
 | 
			
		|||
						}
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				this.processedProfiles.add(profile);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			addConfigurationProperties(this.propertiesLoader.getPropertySources());
 | 
			
		||||
| 
						 | 
				
			
			@ -353,6 +357,12 @@ public class ConfigFileEnvironmentPostProcessor implements EnvironmentPostProces
 | 
			
		|||
						// Try the profile specific file
 | 
			
		||||
						loadIntoGroup(group, location + name + "-" + profile + "." + ext,
 | 
			
		||||
								null);
 | 
			
		||||
						for (String 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.
 | 
			
		||||
| 
						 | 
				
			
			@ -412,7 +422,6 @@ public class ConfigFileEnvironmentPostProcessor implements EnvironmentPostProces
 | 
			
		|||
				}
 | 
			
		||||
				return;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			Set<String> profiles = getProfilesForValue(value);
 | 
			
		||||
			activateProfiles(profiles);
 | 
			
		||||
			if (profiles.size() > 0) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -609,7 +609,7 @@ public class ConfigFileEnvironmentPostProcessorTests {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void profileSubDocumentInProfileSpecificFile() throws Exception {
 | 
			
		||||
	public void profileSubDocumentInSameProfileSpecificFile() throws Exception {
 | 
			
		||||
		// gh-340
 | 
			
		||||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
| 
						 | 
				
			
			@ -639,6 +639,17 @@ public class ConfigFileEnvironmentPostProcessorTests {
 | 
			
		|||
		assertThat((Banner.Mode) field.get(this.application), equalTo(Banner.Mode.OFF));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@Test
 | 
			
		||||
	public void profileSubDocumentInDifferentProfileSpecificFile() throws Exception {
 | 
			
		||||
		// gh-4132
 | 
			
		||||
		SpringApplication application = new SpringApplication(Config.class);
 | 
			
		||||
		application.setWebEnvironment(false);
 | 
			
		||||
		this.context = application.run(
 | 
			
		||||
				"--spring.profiles.active=activeprofilewithdifferentsubdoc,activeprofilewithdifferentsubdoc2");
 | 
			
		||||
		String property = this.context.getEnvironment().getProperty("foobar");
 | 
			
		||||
		assertThat(property, equalTo("baz"));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private static Matcher<? super ConfigurableEnvironment> containsPropertySource(
 | 
			
		||||
			final String sourceName) {
 | 
			
		||||
		return new TypeSafeDiagnosingMatcher<ConfigurableEnvironment>() {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,4 @@
 | 
			
		|||
---
 | 
			
		||||
spring.profiles: activeprofilewithdifferentsubdoc2
 | 
			
		||||
foobar: baz
 | 
			
		||||
---
 | 
			
		||||
		Loading…
	
		Reference in New Issue