diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/jdbc/DataSourceAutoConfiguration.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/jdbc/DataSourceAutoConfiguration.java index a934cb228db..75404680bd9 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/jdbc/DataSourceAutoConfiguration.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/autoconfigure/jdbc/DataSourceAutoConfiguration.java @@ -27,6 +27,7 @@ import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.bootstrap.context.annotation.ConditionLogUtils; import org.springframework.bootstrap.context.annotation.ConditionalOnClass; import org.springframework.bootstrap.context.annotation.ConditionalOnMissingBean; import org.springframework.context.ApplicationContext; @@ -144,12 +145,15 @@ public class DataSourceAutoConfiguration { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + + String checking = ConditionLogUtils.getPrefix(this.logger, metadata); + if (this.tomcatCondition.matches(context, metadata) || this.dbcpCondition.matches(context, metadata) || this.embeddedCondition.matches(context, metadata)) { if (this.logger.isDebugEnabled()) { - this.logger - .debug("Existing auto database detected: match result true"); + this.logger.debug(checking + + "Existing auto database detected: match result true"); } return true; } @@ -157,10 +161,16 @@ public class DataSourceAutoConfiguration { context.getBeanFactory(), DataSource.class, true, false).length > 0) { if (this.logger.isDebugEnabled()) { this.logger - .debug("Existing bean configured database detected: match result true"); + .debug(checking + + "Existing bean configured database detected: match result true"); } return true; } + if (this.logger.isDebugEnabled()) { + this.logger + .debug(checking + + "Existing bean configured database not detected: match result false"); + } return false; } @@ -202,9 +212,12 @@ public class DataSourceAutoConfiguration { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + + String checking = ConditionLogUtils.getPrefix(this.logger, metadata); + if (!ClassUtils.isPresent(getDataSourecClassName(), null)) { if (this.logger.isDebugEnabled()) { - this.logger.debug("Tomcat DataSource pool not found"); + this.logger.debug(checking + "Tomcat DataSource pool not found"); } return false; } @@ -212,7 +225,8 @@ public class DataSourceAutoConfiguration { "spring.database.driverClassName"); String url = context.getEnvironment().getProperty("spring.database.url"); if (this.logger.isDebugEnabled()) { - this.logger.debug("Spring JDBC detected (embedded database type is " + this.logger.debug(checking + + "Spring JDBC detected (embedded database type is " + EmbeddedDatabaseConfiguration.getEmbeddedDatabaseType() + ")."); } if (driverClassName == null) { @@ -228,10 +242,15 @@ public class DataSourceAutoConfiguration { if (driverClassName != null && url != null && ClassUtils.isPresent(driverClassName, null)) { if (this.logger.isDebugEnabled()) { - this.logger.debug("Driver class " + driverClassName + " found"); + this.logger.debug(checking + "Driver class " + driverClassName + + " found"); } return true; } + if (this.logger.isDebugEnabled()) { + this.logger.debug(checking + "Driver class " + driverClassName + + " not found"); + } return false; } @@ -247,16 +266,21 @@ public class DataSourceAutoConfiguration { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + + String checking = ConditionLogUtils.getPrefix(this.logger, metadata); + if (this.tomcatCondition.matches(context, metadata) || this.dbcpCondition.matches(context, metadata)) { if (this.logger.isDebugEnabled()) { this.logger - .debug("Existing non-embedded database detected: match result false"); + .debug(checking + + "Existing non-embedded database detected: match result false"); } return false; } if (this.logger.isDebugEnabled()) { - this.logger.debug("Spring JDBC detected (embedded database type is " + this.logger.debug(checking + + "Spring JDBC detected (embedded database type is " + EmbeddedDatabaseConfiguration.getEmbeddedDatabaseType() + ")."); } return EmbeddedDatabaseConfiguration.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 65f2a9e8b4a..cd217a6e8d9 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 @@ -43,6 +43,9 @@ abstract class AbstractOnBeanCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + + String checking = ConditionLogUtils.getPrefix(this.logger, metadata); + MultiValueMap attributes = metadata.getAllAnnotationAttributes( annotationClass().getName(), true); List beanClasses = collect(attributes, "value"); @@ -78,23 +81,27 @@ abstract class AbstractOnBeanCondition implements Condition { boolean result = evaluate(beanClassesFound, beanNamesFound); if (this.logger.isDebugEnabled()) { if (!beanClasses.isEmpty()) { - this.logger.debug("Looking for beans with class: " + beanClasses); + this.logger.debug(checking + "Looking for beans with class: " + + beanClasses); if (beanClassesFound.isEmpty()) { - this.logger.debug("Found no beans"); + this.logger.debug(checking + "Found no beans"); } else { - this.logger.debug("Found beans with classes: " + beanClassesFound); + this.logger.debug(checking + "Found beans with classes: " + + beanClassesFound); } } if (!beanNames.isEmpty()) { - this.logger.debug("Looking for beans with names: " + beanNames); + this.logger + .debug(checking + "Looking for beans with names: " + beanNames); if (beanNamesFound.isEmpty()) { - this.logger.debug("Found no beans"); + this.logger.debug(checking + "Found no beans"); } else { - this.logger.debug("Found beans with names: " + beanNamesFound); + this.logger.debug(checking + "Found beans with names: " + + beanNamesFound); } } - this.logger.debug("Match result is: " + result); + this.logger.debug(checking + "Match result is: " + result); } return result; } diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ConditionLogUtils.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ConditionLogUtils.java new file mode 100644 index 00000000000..5b0206d6b14 --- /dev/null +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ConditionLogUtils.java @@ -0,0 +1,44 @@ +/* + * Copyright 2012-2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.bootstrap.context.annotation; + +import org.apache.commons.logging.Log; +import org.springframework.core.type.AnnotatedTypeMetadata; +import org.springframework.core.type.ClassMetadata; +import org.springframework.core.type.MethodMetadata; + +/** + * @author Dave Syer + * + */ +public class ConditionLogUtils { + + public static String getPrefix(Log logger, AnnotatedTypeMetadata metadata) { + String prefix = ""; + if (logger.isDebugEnabled()) { + prefix = metadata instanceof ClassMetadata ? "Processing " + + ((ClassMetadata) metadata).getClassName() + ". " + : (metadata instanceof MethodMetadata ? "Processing " + + getMethodName((MethodMetadata) metadata) + ". " : ""); + } + return prefix; + } + + private static String getMethodName(MethodMetadata metadata) { + return metadata.getDeclaringClassName() + "#" + metadata.getMethodName(); + } + +} 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 4d53689ed86..c862d995d59 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 @@ -37,6 +37,9 @@ public class ExpressionCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + + String checking = ConditionLogUtils.getPrefix(logger, metadata); + String value = (String) metadata.getAnnotationAttributes( ConditionalOnExpression.class.getName()).get("value"); if (!value.startsWith("#{")) { @@ -44,7 +47,8 @@ public class ExpressionCondition implements Condition { value = "#{" + value + "}"; } if (logger.isDebugEnabled()) { - StringBuilder builder = new StringBuilder("Evaluating expression"); + StringBuilder builder = new StringBuilder(checking) + .append("Evaluating expression"); if (metadata instanceof ClassMetadata) { builder.append(" on " + ((ClassMetadata) metadata).getClassName()); } @@ -59,7 +63,7 @@ public class ExpressionCondition implements Condition { beanFactory, null) : null; Boolean result = (Boolean) resolver.evaluate(value, expressionContext); if (logger.isDebugEnabled()) { - logger.debug("Finished matching and result is matches=" + result); + logger.debug(checking + "Finished matching and result is matches=" + result); } return result; } 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 85f62996ecc..dc7f15acea3 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 @@ -40,6 +40,9 @@ class OnClassCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + + String checking = ConditionLogUtils.getPrefix(logger, metadata); + MultiValueMap attributes = metadata.getAllAnnotationAttributes( ConditionalOnClass.class.getName(), true); if (attributes != null) { @@ -50,11 +53,11 @@ class OnClassCondition implements Condition { "@ConditionalOnClass annotations must specify at least one class value"); for (String className : classNames) { if (logger.isDebugEnabled()) { - logger.debug("Checking for class: " + className); + logger.debug(checking + "Looking for class: " + className); } if (!ClassUtils.isPresent(className, context.getClassLoader())) { if (logger.isDebugEnabled()) { - logger.debug("Class not found: " + className + logger.debug(checking + "Class not found: " + className + " (search terminated with matches=false)"); } return false; @@ -62,7 +65,7 @@ class OnClassCondition implements Condition { } } if (logger.isDebugEnabled()) { - logger.debug("Match result is: true"); + logger.debug(checking + "Match result is: true"); } return true; } diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnMissingClassCondition.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnMissingClassCondition.java index 43697887f44..7eb22f87a48 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnMissingClassCondition.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnMissingClassCondition.java @@ -40,6 +40,9 @@ class OnMissingClassCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + + String checking = ConditionLogUtils.getPrefix(logger, metadata); + MultiValueMap attributes = metadata.getAllAnnotationAttributes( ConditionalOnMissingClass.class.getName(), true); if (attributes != null) { @@ -49,11 +52,11 @@ class OnMissingClassCondition implements Condition { "@ConditionalOnMissingClass annotations must specify at least one class value"); for (String className : classNames) { if (logger.isDebugEnabled()) { - logger.debug("Checking for class: " + className); + logger.debug(checking + "Looking for class: " + className); } if (ClassUtils.isPresent(className, context.getClassLoader())) { if (logger.isDebugEnabled()) { - logger.debug("Found class: " + className + logger.debug(checking + "Found class: " + className + " (search terminated with matches=false)"); } return false; @@ -61,7 +64,7 @@ class OnMissingClassCondition implements Condition { } } if (logger.isDebugEnabled()) { - logger.debug("Match result is: true"); + logger.debug(checking + "Match result is: true"); } return true; } diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnNotWebApplicationCondition.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnNotWebApplicationCondition.java index cbe4a5bdcf9..e11190121d7 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnNotWebApplicationCondition.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnNotWebApplicationCondition.java @@ -37,20 +37,25 @@ class OnNotWebApplicationCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + + String checking = ConditionLogUtils.getPrefix(logger, metadata); + if (!ClassUtils.isPresent( "org.springframework.web.context.support.GenericWebApplicationContext", null)) { if (logger.isDebugEnabled()) { - logger.debug("Web application classes not found"); + logger.debug(checking + "Web application classes not found"); } return true; } boolean result = !StringUtils.arrayToCommaDelimitedString( context.getBeanFactory().getRegisteredScopeNames()).contains("session"); if (logger.isDebugEnabled()) { - logger.debug("Web application context found: " + !result); + logger.debug(checking + "Web application context found: " + !result); } + return result; + } } 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 3adb5219619..d9857c4c703 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 @@ -43,6 +43,9 @@ class OnResourceCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + + String checking = ConditionLogUtils.getPrefix(logger, metadata); + MultiValueMap attributes = metadata.getAllAnnotationAttributes( ConditionalOnClass.class.getName(), true); if (attributes != null) { @@ -52,17 +55,20 @@ class OnResourceCondition implements Condition { "@ConditionalOnResource annotations must specify at least one resource location"); for (String location : locations) { if (logger.isDebugEnabled()) { - logger.debug("Checking for resource: " + location); + logger.debug(checking + "Checking for resource: " + location); } if (!this.loader.getResource(location).exists()) { if (logger.isDebugEnabled()) { - logger.debug("Found resource: " + location + logger.debug(checking + "Found resource: " + location + " (search terminated with matches=false)"); } return false; } } } + if (logger.isDebugEnabled()) { + logger.debug(checking + "Match result is: true"); + } return true; } diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnWebApplicationCondition.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnWebApplicationCondition.java index 1930c3f9dc8..9ebaef7e73e 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnWebApplicationCondition.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/OnWebApplicationCondition.java @@ -37,11 +37,14 @@ class OnWebApplicationCondition implements Condition { @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { + + String checking = ConditionLogUtils.getPrefix(logger, metadata); + if (!ClassUtils.isPresent( "org.springframework.web.context.support.GenericWebApplicationContext", null)) { if (logger.isDebugEnabled()) { - logger.debug("Web application classes not found"); + logger.debug(checking + "Web application classes not found"); } return false; } @@ -49,7 +52,7 @@ class OnWebApplicationCondition implements Condition { context.getBeanFactory().getRegisteredScopeNames()).contains("session") || context.getEnvironment() instanceof StandardServletEnvironment; if (logger.isDebugEnabled()) { - logger.debug("Web application context found: " + result); + logger.debug(checking + "Web application context found: " + result); } return result; } diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/BasicDataSourceAutoConfigurationTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/BasicDataSourceConfigurationTests.java similarity index 95% rename from spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/BasicDataSourceAutoConfigurationTests.java rename to spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/BasicDataSourceConfigurationTests.java index 61621045a1c..e7dc1e0af36 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/BasicDataSourceAutoConfigurationTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/BasicDataSourceConfigurationTests.java @@ -26,7 +26,7 @@ import static org.junit.Assert.assertNotNull; * @author Dave Syer * */ -public class BasicDataSourceAutoConfigurationTests { +public class BasicDataSourceConfigurationTests { private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/EmbeddedDatabaseAutoConfigurationTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/EmbeddedDatabaseConfigurationTests.java similarity index 95% rename from spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/EmbeddedDatabaseAutoConfigurationTests.java rename to spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/EmbeddedDatabaseConfigurationTests.java index e7affd1d013..c8fabe2658d 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/EmbeddedDatabaseAutoConfigurationTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/EmbeddedDatabaseConfigurationTests.java @@ -26,7 +26,7 @@ import static org.junit.Assert.assertNotNull; * @author Dave Syer * */ -public class EmbeddedDatabaseAutoConfigurationTests { +public class EmbeddedDatabaseConfigurationTests { private AnnotationConfigApplicationContext context; diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/TomcatDataSourceAutoConfigurationTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/TomcatDataSourceConfigurationTests.java similarity index 95% rename from spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/TomcatDataSourceAutoConfigurationTests.java rename to spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/TomcatDataSourceConfigurationTests.java index ab814055552..300f5c48157 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/TomcatDataSourceAutoConfigurationTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/autoconfigure/jdbc/TomcatDataSourceConfigurationTests.java @@ -26,7 +26,7 @@ import static org.junit.Assert.assertNotNull; * @author Dave Syer * */ -public class TomcatDataSourceAutoConfigurationTests { +public class TomcatDataSourceConfigurationTests { private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnMissingBeanConditionTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnMissingBeanConditionTests.java new file mode 100644 index 00000000000..f0cce810f85 --- /dev/null +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/context/annotation/OnMissingBeanConditionTests.java @@ -0,0 +1,69 @@ +/* + * Copyright 2012-2013 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.bootstrap.context.annotation; + +import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * @author Dave Syer + * + */ +public class OnMissingBeanConditionTests { + + private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + + @Test + public void testNameOnMissingBeanCondition() { + this.context.register(FooConfiguration.class, OnBeanNameConfiguration.class); + this.context.refresh(); + assertFalse(this.context.containsBean("bar")); + assertEquals("foo", this.context.getBean("foo")); + } + + @Test + public void testNameOnMissingBeanConditionReverseOrder() { + this.context.register(OnBeanNameConfiguration.class, FooConfiguration.class); + this.context.refresh(); + // FIXME: ideally this would be false, but the ordering is a problem + assertTrue(this.context.containsBean("bar")); + assertEquals("foo", this.context.getBean("foo")); + } + + @Configuration + @ConditionalOnMissingBean(name = "foo") + protected static class OnBeanNameConfiguration { + @Bean + public String bar() { + return "bar"; + } + } + + @Configuration + protected static class FooConfiguration { + @Bean + public String foo() { + return "foo"; + } + } + +}