Browse Source

Consistently use PropertySource.getName() for comparison

Includes synchronization for mutators on MutablePropertySources.

Closes gh-25369

(cherry picked from commit 01bab89dba)
pull/23967/head
Juergen Hoeller 5 years ago
parent
commit
a53d28edf1
  1. 58
      spring-core/src/main/java/org/springframework/core/env/MutablePropertySources.java
  2. 8
      spring-core/src/main/java/org/springframework/core/env/PropertySource.java
  3. 6
      spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java

58
spring-core/src/main/java/org/springframework/core/env/MutablePropertySources.java vendored

@ -1,5 +1,5 @@ @@ -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 class MutablePropertySources implements PropertySources { @@ -79,14 +79,23 @@ public class MutablePropertySources implements PropertySources {
@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 class MutablePropertySources implements PropertySources { @@ -94,16 +103,20 @@ public class MutablePropertySources implements PropertySources {
* 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 class MutablePropertySources implements PropertySources { @@ -112,9 +125,11 @@ public class MutablePropertySources implements PropertySources {
*/
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 class MutablePropertySources implements PropertySources { @@ -123,9 +138,11 @@ public class MutablePropertySources implements PropertySources {
*/
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 class MutablePropertySources implements PropertySources { @@ -141,8 +158,10 @@ public class MutablePropertySources implements PropertySources {
*/
@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 class MutablePropertySources implements PropertySources { @@ -153,8 +172,10 @@ public class MutablePropertySources implements PropertySources {
* @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 class MutablePropertySources implements PropertySources { @@ -169,6 +190,7 @@ public class MutablePropertySources implements PropertySources {
return this.propertySourceList.toString();
}
/**
* Ensure that the given property source is not being added relative to itself.
*/

8
spring-core/src/main/java/org/springframework/core/env/PropertySource.java vendored

@ -136,7 +136,7 @@ public abstract class PropertySource<T> { @@ -136,7 +136,7 @@ public abstract class PropertySource<T> {
@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 abstract class PropertySource<T> { @@ -145,7 +145,7 @@ public abstract class PropertySource<T> {
*/
@Override
public int hashCode() {
return ObjectUtils.nullSafeHashCode(this.name);
return ObjectUtils.nullSafeHashCode(getName());
}
/**
@ -161,10 +161,10 @@ public abstract class PropertySource<T> { @@ -161,10 +161,10 @@ public abstract class PropertySource<T> {
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() + "'}";
}
}

6
spring-web/src/main/java/org/springframework/web/context/support/WebApplicationContextUtils.java

@ -1,5 +1,5 @@ @@ -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 abstract class WebApplicationContextUtils { @@ -296,11 +296,11 @@ public abstract class WebApplicationContextUtils {
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));
}
}

Loading…
Cancel
Save