diff --git a/spring-core/src/main/java/org/springframework/core/env/MutablePropertySources.java b/spring-core/src/main/java/org/springframework/core/env/MutablePropertySources.java index 1fa383b38d93..1bc07bc7b542 100644 --- a/spring-core/src/main/java/org/springframework/core/env/MutablePropertySources.java +++ b/spring-core/src/main/java/org/springframework/core/env/MutablePropertySources.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -79,14 +79,23 @@ public Stream> stream() { @Override public boolean contains(String name) { - return this.propertySourceList.contains(PropertySource.named(name)); + for (PropertySource propertySource : this.propertySourceList) { + if (propertySource.getName().equals(name)) { + return true; + } + } + return false; } @Override @Nullable public PropertySource get(String name) { - int index = this.propertySourceList.indexOf(PropertySource.named(name)); - return (index != -1 ? this.propertySourceList.get(index) : null); + for (PropertySource propertySource : this.propertySourceList) { + if (propertySource.getName().equals(name)) { + return propertySource; + } + } + return null; } @@ -94,16 +103,20 @@ public PropertySource get(String name) { * Add the given property source object with highest precedence. */ public void addFirst(PropertySource propertySource) { - removeIfPresent(propertySource); - this.propertySourceList.add(0, propertySource); + synchronized (this.propertySourceList) { + removeIfPresent(propertySource); + this.propertySourceList.add(0, propertySource); + } } /** * Add the given property source object with lowest precedence. */ public void addLast(PropertySource propertySource) { - removeIfPresent(propertySource); - this.propertySourceList.add(propertySource); + synchronized (this.propertySourceList) { + removeIfPresent(propertySource); + this.propertySourceList.add(propertySource); + } } /** @@ -112,9 +125,11 @@ public void addLast(PropertySource propertySource) { */ public void addBefore(String relativePropertySourceName, PropertySource propertySource) { assertLegalRelativeAddition(relativePropertySourceName, propertySource); - removeIfPresent(propertySource); - int index = assertPresentAndGetIndex(relativePropertySourceName); - addAtIndex(index, propertySource); + synchronized (this.propertySourceList) { + removeIfPresent(propertySource); + int index = assertPresentAndGetIndex(relativePropertySourceName); + addAtIndex(index, propertySource); + } } /** @@ -123,9 +138,11 @@ public void addBefore(String relativePropertySourceName, PropertySource prope */ public void addAfter(String relativePropertySourceName, PropertySource propertySource) { assertLegalRelativeAddition(relativePropertySourceName, propertySource); - removeIfPresent(propertySource); - int index = assertPresentAndGetIndex(relativePropertySourceName); - addAtIndex(index + 1, propertySource); + synchronized (this.propertySourceList) { + removeIfPresent(propertySource); + int index = assertPresentAndGetIndex(relativePropertySourceName); + addAtIndex(index + 1, propertySource); + } } /** @@ -141,8 +158,10 @@ public int precedenceOf(PropertySource propertySource) { */ @Nullable public PropertySource remove(String name) { - int index = this.propertySourceList.indexOf(PropertySource.named(name)); - return (index != -1 ? this.propertySourceList.remove(index) : null); + synchronized (this.propertySourceList) { + int index = this.propertySourceList.indexOf(PropertySource.named(name)); + return (index != -1 ? this.propertySourceList.remove(index) : null); + } } /** @@ -153,8 +172,10 @@ public PropertySource remove(String name) { * @see #contains */ public void replace(String name, PropertySource propertySource) { - int index = assertPresentAndGetIndex(name); - this.propertySourceList.set(index, propertySource); + synchronized (this.propertySourceList) { + int index = assertPresentAndGetIndex(name); + this.propertySourceList.set(index, propertySource); + } } /** @@ -169,6 +190,7 @@ public String toString() { return this.propertySourceList.toString(); } + /** * Ensure that the given property source is not being added relative to itself. */ diff --git a/spring-core/src/main/java/org/springframework/core/env/PropertySource.java b/spring-core/src/main/java/org/springframework/core/env/PropertySource.java index 112fc1350bfe..68eda2f70bcc 100644 --- a/spring-core/src/main/java/org/springframework/core/env/PropertySource.java +++ b/spring-core/src/main/java/org/springframework/core/env/PropertySource.java @@ -136,7 +136,7 @@ public boolean containsProperty(String name) { @Override public boolean equals(Object other) { return (this == other || (other instanceof PropertySource && - ObjectUtils.nullSafeEquals(this.name, ((PropertySource) other).name))); + ObjectUtils.nullSafeEquals(getName(), ((PropertySource) other).getName()))); } /** @@ -145,7 +145,7 @@ public boolean equals(Object other) { */ @Override public int hashCode() { - return ObjectUtils.nullSafeHashCode(this.name); + return ObjectUtils.nullSafeHashCode(getName()); } /** @@ -161,10 +161,10 @@ public int hashCode() { public String toString() { if (logger.isDebugEnabled()) { return getClass().getSimpleName() + "@" + System.identityHashCode(this) + - " {name='" + this.name + "', properties=" + this.source + "}"; + " {name='" + getName() + "', properties=" + getSource() + "}"; } else { - return getClass().getSimpleName() + " {name='" + this.name + "'}"; + return getClass().getSimpleName() + " {name='" + getName() + "'}"; } } diff --git a/spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java b/spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java index 73da6c9e9028..d9bfe57b1523 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java +++ b/spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2020 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. @@ -296,11 +296,11 @@ public static void initServletPropertySources(MutablePropertySources sources, Assert.notNull(sources, "'propertySources' must not be null"); String name = StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME; - if (servletContext != null && sources.contains(name) && sources.get(name) instanceof StubPropertySource) { + if (servletContext != null && sources.get(name) instanceof StubPropertySource) { sources.replace(name, new ServletContextPropertySource(name, servletContext)); } name = StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME; - if (servletConfig != null && sources.contains(name) && sources.get(name) instanceof StubPropertySource) { + if (servletConfig != null && sources.get(name) instanceof StubPropertySource) { sources.replace(name, new ServletConfigPropertySource(name, servletConfig)); } }