From d6216fb74a1310ef55ac7fd35a6dc0666639b6a3 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 10 Feb 2017 10:41:10 +0100 Subject: [PATCH] Consistent logging in ignoreResourceNotFound scenarios Issue: SPR-15218 (cherry picked from commit 448ea4c) --- .../annotation/ConfigurationClassParser.java | 18 ++++++++++++++---- .../io/support/PropertiesLoaderSupport.java | 13 +++++++------ .../util/PropertyPlaceholderHelper.java | 12 ++++++------ .../PropertySourcesPropertyResolverTests.java | 8 ++++---- 4 files changed, 31 insertions(+), 20 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java index 0c67725f8c..0dfcee3a79 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassParser.java @@ -419,14 +419,24 @@ class ConfigurationClassParser { addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding))); } catch (IllegalArgumentException ex) { - // from resolveRequiredPlaceholders - if (!ignoreResourceNotFound) { + // Placeholders not resolvable + if (ignoreResourceNotFound) { + if (logger.isInfoEnabled()) { + logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage()); + } + } + else { throw ex; } } catch (FileNotFoundException ex) { - // from ResourcePropertySource constructor - if (!ignoreResourceNotFound) { + // Resource not found when trying to open it + if (ignoreResourceNotFound) { + if (logger.isInfoEnabled()) { + logger.info("Properties location [" + location + "] not resolvable: " + ex.getMessage()); + } + } + else { throw ex; } } diff --git a/spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java b/spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java index fb9508390e..88edb0ec8a 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/PropertiesLoaderSupport.java +++ b/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"); * you may not use this file except in compliance with the License. @@ -16,6 +16,7 @@ package org.springframework.core.io.support; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; @@ -168,17 +169,17 @@ public abstract class PropertiesLoaderSupport { protected void loadProperties(Properties props) throws IOException { if (this.locations != null) { for (Resource location : this.locations) { - if (logger.isInfoEnabled()) { - logger.info("Loading properties file from " + location); + if (logger.isDebugEnabled()) { + logger.debug("Loading properties file from " + location); } try { PropertiesLoaderUtils.fillProperties( props, new EncodedResource(location, this.fileEncoding), this.propertiesPersister); } - catch (IOException ex) { + catch (FileNotFoundException ex) { if (this.ignoreResourceNotFound) { - if (logger.isWarnEnabled()) { - logger.warn("Could not load properties from " + location + ": " + ex.getMessage()); + if (logger.isInfoEnabled()) { + logger.info("Properties resource not found: " + ex.getMessage()); } } else { diff --git a/spring-core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.java b/spring-core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.java index b5c749fbba..99a09696d5 100644 --- a/spring-core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.java +++ b/spring-core/src/main/java/org/springframework/util/PropertyPlaceholderHelper.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"); * you may not use this file except in compliance with the License. @@ -127,11 +127,11 @@ public class PropertyPlaceholderHelper { } protected String parseStringValue( - String strVal, PlaceholderResolver placeholderResolver, Set visitedPlaceholders) { + String value, PlaceholderResolver placeholderResolver, Set 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) { int endIndex = findPlaceholderEndIndex(result, startIndex); if (endIndex != -1) { @@ -172,7 +172,7 @@ public class PropertyPlaceholderHelper { } else { throw new IllegalArgumentException("Could not resolve placeholder '" + - placeholder + "'" + " in string value \"" + strVal + "\""); + placeholder + "'" + " in value \"" + value + "\""); } visitedPlaceholders.remove(originalPlaceholder); } @@ -212,7 +212,7 @@ public class PropertyPlaceholderHelper { /** * Strategy interface used to resolve replacement values for placeholders contained in Strings. */ - public static interface PlaceholderResolver { + public interface PlaceholderResolver { /** * Resolve the supplied placeholder name to the replacement value. diff --git a/spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java b/spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java index 529bcd4101..aba005f172 100644 --- a/spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.java +++ b/spring-core/src/test/java/org/springframework/core/env/PropertySourcesPropertyResolverTests.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"); * you may not use this file except in compliance with the License. @@ -388,7 +388,7 @@ public class PropertySourcesPropertyResolverTests { } catch (IllegalArgumentException ex) { 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")); try { @@ -420,7 +420,7 @@ public class PropertySourcesPropertyResolverTests { } catch (IllegalArgumentException ex) { 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 @@ -436,7 +436,7 @@ public class PropertySourcesPropertyResolverTests { } catch (IllegalArgumentException ex) { 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}\"")); } }