mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Gracefully handle exclusion of DataSourceAutoConfiguration
This commit makes sure that an auto-configuration that requires `DataSourceProperties` will not break if `DataSourceAutoConfiguration` has been explicitly excluded. Closes gh-12512
This commit is contained in:
parent
2f5ab50367
commit
92d9479765
@ -94,7 +94,7 @@ public class FlywayAutoConfiguration {
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(Flyway.class)
|
||||
@EnableConfigurationProperties(FlywayProperties.class)
|
||||
@EnableConfigurationProperties({ DataSourceProperties.class, FlywayProperties.class })
|
||||
public static class FlywayConfiguration {
|
||||
|
||||
private final FlywayProperties properties;
|
||||
|
@ -80,7 +80,7 @@ public class LiquibaseAutoConfiguration {
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(SpringLiquibase.class)
|
||||
@EnableConfigurationProperties(LiquibaseProperties.class)
|
||||
@EnableConfigurationProperties({ DataSourceProperties.class, LiquibaseProperties.class })
|
||||
@Import(LiquibaseJpaDependencyConfiguration.class)
|
||||
public static class LiquibaseConfiguration {
|
||||
|
||||
|
@ -105,6 +105,16 @@ public class FlywayAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void flywayDataSourceWithoutDataSourceAutoConfiguration() {
|
||||
this.contextRunner.withUserConfiguration(FlywayDataSourceConfiguration.class)
|
||||
.run((context) -> {
|
||||
assertThat(context).hasSingleBean(Flyway.class);
|
||||
assertThat(context.getBean(Flyway.class).getDataSource())
|
||||
.isEqualTo(context.getBean("flywayDataSource"));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void schemaManagementProviderDetectsDataSource() {
|
||||
this.contextRunner.withUserConfiguration(FlywayDataSourceConfiguration.class,
|
||||
|
@ -248,6 +248,17 @@ public class LiquibaseAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void liquibaseDataSourceWithoutDataSourceAutoConfiguration() {
|
||||
this.contextRunner
|
||||
.withUserConfiguration(LiquibaseDataSourceConfiguration.class)
|
||||
.run((context) -> {
|
||||
SpringLiquibase liquibase = context.getBean(SpringLiquibase.class);
|
||||
assertThat(liquibase.getDataSource())
|
||||
.isEqualTo(context.getBean("liquibaseDataSource"));
|
||||
});
|
||||
}
|
||||
|
||||
private ContextConsumer<AssertableApplicationContext> assertLiquibase(
|
||||
Consumer<SpringLiquibase> consumer) {
|
||||
return (context) -> {
|
||||
|
@ -28,8 +28,6 @@ import org.junit.Test;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
@ -53,7 +51,6 @@ public abstract class AbstractDevToolsDataSourceAutoConfigurationTests {
|
||||
@Test
|
||||
public void singleManuallyConfiguredDataSourceIsNotClosed() throws SQLException {
|
||||
ConfigurableApplicationContext context = createContext(
|
||||
DataSourcePropertiesConfiguration.class,
|
||||
SingleDataSourceConfiguration.class);
|
||||
DataSource dataSource = context.getBean(DataSource.class);
|
||||
Statement statement = configureDataSourceBehavior(dataSource);
|
||||
@ -63,7 +60,6 @@ public abstract class AbstractDevToolsDataSourceAutoConfigurationTests {
|
||||
@Test
|
||||
public void multipleDataSourcesAreIgnored() throws SQLException {
|
||||
ConfigurableApplicationContext context = createContext(
|
||||
DataSourcePropertiesConfiguration.class,
|
||||
MultipleDataSourcesConfiguration.class);
|
||||
Collection<DataSource> dataSources = context.getBeansOfType(DataSource.class)
|
||||
.values();
|
||||
@ -80,7 +76,6 @@ public abstract class AbstractDevToolsDataSourceAutoConfigurationTests {
|
||||
AnnotatedGenericBeanDefinition beanDefinition = new AnnotatedGenericBeanDefinition(
|
||||
dataSource.getClass());
|
||||
context.registerBeanDefinition("dataSource", beanDefinition);
|
||||
context.register(DataSourcePropertiesConfiguration.class);
|
||||
context.register(DevToolsDataSourceAutoConfiguration.class);
|
||||
context.refresh();
|
||||
context.close();
|
||||
@ -146,12 +141,6 @@ public abstract class AbstractDevToolsDataSourceAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(DataSourceProperties.class)
|
||||
static class DataSourcePropertiesConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class DataSourceSpyConfiguration {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user