在没有XML配置的情况下初始化数据库,但使用@Configuration [英] Initialize database without XML configuration, but using @Configuration

查看:114
本文介绍了在没有XML配置的情况下初始化数据库,但使用@Configuration的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在不创建XML文件的情况下初始化数据库。

I would like to know how to initialize a database without having to create an XML file.

我已经使用了这种初始化工作正常,但在我的当前案例我不想创建XML:

I already use this kind of initialization that works fine, but in my current case I don't want to create an XML:

<jdbc:initialize-database data-source="dataSource">
  <jdbc:script location="classpath:com/foo/sql/db-schema.sql"/>
  <jdbc:script location="classpath:com/foo/sql/db-test-data.sql"/>
</jdbc:initialize-database>

我知道我可以创建一个嵌入式数据库:

I know I can create an embedded database with:

EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder.setType(H2).addScript("my-schema.sql").addScript("my-test-data.sql").build();

在我的例子中,数据库和模式是使用Liquibase创建的。

In my case, the database and schema are created using Liquibase.

我只是想用Spring和我的自定义数据集初始化它,而不必每次只为此创建一个新的XML文件。

I just want to initialize it with Spring and with my customized dataset, without having to create a new XML file each time just for that.

是否有可能?

推荐答案

在查看与EmbeddedDatabaseBuilder相关的Spring类之后,我发现DatabaseBuilder正在使用一些看起来像这样的代码:

After looking at Spring classes related to EmbeddedDatabaseBuilder I found out that the DatabaseBuilder is using some code looking like this:

ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
for (String sqlScript: sqlInitializationScripts ) {
  Resource sqlScriptResource = RESOURCE_LOADER.getResource(sqlScript);
  populator.addScript(sqlScriptResource);
}
DatabasePopulatorUtils.execute(populator, dataSource);

这对我来说很好,即使它是@BeforeTest方法而不是Spring配置。

This will work fine for me, even if it will be on a @BeforeTest method and not on the Spring configuration.

这篇关于在没有XML配置的情况下初始化数据库,但使用@Configuration的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆