|
|
@ -19,11 +19,13 @@ package org.springframework.bootstrap.autoconfigure.jdbc;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.PreDestroy;
|
|
|
|
import javax.sql.DataSource;
|
|
|
|
import javax.sql.DataSource;
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
|
|
|
|
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
|
|
|
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
|
|
|
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
|
|
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
|
|
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
|
|
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
|
|
|
import org.springframework.util.ClassUtils;
|
|
|
|
import org.springframework.util.ClassUtils;
|
|
|
@ -36,25 +38,42 @@ import org.springframework.util.ClassUtils;
|
|
|
|
@Configuration
|
|
|
|
@Configuration
|
|
|
|
public class EmbeddedDatabaseConfiguration {
|
|
|
|
public class EmbeddedDatabaseConfiguration {
|
|
|
|
|
|
|
|
|
|
|
|
private static final Map<EmbeddedDatabaseType, String> EMBEDDED_DATABASE_TYPE_CLASSES;
|
|
|
|
|
|
|
|
private static final Map<EmbeddedDatabaseType, String> EMBEDDED_DATABASE_DRIVER_CLASSES;
|
|
|
|
private static final Map<EmbeddedDatabaseType, String> EMBEDDED_DATABASE_DRIVER_CLASSES;
|
|
|
|
private static final Map<EmbeddedDatabaseType, String> EMBEDDED_DATABASE_URLS;
|
|
|
|
private static final Map<EmbeddedDatabaseType, String> EMBEDDED_DATABASE_URLS;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private EmbeddedDatabase database;
|
|
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
static {
|
|
|
|
EMBEDDED_DATABASE_TYPE_CLASSES = new LinkedHashMap<EmbeddedDatabaseType, String>();
|
|
|
|
|
|
|
|
EMBEDDED_DATABASE_TYPE_CLASSES.put(EmbeddedDatabaseType.HSQL,
|
|
|
|
|
|
|
|
"org.hsqldb.Database");
|
|
|
|
|
|
|
|
EMBEDDED_DATABASE_DRIVER_CLASSES = new LinkedHashMap<EmbeddedDatabaseType, String>();
|
|
|
|
EMBEDDED_DATABASE_DRIVER_CLASSES = new LinkedHashMap<EmbeddedDatabaseType, String>();
|
|
|
|
|
|
|
|
EMBEDDED_DATABASE_DRIVER_CLASSES.put(EmbeddedDatabaseType.H2, "org.h2.Driver");
|
|
|
|
|
|
|
|
EMBEDDED_DATABASE_DRIVER_CLASSES.put(EmbeddedDatabaseType.DERBY,
|
|
|
|
|
|
|
|
"org.apache.derby.jdbc.EmbeddedDriver");
|
|
|
|
EMBEDDED_DATABASE_DRIVER_CLASSES.put(EmbeddedDatabaseType.HSQL,
|
|
|
|
EMBEDDED_DATABASE_DRIVER_CLASSES.put(EmbeddedDatabaseType.HSQL,
|
|
|
|
"org.hsqldb.jdbcDriver");
|
|
|
|
"org.hsqldb.jdbcDriver");
|
|
|
|
|
|
|
|
|
|
|
|
EMBEDDED_DATABASE_URLS = new LinkedHashMap<EmbeddedDatabaseType, String>();
|
|
|
|
EMBEDDED_DATABASE_URLS = new LinkedHashMap<EmbeddedDatabaseType, String>();
|
|
|
|
|
|
|
|
EMBEDDED_DATABASE_URLS.put(EmbeddedDatabaseType.H2,
|
|
|
|
|
|
|
|
"jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1");
|
|
|
|
|
|
|
|
EMBEDDED_DATABASE_URLS.put(EmbeddedDatabaseType.DERBY,
|
|
|
|
|
|
|
|
"jdbc:derby:memory:testdb;create=true");
|
|
|
|
EMBEDDED_DATABASE_URLS.put(EmbeddedDatabaseType.HSQL, "jdbc:hsqldb:mem:testdb");
|
|
|
|
EMBEDDED_DATABASE_URLS.put(EmbeddedDatabaseType.HSQL, "jdbc:hsqldb:mem:testdb");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
|
@Bean
|
|
|
|
public DataSource dataSource() {
|
|
|
|
public DataSource dataSource() {
|
|
|
|
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
|
|
|
|
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
|
|
|
|
.setType(getEmbeddedDatabaseType());
|
|
|
|
.setType(getEmbeddedDatabaseType());
|
|
|
|
return builder.build();
|
|
|
|
this.database = builder.build();
|
|
|
|
|
|
|
|
return this.database;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@PreDestroy
|
|
|
|
|
|
|
|
public void close() {
|
|
|
|
|
|
|
|
if (this.database != null) {
|
|
|
|
|
|
|
|
this.database.shutdown();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String getEmbeddedDatabaseDriverClass(
|
|
|
|
public static String getEmbeddedDatabaseDriverClass(
|
|
|
@ -67,7 +86,7 @@ public class EmbeddedDatabaseConfiguration {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static EmbeddedDatabaseType getEmbeddedDatabaseType() {
|
|
|
|
public static EmbeddedDatabaseType getEmbeddedDatabaseType() {
|
|
|
|
for (Map.Entry<EmbeddedDatabaseType, String> entry : EMBEDDED_DATABASE_TYPE_CLASSES
|
|
|
|
for (Map.Entry<EmbeddedDatabaseType, String> entry : EMBEDDED_DATABASE_DRIVER_CLASSES
|
|
|
|
.entrySet()) {
|
|
|
|
.entrySet()) {
|
|
|
|
if (ClassUtils.isPresent(entry.getValue(),
|
|
|
|
if (ClassUtils.isPresent(entry.getValue(),
|
|
|
|
EmbeddedDatabaseConfiguration.class.getClassLoader())) {
|
|
|
|
EmbeddedDatabaseConfiguration.class.getClassLoader())) {
|
|
|
|