Browse Source

Consistent logging in ignoreResourceNotFound scenarios

Issue: SPR-15218
pull/1323/head
Juergen Hoeller 9 years ago
parent
commit
448ea4cdfe
  1. 15
      spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java
  2. 13
      spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java
  3. 10
      spring-core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.java
  4. 15
      spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java

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

@ -417,15 +417,14 @@ class ConfigurationClassParser {
Resource resource = this.resourceLoader.getResource(resolvedLocation); Resource resource = this.resourceLoader.getResource(resolvedLocation);
addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding))); addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
} }
catch (IllegalArgumentException ex) { catch (IllegalArgumentException | FileNotFoundException ex) {
// from resolveRequiredPlaceholders // Placeholders not resolvable or resource not found when trying to open it
if (!ignoreResourceNotFound) { if (ignoreResourceNotFound) {
throw ex; if (logger.isInfoEnabled()) {
logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage());
}
} }
} else {
catch (FileNotFoundException ex) {
// from ResourcePropertySource constructor
if (!ignoreResourceNotFound) {
throw ex; throw ex;
} }
} }

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,6 +16,7 @@
package org.springframework.core.io.support; package org.springframework.core.io.support;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.Properties; import java.util.Properties;
@ -168,17 +169,17 @@ public abstract class PropertiesLoaderSupport {
protected void loadProperties(Properties props) throws IOException { protected void loadProperties(Properties props) throws IOException {
if (this.locations != null) { if (this.locations != null) {
for (Resource location : this.locations) { for (Resource location : this.locations) {
if (logger.isInfoEnabled()) { if (logger.isDebugEnabled()) {
logger.info("Loading properties file from " + location); logger.debug("Loading properties file from " + location);
} }
try { try {
PropertiesLoaderUtils.fillProperties( PropertiesLoaderUtils.fillProperties(
props, new EncodedResource(location, this.fileEncoding), this.propertiesPersister); props, new EncodedResource(location, this.fileEncoding), this.propertiesPersister);
} }
catch (IOException ex) { catch (FileNotFoundException ex) {
if (this.ignoreResourceNotFound) { if (this.ignoreResourceNotFound) {
if (logger.isWarnEnabled()) { if (logger.isInfoEnabled()) {
logger.warn("Could not load properties from " + location + ": " + ex.getMessage()); logger.info("Properties resource not found: " + ex.getMessage());
} }
} }
else { else {

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -127,11 +127,11 @@ public class PropertyPlaceholderHelper {
} }
protected String parseStringValue( protected String parseStringValue(
String strVal, PlaceholderResolver placeholderResolver, Set<String> visitedPlaceholders) { String value, PlaceholderResolver placeholderResolver, Set<String> visitedPlaceholders) {
StringBuilder result = new StringBuilder(strVal); StringBuilder result = new StringBuilder(value);
int startIndex = strVal.indexOf(this.placeholderPrefix); int startIndex = value.indexOf(this.placeholderPrefix);
while (startIndex != -1) { while (startIndex != -1) {
int endIndex = findPlaceholderEndIndex(result, startIndex); int endIndex = findPlaceholderEndIndex(result, startIndex);
if (endIndex != -1) { if (endIndex != -1) {
@ -172,7 +172,7 @@ public class PropertyPlaceholderHelper {
} }
else { else {
throw new IllegalArgumentException("Could not resolve placeholder '" + throw new IllegalArgumentException("Could not resolve placeholder '" +
placeholder + "'" + " in string value \"" + strVal + "\""); placeholder + "'" + " in value \"" + value + "\"");
} }
visitedPlaceholders.remove(originalPlaceholder); visitedPlaceholders.remove(originalPlaceholder);
} }

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -315,7 +315,7 @@ public class PropertySourcesPropertyResolverTests {
} }
catch (IllegalArgumentException ex) { catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(), containsString( assertThat(ex.getMessage(), containsString(
"Could not resolve placeholder 'bogus' in string value \"${p1}:${p2}:${bogus}\"")); "Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\""));
} }
assertThat(pr.getProperty("p6"), equalTo("v1:v2:def")); assertThat(pr.getProperty("p6"), equalTo("v1:v2:def"));
try { try {
@ -347,7 +347,7 @@ public class PropertySourcesPropertyResolverTests {
} }
catch (IllegalArgumentException ex) { catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(), containsString( assertThat(ex.getMessage(), containsString(
"Could not resolve placeholder 'bogus' in string value \"${p1}:${p2}:${bogus}\"")); "Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\""));
} }
// relax the treatment of unresolvable nested placeholders // relax the treatment of unresolvable nested placeholders
@ -363,15 +363,8 @@ public class PropertySourcesPropertyResolverTests {
} }
catch (IllegalArgumentException ex) { catch (IllegalArgumentException ex) {
assertThat(ex.getMessage(), containsString( assertThat(ex.getMessage(), containsString(
"Could not resolve placeholder 'bogus' in string value \"${p1}:${p2}:${bogus}\"")); "Could not resolve placeholder 'bogus' in value \"${p1}:${p2}:${bogus}\""));
} }
} }
interface SomeType {
}
static class SpecificType implements SomeType {
}
} }

Loading…
Cancel
Save