Clarify DataSourceAutoConfiguration

The `DataSource` auto-configuration in Spring Boot supports two modes:
regular pooled DataSource and embedded database (via the
`EmbeddedDatabase` infrastructure provided by `spring-jdbc`. These were
previously named `NonEmbedded` and `Embedded` respectively.

This commit clarifies those mode and in particular that a pooled
`DataSource` can also handle an embedded database.

Closes gh-4634
pull/5041/head
Stephane Nicoll 9 years ago
parent f33d95bc32
commit 217f825d3d

@ -60,6 +60,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
* *
* @author Dave Syer * @author Dave Syer
* @author Phillip Webb * @author Phillip Webb
* @author Stephane Nicoll
*/ */
@Configuration @Configuration
@ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class }) @ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class })
@ -87,10 +88,10 @@ public class DataSourceAutoConfiguration {
} }
} }
@Conditional(DataSourceAutoConfiguration.EmbeddedDataSourceCondition.class) @Conditional(EmbeddedDatabaseCondition.class)
@ConditionalOnMissingBean({ DataSource.class, XADataSource.class }) @ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
@Import(EmbeddedDataSourceConfiguration.class) @Import(EmbeddedDataSourceConfiguration.class)
protected static class EmbeddedConfiguration { protected static class EmbeddedDatabaseConfiguration {
} }
@ -105,9 +106,9 @@ public class DataSourceAutoConfiguration {
} }
@Conditional(DataSourceAutoConfiguration.NonEmbeddedDataSourceCondition.class) @Conditional(PooledDataSourceCondition.class)
@ConditionalOnMissingBean({ DataSource.class, XADataSource.class }) @ConditionalOnMissingBean({ DataSource.class, XADataSource.class })
protected static class NonEmbeddedConfiguration { protected static class PooledDataSourceConfiguration {
@Autowired @Autowired
private DataSourceProperties properties; private DataSourceProperties properties;
@ -171,10 +172,9 @@ public class DataSourceAutoConfiguration {
} }
/** /**
* {@link Condition} to test is a supported non-embedded {@link DataSource} type is * {@link Condition} to test if a supported connection pool is available.
* available.
*/ */
static class NonEmbeddedDataSourceCondition extends SpringBootCondition { static class PooledDataSourceCondition extends SpringBootCondition {
@Override @Override
public ConditionOutcome getMatchOutcome(ConditionContext context, public ConditionOutcome getMatchOutcome(ConditionContext context,
@ -200,17 +200,20 @@ public class DataSourceAutoConfiguration {
/** /**
* {@link Condition} to detect when an embedded {@link DataSource} type can be used. * {@link Condition} to detect when an embedded {@link DataSource} type can be used.
* <p>
* If a pooled {@link DataSource} is available, it will always be preferred to
* an {@code EmbeddedDatabase}.
*/ */
static class EmbeddedDataSourceCondition extends SpringBootCondition { static class EmbeddedDatabaseCondition extends SpringBootCondition {
private final SpringBootCondition nonEmbedded = new NonEmbeddedDataSourceCondition(); private final SpringBootCondition pooledCondition = new PooledDataSourceCondition();
@Override @Override
public ConditionOutcome getMatchOutcome(ConditionContext context, public ConditionOutcome getMatchOutcome(ConditionContext context,
AnnotatedTypeMetadata metadata) { AnnotatedTypeMetadata metadata) {
if (anyMatches(context, metadata, this.nonEmbedded)) { if (anyMatches(context, metadata, this.pooledCondition)) {
return ConditionOutcome return ConditionOutcome
.noMatch("existing non-embedded database detected"); .noMatch("supported DataSource class found");
} }
EmbeddedDatabaseType type = EmbeddedDatabaseConnection EmbeddedDatabaseType type = EmbeddedDatabaseConnection
.get(context.getClassLoader()).getType(); .get(context.getClassLoader()).getType();
@ -229,9 +232,9 @@ public class DataSourceAutoConfiguration {
@Order(Ordered.LOWEST_PRECEDENCE - 10) @Order(Ordered.LOWEST_PRECEDENCE - 10)
static class DataSourceAvailableCondition extends SpringBootCondition { static class DataSourceAvailableCondition extends SpringBootCondition {
private final SpringBootCondition nonEmbedded = new NonEmbeddedDataSourceCondition(); private final SpringBootCondition pooledCondition = new PooledDataSourceCondition();
private final SpringBootCondition embeddedCondition = new EmbeddedDataSourceCondition(); private final SpringBootCondition embeddedCondition = new EmbeddedDatabaseCondition();
@Override @Override
public ConditionOutcome getMatchOutcome(ConditionContext context, public ConditionOutcome getMatchOutcome(ConditionContext context,
@ -241,7 +244,7 @@ public class DataSourceAutoConfiguration {
return ConditionOutcome return ConditionOutcome
.match("existing bean configured database detected"); .match("existing bean configured database detected");
} }
if (anyMatches(context, metadata, this.nonEmbedded, this.embeddedCondition)) { if (anyMatches(context, metadata, this.pooledCondition, this.embeddedCondition)) {
return ConditionOutcome.match("existing auto database detected"); return ConditionOutcome.match("existing auto database detected");
} }
return ConditionOutcome.noMatch("no existing bean configured database"); return ConditionOutcome.noMatch("no existing bean configured database");

Loading…
Cancel
Save