diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java index 0d1fb29713e..bd4c2a6bb98 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java @@ -305,8 +305,7 @@ class ConfigurationClassParser { String name = propertySource.getString("name"); String[] locations = propertySource.getStringArray("value"); boolean ignoreResourceNotFound = propertySource.getBoolean("ignoreResourceNotFound"); - int locationCount = locations.length; - if (locationCount == 0) { + if (locations.length == 0) { throw new IllegalArgumentException("At least one @PropertySource(value) location is required"); } for (String location : locations) { @@ -314,8 +313,7 @@ class ConfigurationClassParser { Resource resource = this.resourceLoader.getResource( this.environment.resolveRequiredPlaceholders(location)); if (!StringUtils.hasText(name) || this.propertySources.containsKey(name)) { - // We need to ensure unique names when the property source will - // ultimately end up in a composite + // We need to ensure unique names when the property source will ultimately end up in a composite ResourcePropertySource ps = new ResourcePropertySource(resource); this.propertySources.add((StringUtils.hasText(name) ? name : ps.getName()), ps); } diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java index a941d7e3057..fc19634732e 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -307,7 +307,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo "Reason: Environment must implement ConfigurableEnvironment"); } else { - MutablePropertySources envPropertySources = ((ConfigurableEnvironment)this.environment).getPropertySources(); + MutablePropertySources envPropertySources = ((ConfigurableEnvironment) this.environment).getPropertySources(); for (PropertySource propertySource : parsedPropertySources) { envPropertySources.addLast(propertySource); } diff --git a/spring-context/src/test/java/org/springframework/context/annotation/PropertySourceAnnotationTests.java b/spring-context/src/test/java/org/springframework/context/annotation/PropertySourceAnnotationTests.java index 0776c801537..fa880e46c5c 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/PropertySourceAnnotationTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/PropertySourceAnnotationTests.java @@ -124,18 +124,6 @@ public class PropertySourceAnnotationTests { System.clearProperty("path.to.properties"); } - /** - * Corner bug reported in SPR-9127. - */ - @Test - public void withNameAndMultipleResourceLocations() { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - ctx.register(ConfigWithNameAndMultipleResourceLocations.class); - ctx.refresh(); - assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true)); - assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true)); - } - /** * SPR-10820 */ @@ -155,6 +143,24 @@ public class PropertySourceAnnotationTests { ctx.refresh(); } + @Test + public void withNameAndMultipleResourceLocations() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNameAndMultipleResourceLocations.class); + assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true)); + assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true)); + // p2 should 'win' as it was registered last + assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean")); + } + + @Test + public void withMultipleResourceLocations() { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithMultipleResourceLocations.class); + assertThat(ctx.getEnvironment().containsProperty("from.p1"), is(true)); + assertThat(ctx.getEnvironment().containsProperty("from.p2"), is(true)); + // p2 should 'win' as it was registered last + assertThat(ctx.getEnvironment().getProperty("testbean.name"), equalTo("p2TestBean")); + } + @Test public void withPropertySources() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithPropertySources.class); diff --git a/spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java b/spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java index 229c9cd2289..f05d0ce4cdc 100644 --- a/spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java +++ b/spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,12 +29,11 @@ import java.util.Set; */ public class CompositePropertySource extends PropertySource { - private Set> propertySources = new LinkedHashSet>(); + private final Set> propertySources = new LinkedHashSet>(); /** * Create a new {@code CompositePropertySource}. - * * @param name the name of the property source */ public CompositePropertySource(String name) { @@ -60,6 +59,7 @@ public class CompositePropertySource extends PropertySource { @Override public String toString() { return String.format("%s [name='%s', propertySources=%s]", - this.getClass().getSimpleName(), this.name, this.propertySources); + getClass().getSimpleName(), this.name, this.propertySources); } + }