From 69ab956e8b75ed1f6bee77b7c7aa9c2eb82223bd Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 3 May 2018 11:46:27 +0200 Subject: [PATCH] Stop assuming datasource creation failure originates from auto-config There are documented way to reuse bits of the infrastructure in user config to offer similar datasource configuration. If that fails, the regular failure there will kick in. This commit improves `DataSourceBeanCreationFailureAnalyzer` to not misguide users that the auto-configuration has failed. Rather, it describes what has failed in a more generic way. Closes gh-12947 --- .../jdbc/DataSourceBeanCreationFailureAnalyzer.java | 8 ++++---- .../autoconfigure/jdbc/DataSourceProperties.java | 13 ++++++++++--- .../DataSourceBeanCreationFailureAnalyzerTests.java | 4 ++-- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceBeanCreationFailureAnalyzer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceBeanCreationFailureAnalyzer.java index 38fdda176c5..5415ffef1a1 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceBeanCreationFailureAnalyzer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceBeanCreationFailureAnalyzer.java @@ -58,12 +58,12 @@ class DataSourceBeanCreationFailureAnalyzer private String getDescription(DataSourceBeanCreationException cause) { StringBuilder description = new StringBuilder(); - description.append("Failed to auto-configure a DataSource: "); - if (!this.environment.containsProperty("spring.datasource.url")) { - description.append("'spring.datasource.url' is not specified and "); + description.append("Failed to configure a DataSource: "); + if (!StringUtils.hasText(cause.getProperties().getUrl())) { + description.append("'url' attribute is not specified and "); } description.append( - String.format("no embedded datasource could be auto-configured.%n")); + String.format("no embedded datasource could be configured.%n")); description.append(String.format("%nReason: %s%n", cause.getMessage())); return description.toString(); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java index c71ae005729..27767dfe8c8 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java @@ -235,7 +235,7 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB if (!StringUtils.hasText(driverClassName)) { throw new DataSourceBeanCreationException( "Failed to determine a suitable driver class", - this.embeddedDatabaseConnection); + this, this.embeddedDatabaseConnection); } return driverClassName; } @@ -282,7 +282,7 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB if (!StringUtils.hasText(url)) { throw new DataSourceBeanCreationException( "Failed to determine suitable jdbc url", - this.embeddedDatabaseConnection); + this, this.embeddedDatabaseConnection); } return url; } @@ -513,14 +513,21 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB static class DataSourceBeanCreationException extends BeanCreationException { + private final DataSourceProperties properties; + private final EmbeddedDatabaseConnection connection; - DataSourceBeanCreationException(String message, + DataSourceBeanCreationException(String message, DataSourceProperties properties, EmbeddedDatabaseConnection connection) { super(message); + this.properties = properties; this.connection = connection; } + public DataSourceProperties getProperties() { + return this.properties; + } + public EmbeddedDatabaseConnection getConnection() { return this.connection; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceBeanCreationFailureAnalyzerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceBeanCreationFailureAnalyzerTests.java index 6422dd98353..9bea0d17042 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceBeanCreationFailureAnalyzerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceBeanCreationFailureAnalyzerTests.java @@ -46,8 +46,8 @@ public class DataSourceBeanCreationFailureAnalyzerTests { public void failureAnalysisIsPerformed() { FailureAnalysis failureAnalysis = performAnalysis(TestConfiguration.class); assertThat(failureAnalysis.getDescription()).contains( - "'spring.datasource.url' is not specified", - "no embedded datasource could be auto-configured", + "'url' attribute is not specified", + "no embedded datasource could be configured", "Failed to determine a suitable driver class"); assertThat(failureAnalysis.getAction()).contains( "If you want an embedded database (H2, HSQL or Derby), please put it on the classpath",