|
|
|
|
@@ -51,7 +51,7 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
|
|
|
|
* @author Phillip Webb
|
|
|
|
|
*/
|
|
|
|
|
@Configuration
|
|
|
|
|
@ConditionalOnClass(EmbeddedDatabaseType.class)
|
|
|
|
|
@ConditionalOnClass({ DataSource.class, EmbeddedDatabaseType.class })
|
|
|
|
|
@EnableConfigurationProperties(DataSourceProperties.class)
|
|
|
|
|
@Import(Registrar.class)
|
|
|
|
|
public class DataSourceAutoConfiguration {
|
|
|
|
|
@@ -73,7 +73,7 @@ public class DataSourceAutoConfiguration {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Conditional(DataSourceAutoConfiguration.EmbeddedDatabaseCondition.class)
|
|
|
|
|
@Conditional(DataSourceAutoConfiguration.EmbeddedDataSourceCondition.class)
|
|
|
|
|
@ConditionalOnMissingBean(DataSource.class)
|
|
|
|
|
@Import(EmbeddedDataSourceConfiguration.class)
|
|
|
|
|
protected static class EmbeddedConfiguration {
|
|
|
|
|
@@ -88,9 +88,10 @@ public class DataSourceAutoConfiguration {
|
|
|
|
|
public DataSourceInitializer dataSourceInitializer() {
|
|
|
|
|
return new DataSourceInitializer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Conditional(DataSourceAutoConfiguration.NonEmbeddedDatabaseCondition.class)
|
|
|
|
|
@Conditional(DataSourceAutoConfiguration.NonEmbeddedDataSourceCondition.class)
|
|
|
|
|
@ConditionalOnMissingBean(DataSource.class)
|
|
|
|
|
protected static class NonEmbeddedConfiguration {
|
|
|
|
|
|
|
|
|
|
@@ -112,7 +113,7 @@ public class DataSourceAutoConfiguration {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Configuration
|
|
|
|
|
@Conditional(DataSourceAutoConfiguration.DatabaseCondition.class)
|
|
|
|
|
@Conditional(DataSourceAutoConfiguration.DataSourceAvailableCondition.class)
|
|
|
|
|
protected static class JdbcTemplateConfiguration {
|
|
|
|
|
|
|
|
|
|
@Autowired(required = false)
|
|
|
|
|
@@ -133,19 +134,17 @@ public class DataSourceAutoConfiguration {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Base {@link Condition} for non-embedded database checks.
|
|
|
|
|
* {@link Condition} to test is a supported non-embedded {@link DataSource} type is
|
|
|
|
|
* available.
|
|
|
|
|
*/
|
|
|
|
|
static class NonEmbeddedDatabaseCondition extends SpringBootCondition {
|
|
|
|
|
static class NonEmbeddedDataSourceCondition extends SpringBootCondition {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ConditionOutcome getMatchOutcome(ConditionContext context,
|
|
|
|
|
AnnotatedTypeMetadata metadata) {
|
|
|
|
|
|
|
|
|
|
ClassLoader dataSourceClassLoader = getDataSourceClassLoader(context);
|
|
|
|
|
if (dataSourceClassLoader != null) {
|
|
|
|
|
return ConditionOutcome.match("Supported DataSource class found");
|
|
|
|
|
if (getDataSourceClassLoader(context) != null) {
|
|
|
|
|
return ConditionOutcome.match("supported DataSource class found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ConditionOutcome.noMatch("missing supported DataSource");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -156,20 +155,16 @@ public class DataSourceAutoConfiguration {
|
|
|
|
|
private ClassLoader getDataSourceClassLoader(ConditionContext context) {
|
|
|
|
|
Class<?> dataSourceClass = new DataSourceBuilder(context.getClassLoader())
|
|
|
|
|
.findType();
|
|
|
|
|
if (dataSourceClass == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return dataSourceClass.getClassLoader();
|
|
|
|
|
return (dataSourceClass == null ? null : dataSourceClass.getClassLoader());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@link Condition} to detect when an embedded database is used.
|
|
|
|
|
* {@link Condition} to detect when an embedded {@link DataSource} type can be used.
|
|
|
|
|
*/
|
|
|
|
|
static class EmbeddedDatabaseCondition extends SpringBootCondition {
|
|
|
|
|
static class EmbeddedDataSourceCondition extends SpringBootCondition {
|
|
|
|
|
|
|
|
|
|
private final SpringBootCondition nonEmbedded = new NonEmbeddedDatabaseCondition();
|
|
|
|
|
private final SpringBootCondition nonEmbedded = new NonEmbeddedDataSourceCondition();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ConditionOutcome getMatchOutcome(ConditionContext context,
|
|
|
|
|
@@ -189,27 +184,25 @@ public class DataSourceAutoConfiguration {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@link Condition} to detect when a database is configured.
|
|
|
|
|
* {@link Condition} to detect when a {@link DataSource} is available (either because
|
|
|
|
|
* the user provided one or because one will be auto-configured)
|
|
|
|
|
*/
|
|
|
|
|
static class DatabaseCondition extends SpringBootCondition {
|
|
|
|
|
static class DataSourceAvailableCondition extends SpringBootCondition {
|
|
|
|
|
|
|
|
|
|
private final SpringBootCondition nonEmbedded = new NonEmbeddedDatabaseCondition();
|
|
|
|
|
private final SpringBootCondition nonEmbedded = new NonEmbeddedDataSourceCondition();
|
|
|
|
|
|
|
|
|
|
private final SpringBootCondition embeddedCondition = new EmbeddedDatabaseCondition();
|
|
|
|
|
private final SpringBootCondition embeddedCondition = new EmbeddedDataSourceCondition();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public ConditionOutcome getMatchOutcome(ConditionContext context,
|
|
|
|
|
AnnotatedTypeMetadata metadata) {
|
|
|
|
|
|
|
|
|
|
if (hasBean(context, DataSource.class)) {
|
|
|
|
|
return ConditionOutcome
|
|
|
|
|
.match("existing bean configured database detected");
|
|
|
|
|
}
|
|
|
|
|
if (anyMatches(context, metadata, this.nonEmbedded, this.embeddedCondition)) {
|
|
|
|
|
return ConditionOutcome.match("existing auto database detected");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (hasBean(context, DataSource.class)) {
|
|
|
|
|
return ConditionOutcome
|
|
|
|
|
.match("Existing bean configured database detected");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ConditionOutcome.noMatch("no existing bean configured database");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|