mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-05 00:56:58 +08:00
Improve logging for @Conditional processing
[Fixes #49989913] [bs-125] @Conditional* seem to get processed multiple times?
This commit is contained in:
parent
00c227ac01
commit
1a0902b32a
@ -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;
|
||||
|
@ -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<String, Object> attributes = metadata.getAllAnnotationAttributes(
|
||||
annotationClass().getName(), true);
|
||||
List<String> 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;
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
@ -40,6 +40,9 @@ class OnClassCondition implements Condition {
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
|
||||
String checking = ConditionLogUtils.getPrefix(logger, metadata);
|
||||
|
||||
MultiValueMap<String, Object> 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;
|
||||
}
|
||||
|
@ -40,6 +40,9 @@ class OnMissingClassCondition implements Condition {
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
|
||||
String checking = ConditionLogUtils.getPrefix(logger, metadata);
|
||||
|
||||
MultiValueMap<String, Object> 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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -43,6 +43,9 @@ class OnResourceCondition implements Condition {
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
|
||||
String checking = ConditionLogUtils.getPrefix(logger, metadata);
|
||||
|
||||
MultiValueMap<String, Object> 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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ import static org.junit.Assert.assertNotNull;
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class BasicDataSourceAutoConfigurationTests {
|
||||
public class BasicDataSourceConfigurationTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
|
@ -26,7 +26,7 @@ import static org.junit.Assert.assertNotNull;
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class EmbeddedDatabaseAutoConfigurationTests {
|
||||
public class EmbeddedDatabaseConfigurationTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context;
|
||||
|
@ -26,7 +26,7 @@ import static org.junit.Assert.assertNotNull;
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public class TomcatDataSourceAutoConfigurationTests {
|
||||
public class TomcatDataSourceConfigurationTests {
|
||||
|
||||
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
|
@ -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";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user