From ad3076784de912a28b1e3d56a1590a24c01c80fa Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 13 May 2013 14:27:43 +0100 Subject: [PATCH] [bs-53] Aid for users debugging auto-configuration failures Add logging to Condition implementations. [Fixes #48389533] --- .../jdbc/EmbeddedDatabaseAutoConfiguration.java | 12 ++++++++++++ .../annotation/AbstractOnBeanCondition.java | 16 +++++++++++++++- .../context/annotation/ExpressionCondition.java | 6 +++++- .../context/annotation/OnClassCondition.java | 11 +++++++++++ .../context/annotation/OnResourceCondition.java | 11 +++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/jdbc/EmbeddedDatabaseAutoConfiguration.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/jdbc/EmbeddedDatabaseAutoConfiguration.java index 7addf3f01e..d2788a0d49 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/jdbc/EmbeddedDatabaseAutoConfiguration.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/jdbc/EmbeddedDatabaseAutoConfiguration.java @@ -22,6 +22,8 @@ import java.util.Map; import javax.annotation.PostConstruct; import javax.sql.DataSource; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.bootstrap.context.annotation.ConditionalOnMissingBean; import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration; @@ -48,6 +50,9 @@ import org.springframework.util.ClassUtils; @ConditionalOnMissingBean(DataSource.class) public class EmbeddedDatabaseAutoConfiguration { + private static Log logger = LogFactory + .getLog(EmbeddedDatabaseAutoConfiguration.class); + private static final Map EMBEDDED_DATABASE_TYPE_CLASSES; static { EMBEDDED_DATABASE_TYPE_CLASSES = new LinkedHashMap(); @@ -100,8 +105,15 @@ public class EmbeddedDatabaseAutoConfiguration { if (!ClassUtils.isPresent( "org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType", context.getClassLoader())) { + if (logger.isDebugEnabled()) { + logger.debug("Spring JDBC not detected (EmbeddedDatabaseCondition evaluated false)."); + } return false; } + if (logger.isDebugEnabled()) { + logger.debug("Spring JDBC detected (embedded database type is " + + getEmbeddedDatabaseType() + ")."); + } return getEmbeddedDatabaseType() != null; } } diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/AbstractOnBeanCondition.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/AbstractOnBeanCondition.java index b84b0bbf21..2a61f39f78 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/AbstractOnBeanCondition.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/AbstractOnBeanCondition.java @@ -19,6 +19,8 @@ package org.springframework.bootstrap.context.annotation; import java.util.ArrayList; import java.util.List; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; @@ -34,6 +36,8 @@ import org.springframework.util.MultiValueMap; */ abstract class AbstractOnBeanCondition implements Condition { + private static Log logger = LogFactory.getLog(OnBeanCondition.class); + protected abstract Class annotationClass(); @Override @@ -49,6 +53,9 @@ abstract class AbstractOnBeanCondition implements Condition { List beanClassesFound = new ArrayList(); List beanNamesFound = new ArrayList(); + if (logger.isDebugEnabled()) { + logger.debug("Looking for beans with class: " + beanClasses); + } for (String beanClass : beanClasses) { try { // eagerInit set to false to prevent early instantiation (some @@ -65,13 +72,20 @@ abstract class AbstractOnBeanCondition implements Condition { } } + if (logger.isDebugEnabled()) { + logger.debug("Looking for beans with names: " + beanNames); + } for (String beanName : beanNames) { if (context.getBeanFactory().containsBeanDefinition(beanName)) { beanNamesFound.add(beanName); } } - return evaluate(beanClassesFound, beanNamesFound); + boolean result = evaluate(beanClassesFound, beanNamesFound); + if (logger.isDebugEnabled()) { + logger.debug("Finished matching and result is matches" + result); + } + return result; } protected boolean evaluate(List beanClassesFound, List beanNamesFound) { diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ExpressionCondition.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ExpressionCondition.java index b1b00c20ba..4d53689ed8 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ExpressionCondition.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ExpressionCondition.java @@ -57,7 +57,11 @@ public class ExpressionCondition implements Condition { BeanExpressionResolver resolver = beanFactory.getBeanExpressionResolver(); BeanExpressionContext expressionContext = (beanFactory != null) ? new BeanExpressionContext( beanFactory, null) : null; - return (Boolean) resolver.evaluate(value, expressionContext); + Boolean result = (Boolean) resolver.evaluate(value, expressionContext); + if (logger.isDebugEnabled()) { + logger.debug("Finished matching and result is matches=" + result); + } + return result; } } \ No newline at end of file diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnClassCondition.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnClassCondition.java index 524a984e50..06d2bf4b23 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnClassCondition.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnClassCondition.java @@ -19,6 +19,8 @@ package org.springframework.bootstrap.context.annotation; import java.util.ArrayList; import java.util.List; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.type.AnnotatedTypeMetadata; @@ -34,6 +36,8 @@ import org.springframework.util.MultiValueMap; */ class OnClassCondition implements Condition { + private static Log logger = LogFactory.getLog(OnClassCondition.class); + @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { MultiValueMap attributes = metadata.getAllAnnotationAttributes( @@ -45,7 +49,14 @@ class OnClassCondition implements Condition { Assert.isTrue(classNames.size() > 0, "@ConditionalOnClass annotations must specify at least one class value"); for (String className : classNames) { + if (logger.isDebugEnabled()) { + logger.debug("Checking for class: " + className); + } if (!ClassUtils.isPresent(className, context.getClassLoader())) { + if (logger.isDebugEnabled()) { + logger.debug("Found class: " + className + + " (search terminated with matches=false)"); + } return false; } } diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnResourceCondition.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnResourceCondition.java index 1f4b675070..3adb521961 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnResourceCondition.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnResourceCondition.java @@ -19,6 +19,8 @@ package org.springframework.bootstrap.context.annotation; import java.util.ArrayList; import java.util.List; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.springframework.context.annotation.Condition; import org.springframework.context.annotation.ConditionContext; import org.springframework.core.io.DefaultResourceLoader; @@ -35,6 +37,8 @@ import org.springframework.util.MultiValueMap; */ class OnResourceCondition implements Condition { + private static Log logger = LogFactory.getLog(OnResourceCondition.class); + private ResourceLoader loader = new DefaultResourceLoader(); @Override @@ -47,7 +51,14 @@ class OnResourceCondition implements Condition { Assert.isTrue(locations.size() > 0, "@ConditionalOnResource annotations must specify at least one resource location"); for (String location : locations) { + if (logger.isDebugEnabled()) { + logger.debug("Checking for resource: " + location); + } if (!this.loader.getResource(location).exists()) { + if (logger.isDebugEnabled()) { + logger.debug("Found resource: " + location + + " (search terminated with matches=false)"); + } return false; } }