Skip to content

Commit a6e4b64

Browse files
committed
PropertySourcesPropertyResolver provides logKeyFound template method
Issue: SPR-14370
1 parent f6334fc commit a6e4b64

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

spring-core/src/main/java/org/springframework/core/env/PropertySourcesPropertyResolver.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,7 @@ protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolv
8282
if (resolveNestedPlaceholders && value instanceof String) {
8383
value = resolveNestedPlaceholders((String) value);
8484
}
85-
if (logger.isDebugEnabled()) {
86-
logger.debug(String.format("Found key '%s' in [%s] with type [%s] and value '%s'",
87-
key, propertySource.getName(), value.getClass().getSimpleName(), value));
88-
}
85+
logKeyFound(key, propertySource, value);
8986
return this.conversionService.convert(value, targetValueType);
9087
}
9188
}
@@ -106,10 +103,7 @@ public <T> Class<T> getPropertyAsClass(String key, Class<T> targetValueType) {
106103
}
107104
Object value = propertySource.getProperty(key);
108105
if (value != null) {
109-
if (logger.isDebugEnabled()) {
110-
logger.debug(String.format(
111-
"Found key '%s' in [%s] with value '%s'", key, propertySource.getName(), value));
112-
}
106+
logKeyFound(key, propertySource, value);
113107
Class<?> clazz;
114108
if (value instanceof String) {
115109
try {
@@ -140,17 +134,36 @@ else if (value instanceof Class) {
140134
return null;
141135
}
142136

137+
/**
138+
* Log the given key as found in the given {@link PropertySource}, resulting in
139+
* the given value.
140+
* <p>The default implementation writes a debug log message, including the value.
141+
* Subclasses may override this to change the log level and/or the log message.
142+
* @param key the key found
143+
* @param propertySource the {@code PropertySource} that the key has been found in
144+
* @param value the corresponding value
145+
* @since 4.3.1
146+
*/
147+
protected void logKeyFound(String key, PropertySource<?> propertySource, Object value) {
148+
if (logger.isDebugEnabled()) {
149+
logger.debug(String.format("Found key '%s' in [%s] with type [%s] and value '%s'",
150+
key, propertySource.getName(), value.getClass().getSimpleName(), value));
151+
}
152+
}
153+
143154

144155
@SuppressWarnings("serial")
145156
@Deprecated
146157
private static class ClassConversionException extends ConversionException {
147158

148159
public ClassConversionException(Class<?> actual, Class<?> expected) {
149-
super(String.format("Actual type %s is not assignable to expected type %s", actual.getName(), expected.getName()));
160+
super(String.format("Actual type %s is not assignable to expected type %s",
161+
actual.getName(), expected.getName()));
150162
}
151163

152164
public ClassConversionException(String actual, Class<?> expected, Exception ex) {
153-
super(String.format("Could not find/load class %s during attempt to convert to %s", actual, expected.getName()), ex);
165+
super(String.format("Could not find/load class %s during attempt to convert to %s",
166+
actual, expected.getName()), ex);
154167
}
155168
}
156169

0 commit comments

Comments
 (0)