diff --git a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.java b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.java index 65b29ce6f1..1a5c712918 100644 --- a/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.java +++ b/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfiguration.java @@ -25,10 +25,13 @@ import javax.sql.DataSource; import org.springframework.beans.factory.DisposableBean; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.AllNestedConditions; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; +import org.springframework.boot.devtools.autoconfigure.DevToolsDataSourceAutoConfiguration.DevToolsDataSourceCondition; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; @@ -40,7 +43,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; * @since 1.3.3 */ @AutoConfigureAfter(DataSourceAutoConfiguration.class) -@ConditionalOnBean({ DataSource.class, DataSourceProperties.class }) +@Conditional(DevToolsDataSourceCondition.class) @Configuration public class DevToolsDataSourceAutoConfiguration { @@ -85,4 +88,22 @@ public class DevToolsDataSourceAutoConfiguration { } + static class DevToolsDataSourceCondition extends AllNestedConditions { + + DevToolsDataSourceCondition() { + super(ConfigurationPhase.REGISTER_BEAN); + } + + @ConditionalOnBean(DataSource.class) + static final class DataSourceBean { + + } + + @ConditionalOnBean(DataSourceProperties.class) + static final class DataSourcePropertiesBean { + + } + + } + } diff --git a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfigurationTests.java b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfigurationTests.java index 57c5a340d4..5b0b21e63f 100644 --- a/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfigurationTests.java +++ b/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/autoconfigure/DevToolsDataSourceAutoConfigurationTests.java @@ -33,6 +33,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -76,6 +77,14 @@ public class DevToolsDataSourceAutoConfigurationTests { verify(statement).execute("SHUTDOWN"); } + @Test + public void configurationBacksOffWithoutDataSourceProperties() throws SQLException { + ConfigurableApplicationContext context = createContext("org.h2.Driver", + NoDataSourcePropertiesConfiguration.class); + assertThat(context.getBeansOfType(DevToolsDataSourceAutoConfiguration.class)) + .isEmpty(); + } + private ConfigurableApplicationContext createContext(String driver, Class... classes) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); @@ -108,4 +117,14 @@ public class DevToolsDataSourceAutoConfigurationTests { } + @Configuration + static class NoDataSourcePropertiesConfiguration { + + @Bean + public DataSource in() { + return mock(DataSource.class); + } + + } + }