mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-07-15 01:07:30 +08:00
[bs-53] Aid for users debugging auto-configuration failures
Add logging to Condition implementations. [Fixes #48389533]
This commit is contained in:
parent
6fb8ae3d3b
commit
ad3076784d
@ -22,6 +22,8 @@ import java.util.Map;
|
|||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
import javax.sql.DataSource;
|
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.beans.factory.annotation.Value;
|
||||||
import org.springframework.bootstrap.context.annotation.ConditionalOnMissingBean;
|
import org.springframework.bootstrap.context.annotation.ConditionalOnMissingBean;
|
||||||
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
|
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
|
||||||
@ -48,6 +50,9 @@ import org.springframework.util.ClassUtils;
|
|||||||
@ConditionalOnMissingBean(DataSource.class)
|
@ConditionalOnMissingBean(DataSource.class)
|
||||||
public class EmbeddedDatabaseAutoConfiguration {
|
public class EmbeddedDatabaseAutoConfiguration {
|
||||||
|
|
||||||
|
private static Log logger = LogFactory
|
||||||
|
.getLog(EmbeddedDatabaseAutoConfiguration.class);
|
||||||
|
|
||||||
private static final Map<EmbeddedDatabaseType, String> EMBEDDED_DATABASE_TYPE_CLASSES;
|
private static final Map<EmbeddedDatabaseType, String> EMBEDDED_DATABASE_TYPE_CLASSES;
|
||||||
static {
|
static {
|
||||||
EMBEDDED_DATABASE_TYPE_CLASSES = new LinkedHashMap<EmbeddedDatabaseType, String>();
|
EMBEDDED_DATABASE_TYPE_CLASSES = new LinkedHashMap<EmbeddedDatabaseType, String>();
|
||||||
@ -100,8 +105,15 @@ public class EmbeddedDatabaseAutoConfiguration {
|
|||||||
if (!ClassUtils.isPresent(
|
if (!ClassUtils.isPresent(
|
||||||
"org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType",
|
"org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType",
|
||||||
context.getClassLoader())) {
|
context.getClassLoader())) {
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug("Spring JDBC not detected (EmbeddedDatabaseCondition evaluated false).");
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug("Spring JDBC detected (embedded database type is "
|
||||||
|
+ getEmbeddedDatabaseType() + ").");
|
||||||
|
}
|
||||||
return getEmbeddedDatabaseType() != null;
|
return getEmbeddedDatabaseType() != null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,8 @@ package org.springframework.bootstrap.context.annotation;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
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.beans.factory.BeanFactoryUtils;
|
||||||
import org.springframework.context.annotation.Condition;
|
import org.springframework.context.annotation.Condition;
|
||||||
import org.springframework.context.annotation.ConditionContext;
|
import org.springframework.context.annotation.ConditionContext;
|
||||||
@ -34,6 +36,8 @@ import org.springframework.util.MultiValueMap;
|
|||||||
*/
|
*/
|
||||||
abstract class AbstractOnBeanCondition implements Condition {
|
abstract class AbstractOnBeanCondition implements Condition {
|
||||||
|
|
||||||
|
private static Log logger = LogFactory.getLog(OnBeanCondition.class);
|
||||||
|
|
||||||
protected abstract Class<?> annotationClass();
|
protected abstract Class<?> annotationClass();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -49,6 +53,9 @@ abstract class AbstractOnBeanCondition implements Condition {
|
|||||||
List<String> beanClassesFound = new ArrayList<String>();
|
List<String> beanClassesFound = new ArrayList<String>();
|
||||||
List<String> beanNamesFound = new ArrayList<String>();
|
List<String> beanNamesFound = new ArrayList<String>();
|
||||||
|
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug("Looking for beans with class: " + beanClasses);
|
||||||
|
}
|
||||||
for (String beanClass : beanClasses) {
|
for (String beanClass : beanClasses) {
|
||||||
try {
|
try {
|
||||||
// eagerInit set to false to prevent early instantiation (some
|
// 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) {
|
for (String beanName : beanNames) {
|
||||||
if (context.getBeanFactory().containsBeanDefinition(beanName)) {
|
if (context.getBeanFactory().containsBeanDefinition(beanName)) {
|
||||||
beanNamesFound.add(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) {
|
protected boolean evaluate(List<String> beanClassesFound, List<String> beanNamesFound) {
|
||||||
|
@ -57,7 +57,11 @@ public class ExpressionCondition implements Condition {
|
|||||||
BeanExpressionResolver resolver = beanFactory.getBeanExpressionResolver();
|
BeanExpressionResolver resolver = beanFactory.getBeanExpressionResolver();
|
||||||
BeanExpressionContext expressionContext = (beanFactory != null) ? new BeanExpressionContext(
|
BeanExpressionContext expressionContext = (beanFactory != null) ? new BeanExpressionContext(
|
||||||
beanFactory, null) : null;
|
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.ArrayList;
|
||||||
import java.util.List;
|
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.Condition;
|
||||||
import org.springframework.context.annotation.ConditionContext;
|
import org.springframework.context.annotation.ConditionContext;
|
||||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||||
@ -34,6 +36,8 @@ import org.springframework.util.MultiValueMap;
|
|||||||
*/
|
*/
|
||||||
class OnClassCondition implements Condition {
|
class OnClassCondition implements Condition {
|
||||||
|
|
||||||
|
private static Log logger = LogFactory.getLog(OnClassCondition.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||||
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(
|
MultiValueMap<String, Object> attributes = metadata.getAllAnnotationAttributes(
|
||||||
@ -45,7 +49,14 @@ class OnClassCondition implements Condition {
|
|||||||
Assert.isTrue(classNames.size() > 0,
|
Assert.isTrue(classNames.size() > 0,
|
||||||
"@ConditionalOnClass annotations must specify at least one class value");
|
"@ConditionalOnClass annotations must specify at least one class value");
|
||||||
for (String className : classNames) {
|
for (String className : classNames) {
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug("Checking for class: " + className);
|
||||||
|
}
|
||||||
if (!ClassUtils.isPresent(className, context.getClassLoader())) {
|
if (!ClassUtils.isPresent(className, context.getClassLoader())) {
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug("Found class: " + className
|
||||||
|
+ " (search terminated with matches=false)");
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,8 @@ package org.springframework.bootstrap.context.annotation;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
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.Condition;
|
||||||
import org.springframework.context.annotation.ConditionContext;
|
import org.springframework.context.annotation.ConditionContext;
|
||||||
import org.springframework.core.io.DefaultResourceLoader;
|
import org.springframework.core.io.DefaultResourceLoader;
|
||||||
@ -35,6 +37,8 @@ import org.springframework.util.MultiValueMap;
|
|||||||
*/
|
*/
|
||||||
class OnResourceCondition implements Condition {
|
class OnResourceCondition implements Condition {
|
||||||
|
|
||||||
|
private static Log logger = LogFactory.getLog(OnResourceCondition.class);
|
||||||
|
|
||||||
private ResourceLoader loader = new DefaultResourceLoader();
|
private ResourceLoader loader = new DefaultResourceLoader();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -47,7 +51,14 @@ class OnResourceCondition implements Condition {
|
|||||||
Assert.isTrue(locations.size() > 0,
|
Assert.isTrue(locations.size() > 0,
|
||||||
"@ConditionalOnResource annotations must specify at least one resource location");
|
"@ConditionalOnResource annotations must specify at least one resource location");
|
||||||
for (String location : locations) {
|
for (String location : locations) {
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug("Checking for resource: " + location);
|
||||||
|
}
|
||||||
if (!this.loader.getResource(location).exists()) {
|
if (!this.loader.getResource(location).exists()) {
|
||||||
|
if (logger.isDebugEnabled()) {
|
||||||
|
logger.debug("Found resource: " + location
|
||||||
|
+ " (search terminated with matches=false)");
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user