简体   繁体   English

在Spring应用程序启动期间填充数据源

[英]Populate a datasource during the start of the spring application

I want to run some part of the code to populate the database with dummy data every time the server starts. 我想运行部分代码,以便在每次服务器启动时用虚拟数据填充数据库。 I use Tomcat as my servlet container. 我将Tomcat用作servlet容器。 My application is created using Spring. 我的应用程序是使用Spring创建的。 Is there a hook where I can run my code to populate the db just after my application is started? 在应用程序启动后,是否有一个钩子可以用来运行代码来填充数据库?

You have two different alternatives. 您有两种不同的选择。

The first one is using Spring's DataSourceInitializer . 第一个是使用Spring的DataSourceInitializer You can pass your initialisation query as a parameter and it executes that query. 您可以将初始化查询作为参数传递,并执行该查询。 You can execute any command that you like. 您可以执行所需的任何命令。

Example: 例:

<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>

The second alternative is implementing a Spring ApplicationListener . 第二种选择是实现Spring ApplicationListener Populate each datum from that listener to your database manually. 手动将每个数据从该侦听器填充到数据库中。 It is a little harder to achieve. 这很难实现。

Example: 例:

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(...)
            // ...
        }
    }
}

This bean must be initialised by Spring. 该bean必须在Spring之前初始化。 You can either define it in your applicationContext.xml or in your configuration class. 您可以在applicationContext.xml或配置类中定义它。

By the way, you can always listen for your ServletContext status by using a ServletContextListener . 顺便说一句,您始终可以使用ServletContextListener侦听ServletContext状态。 But if you are using Spring, there are easier methods. 但是,如果您使用的是Spring,则有更简便的方法。

You can use Liquibase , and if you're using Spring Boot all you have to do is add liquibase-core to your classpath, via maven or whatever build tool you're using is. 您可以使用Liquibase ,如果您使用的是Spring Boot,您所要做的就是通过maven或您使用的任何构建工具将liquibase-core添加到类路径中。 Spring Boot uses YAML files by default. Spring Boot默认使用YAML文件。 Spring Boot will then run Liquibase on every application startup. 然后,Spring Boot将在每次应用程序启动时运行Liquibase。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM