类 EmbeddedDatabaseFactory
- java.lang.Object
- org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory
- 直接已知子类:
EmbeddedDatabaseFactoryBean
public class EmbeddedDatabaseFactory extends Object
Factory for creating anEmbeddedDatabase
instance.Callers are guaranteed that the returned database has been fully initialized and populated.
The factory can be configured as follows:
- Call
generateUniqueDatabaseName
to set a unique, random name for the database. - Call
setDatabaseName(java.lang.String)
to set an explicit name for the database. - Call
setDatabaseType(org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType)
to set the database type if you wish to use one of the supported types. - Call
setDatabaseConfigurer(org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseConfigurer)
to configure support for a custom embedded database type. - Call
setDatabasePopulator(org.springframework.jdbc.datasource.init.DatabasePopulator)
to change the algorithm used to populate the database. - Call
setDataSourceFactory(org.springframework.jdbc.datasource.embedded.DataSourceFactory)
to change the type ofDataSource
used to connect to the database.
After configuring the factory, call
getDatabase()
to obtain a reference to theEmbeddedDatabase
instance.- 从以下版本开始:
- 3.0
- 作者:
- Keith Donald, Juergen Hoeller, Sam Brannen
字段概要
字段 修饰符和类型 字段 说明 static String
DEFAULT_DATABASE_NAME
Default name for an embedded database: "testdb"
构造器概要
构造器 构造器 说明 EmbeddedDatabaseFactory()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 EmbeddedDatabase
getDatabase()
Factory method that returns the embedded database instance, which is also aDataSource
.protected DataSource
getDataSource()
Hook that gets theDataSource
that provides the connectivity to the embedded database.protected void
initDatabase()
Hook to initialize the embedded database.void
setDatabaseConfigurer(EmbeddedDatabaseConfigurer configurer)
Set the strategy that will be used to configure the embedded database instance.void
setDatabaseName(String databaseName)
Set the name of the database.void
setDatabasePopulator(DatabasePopulator populator)
Set the strategy that will be used to initialize or populate the embedded database.void
setDatabaseType(EmbeddedDatabaseType type)
Set the type of embedded database to use.void
setDataSourceFactory(DataSourceFactory dataSourceFactory)
Set the factory to use to create theDataSource
instance that connects to the embedded database.void
setGenerateUniqueDatabaseName(boolean generateUniqueDatabaseName)
Set thegenerateUniqueDatabaseName
flag to enable or disable generation of a pseudo-random unique ID to be used as the database name.protected void
shutdownDatabase()
Hook to shutdown the embedded database.
字段详细资料
DEFAULT_DATABASE_NAME
public static final String DEFAULT_DATABASE_NAME
Default name for an embedded database: "testdb"- 另请参阅:
- 常量字段值
构造器详细资料
EmbeddedDatabaseFactory
public EmbeddedDatabaseFactory()
方法详细资料
setGenerateUniqueDatabaseName
public void setGenerateUniqueDatabaseName(boolean generateUniqueDatabaseName)
Set thegenerateUniqueDatabaseName
flag to enable or disable generation of a pseudo-random unique ID to be used as the database name.Setting this flag to
true
overrides any explicit name set viasetDatabaseName(java.lang.String)
.- 从以下版本开始:
- 4.2
- 另请参阅:
setDatabaseName(java.lang.String)
setDatabaseName
public void setDatabaseName(String databaseName)
Set the name of the database.Defaults to "testdb".
Will be overridden if the
generateUniqueDatabaseName
flag has been set totrue
.- 参数:
databaseName
- name of the embedded database- 另请参阅:
setGenerateUniqueDatabaseName(boolean)
setDataSourceFactory
public void setDataSourceFactory(DataSourceFactory dataSourceFactory)
Set the factory to use to create theDataSource
instance that connects to the embedded database.Defaults to
SimpleDriverDataSourceFactory
.
setDatabaseType
public void setDatabaseType(EmbeddedDatabaseType type)
Set the type of embedded database to use.Call this when you wish to configure one of the pre-supported types.
Defaults to HSQL.
- 参数:
type
- the database type
setDatabaseConfigurer
public void setDatabaseConfigurer(EmbeddedDatabaseConfigurer configurer)
Set the strategy that will be used to configure the embedded database instance.Call this when you wish to use an embedded database type not already supported.
setDatabasePopulator
public void setDatabasePopulator(DatabasePopulator populator)
Set the strategy that will be used to initialize or populate the embedded database.Defaults to
null
.
getDatabase
public EmbeddedDatabase getDatabase()
Factory method that returns the embedded database instance, which is also aDataSource
.
initDatabase
protected void initDatabase()
Hook to initialize the embedded database.If the
generateUniqueDatabaseName
flag has been set totrue
, the current value of the database name will be overridden with an auto-generated name.Subclasses may call this method to force initialization; however, this method should only be invoked once.
After calling this method,
getDataSource()
returns theDataSource
providing connectivity to the database.
shutdownDatabase
protected void shutdownDatabase()
Hook to shutdown the embedded database. Subclasses may call this method to force shutdown.After calling,
getDataSource()
returnsnull
.Does nothing if no embedded database has been initialized.
getDataSource
protected final DataSource getDataSource()
Hook that gets theDataSource
that provides the connectivity to the embedded database.Returns
null
if theDataSource
has not been initialized or if the database has been shut down. Subclasses may call this method to access theDataSource
instance directly.