在 spring 应用程序启动期间填充数据源
Populate a datasource during the start of the spring application
我想 运行 代码的某些部分在每次服务器启动时用虚拟数据填充数据库。我使用 Tomcat 作为我的 servlet 容器。我的应用程序是使用 Spring 创建的。有没有一个钩子可以让我 运行 我的代码在我的应用程序启动后立即填充数据库?
你有两个不同的选择。
第一个是使用 Spring 的 DataSourceInitializer
。您可以将初始化查询作为参数传递并执行该查询。您可以执行任何您喜欢的命令。
示例:
<bean id="dbInitializer" class="org.springframework.jdbc.datasource.init.DataSourceInitializer">
<property name="dataSource" ref="myDataSourceRef"/>
<property name="enabled" value="true"/>
<property name="databasePopulator">
<bean class="org.springframework.jdbc.datasource.init.ResourceDatabasePopulator">
<property name="continueOnError" value="true"/>
<property name="ignoreFailedDrops" value="true"/>
<property name="sqlScriptEncoding" value="UTF-8"/>
<property name="scripts">
<array>
<value type="org.springframework.core.io.Resource">init.sql</value>
</array>
</property>
</bean>
</property>
</bean>
第二种选择是实施 Spring ApplicationListener
。手动将该侦听器中的每个数据填充到您的数据库中。有点难实现。
示例:
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
public class ApplicationListenerBean implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
ApplicationContext applicationContext = ((ContextRefreshedEvent) event).getApplicationContext();
// now you can do applicationContext.getBean(...)
// ...
}
}
}
此 bean 必须由 Spring 初始化。您可以在 applicationContext.xml
或配置 class.
中定义它
顺便说一句,您始终可以使用 ServletContextListener
来监听您的 ServletContext 状态。但是如果你使用Spring,还有更简单的方法。
您可以使用 Liquibase,如果您使用的是 Spring Boot,您所要做的就是通过 maven 或任何构建工具将 liquibase-core
添加到您的类路径中重新使用是。 Spring Boot 默认使用 YAML 文件。 Spring Boot 将在每次应用程序启动时 运行 Liquibase。
我想 运行 代码的某些部分在每次服务器启动时用虚拟数据填充数据库。我使用 Tomcat 作为我的 servlet 容器。我的应用程序是使用 Spring 创建的。有没有一个钩子可以让我 运行 我的代码在我的应用程序启动后立即填充数据库?
你有两个不同的选择。
第一个是使用 Spring 的 DataSourceInitializer
。您可以将初始化查询作为参数传递并执行该查询。您可以执行任何您喜欢的命令。
示例:
<bean id="dbInitializer" class="org.springframework.jdbc.datasource.init.DataSourceInitializer">
<property name="dataSource" ref="myDataSourceRef"/>
<property name="enabled" value="true"/>
<property name="databasePopulator">
<bean class="org.springframework.jdbc.datasource.init.ResourceDatabasePopulator">
<property name="continueOnError" value="true"/>
<property name="ignoreFailedDrops" value="true"/>
<property name="sqlScriptEncoding" value="UTF-8"/>
<property name="scripts">
<array>
<value type="org.springframework.core.io.Resource">init.sql</value>
</array>
</property>
</bean>
</property>
</bean>
第二种选择是实施 Spring ApplicationListener
。手动将该侦听器中的每个数据填充到您的数据库中。有点难实现。
示例:
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
public class ApplicationListenerBean implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent) {
ApplicationContext applicationContext = ((ContextRefreshedEvent) event).getApplicationContext();
// now you can do applicationContext.getBean(...)
// ...
}
}
}
此 bean 必须由 Spring 初始化。您可以在 applicationContext.xml
或配置 class.
顺便说一句,您始终可以使用 ServletContextListener
来监听您的 ServletContext 状态。但是如果你使用Spring,还有更简单的方法。
您可以使用 Liquibase,如果您使用的是 Spring Boot,您所要做的就是通过 maven 或任何构建工具将 liquibase-core
添加到您的类路径中重新使用是。 Spring Boot 默认使用 YAML 文件。 Spring Boot 将在每次应用程序启动时 运行 Liquibase。