From 06f3202c68a9440cf5370a237704fcd76f55ee86 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 24 Sep 2015 10:24:08 -0700 Subject: [PATCH] Improve DataSourceProperties exception messages Update DataSourceProperties exceptions to include a less misleading message. Errors message now note that you may need to add an embedded database to the classpath or active a profile to pickup specific settings. Fixes gh-4012 --- .../jdbc/DataSourceProperties.java | 65 +++++++++++++++---- 1 file changed, 52 insertions(+), 13 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java index d7dcae8732d0..9d8959de6ed1 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java @@ -26,8 +26,11 @@ import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.InitializingBean; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.EnvironmentAware; +import org.springframework.core.env.Environment; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; /** @@ -39,10 +42,15 @@ * @since 1.1.0 */ @ConfigurationProperties(prefix = DataSourceProperties.PREFIX) -public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean { +public class DataSourceProperties implements BeanClassLoaderAware, EnvironmentAware, + InitializingBean { public static final String PREFIX = "spring.datasource"; + private ClassLoader classLoader; + + private Environment environment; + /** * Name of the datasource. */ @@ -74,8 +82,6 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB */ private String password; - private ClassLoader classLoader; - /** * JNDI location of the datasource. Class, url, username & password are ignored when * set. @@ -126,6 +132,11 @@ public void setBeanClassLoader(ClassLoader classLoader) { this.classLoader = classLoader; } + @Override + public void setEnvironment(Environment environment) { + this.environment = environment; + } + @Override public void afterPropertiesSet() throws Exception { this.embeddedDatabaseConnection = EmbeddedDatabaseConnection @@ -165,11 +176,8 @@ public String getDriverClassName() { } if (!StringUtils.hasText(driverClassName)) { - throw new BeanCreationException( - "Cannot determine embedded database driver class for database type " - + this.embeddedDatabaseConnection - + ". If you want an embedded " - + "database please put a supported one on the classpath."); + throw new DataSourceBeanCreationException(this.embeddedDatabaseConnection, + this.environment, "driver class"); } return driverClassName; } @@ -184,11 +192,8 @@ public String getUrl() { } String url = this.embeddedDatabaseConnection.getUrl(); if (!StringUtils.hasText(url)) { - throw new BeanCreationException( - "Cannot determine embedded database url for database type " - + this.embeddedDatabaseConnection - + ". If you want an embedded " - + "database please put a supported one on the classpath."); + throw new DataSourceBeanCreationException(this.embeddedDatabaseConnection, + this.environment, "url"); } return url; } @@ -340,4 +345,38 @@ public void setProperties(Map properties) { } + private static class DataSourceBeanCreationException extends BeanCreationException { + + public DataSourceBeanCreationException(EmbeddedDatabaseConnection connection, + Environment environment, String property) { + super(getMessage(connection, environment, property)); + } + + private static String getMessage(EmbeddedDatabaseConnection connection, + Environment environment, String property) { + StringBuilder message = new StringBuilder(); + message.append("Cannot determine embedded database " + property + + " for database type " + connection + ". "); + message.append("If you want an embedded database please put a supported " + + "one on the classpath. "); + message.append("If you have database settings to be loaded from a " + + "particular profile you may need to active it"); + if (environment != null) { + String[] profiles = environment.getActiveProfiles(); + if (ObjectUtils.isEmpty(profiles)) { + message.append(" (no profiles are currently active)"); + } + else { + message.append(" (the profiles \"" + + StringUtils.arrayToCommaDelimitedString(environment + .getActiveProfiles()) + "\" are currently active)"); + + } + } + message.append("."); + return message.toString(); + } + + } + }