[bs-53] Aid for users debugging auto-configuration failures

Add logging to Condition implementations.

[Fixes #48389533]
pull/1/merge
Dave Syer 12 years ago
parent 6fb8ae3d3b
commit ad3076784d

@ -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<EmbeddedDatabaseType, String> EMBEDDED_DATABASE_TYPE_CLASSES;
static {
EMBEDDED_DATABASE_TYPE_CLASSES = new LinkedHashMap<EmbeddedDatabaseType, String>();
@ -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;
}
}

@ -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<String> beanClassesFound = new ArrayList<String>();
List<String> beanNamesFound = new ArrayList<String>();
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<String> beanClassesFound, List<String> beanNamesFound) {

@ -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;
}
}

@ -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<String, Object> 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;
}
}

@ -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;
}
}

Loading…
Cancel
Save