Skip to content

Commit d6216fb

Browse files
committed
Consistent logging in ignoreResourceNotFound scenarios
Issue: SPR-15218 (cherry picked from commit 448ea4c)
1 parent 941f3e7 commit d6216fb

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,24 @@ private void processPropertySource(AnnotationAttributes propertySource) throws I
419419
addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
420420
}
421421
catch (IllegalArgumentException ex) {
422-
// from resolveRequiredPlaceholders
423-
if (!ignoreResourceNotFound) {
422+
// Placeholders not resolvable
423+
if (ignoreResourceNotFound) {
424+
if (logger.isInfoEnabled()) {
425+
logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage());
426+
}
427+
}
428+
else {
424429
throw ex;
425430
}
426431
}
427432
catch (FileNotFoundException ex) {
428-
// from ResourcePropertySource constructor
429-
if (!ignoreResourceNotFound) {
433+
// Resource not found when trying to open it
434+
if (ignoreResourceNotFound) {
435+
if (logger.isInfoEnabled()) {
436+
logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage());
437+
}
438+
}
439+
else {
430440
throw ex;
431441
}
432442
}

spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.core.io.support;
1818

19+
import java.io.FileNotFoundException;
1920
import java.io.IOException;
2021
import java.util.Properties;
2122

@@ -168,17 +169,17 @@ protected Properties mergeProperties() throws IOException {
168169
protected void loadProperties(Properties props) throws IOException {
169170
if (this.locations != null) {
170171
for (Resource location : this.locations) {
171-
if (logger.isInfoEnabled()) {
172-
logger.info("Loading properties file from " + location);
172+
if (logger.isDebugEnabled()) {
173+
logger.debug("Loading properties file from " + location);
173174
}
174175
try {
175176
PropertiesLoaderUtils.fillProperties(
176177
props, new EncodedResource(location, this.fileEncoding), this.propertiesPersister);
177178
}
178-
catch (IOException ex) {
179+
catch (FileNotFoundException ex) {
179180
if (this.ignoreResourceNotFound) {
180-
if (logger.isWarnEnabled()) {
181-
logger.warn("Could not load properties from " + location + ": " + ex.getMessage());
181+
if (logger.isInfoEnabled()) {
182+
logger.info("Properties resource not found: " + ex.getMessage());
182183
}
183184
}
184185
else {

spring-core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -127,11 +127,11 @@ public String replacePlaceholders(String value, PlaceholderResolver placeholderR
127127
}
128128

129129
protected String parseStringValue(
130-
String strVal, PlaceholderResolver placeholderResolver, Set<String> visitedPlaceholders) {
130+
String value, PlaceholderResolver placeholderResolver, Set<String> visitedPlaceholders) {
131131

132-
StringBuilder result = new StringBuilder(strVal);
132+
StringBuilder result = new StringBuilder(value);
133133

134-
int startIndex = strVal.indexOf(this.placeholderPrefix);
134+
int startIndex = value.indexOf(this.placeholderPrefix);
135135
while (startIndex != -1) {
136136
int endIndex = findPlaceholderEndIndex(result, startIndex);
137137
if (endIndex != -1) {
@@ -172,7 +172,7 @@ else if (this.ignoreUnresolvablePlaceholders) {
172172
}
173173
else {
174174
throw new IllegalArgumentException("Could not resolve placeholder '" +
175-
placeholder + "'" + " in string value \"" + strVal + "\"");
175+
placeholder + "'" + " in value \"" + value + "\"");
176176
}
177177
visitedPlaceholders.remove(originalPlaceholder);
178178
}
@@ -212,7 +212,7 @@ else if (StringUtils.substringMatch(buf, index, this.simplePrefix)) {
212212
/**
213213
* Strategy interface used to resolve replacement values for placeholders contained in Strings.
214214
*/
215-
public static interface PlaceholderResolver {
215+
public interface PlaceholderResolver {
216216

217217
/**
218218
* Resolve the supplied placeholder name to the replacement value.

spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -388,7 +388,7 @@ public void resolveNestedPropertyPlaceholders() {
388388
}
389389
catch (IllegalArgumentException ex) {
390390
assertThat(ex.getMessage(), containsString(
391-
"Could not resolve placeholder 'bogus' in string value \"${p1}:${p2}:${bogus}\""));
391+
"Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\""));
392392
}
393393
assertThat(pr.getProperty("p6"), equalTo("v1:v2:def"));
394394
try {
@@ -420,7 +420,7 @@ public void ignoreUnresolvableNestedPlaceholdersIsConfigurable() {
420420
}
421421
catch (IllegalArgumentException ex) {
422422
assertThat(ex.getMessage(), containsString(
423-
"Could not resolve placeholder 'bogus' in string value \"${p1}:${p2}:${bogus}\""));
423+
"Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\""));
424424
}
425425

426426
// relax the treatment of unresolvable nested placeholders
@@ -436,7 +436,7 @@ public void ignoreUnresolvableNestedPlaceholdersIsConfigurable() {
436436
}
437437
catch (IllegalArgumentException ex) {
438438
assertThat(ex.getMessage(), containsString(
439-
"Could not resolve placeholder 'bogus' in string value \"${p1}:${p2}:${bogus}\""));
439+
"Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\""));
440440
}
441441
}
442442

0 commit comments

Comments
 (0)