繁体   English   中英

使用Spring,如何在初始化HIbernate之后但在应用程序启动之前执行代码?

[英]With Spring how can I execute code after HIbernate is initialized but before the application starts?

如果应用程序启动时带有一些空的数据库表,我想使用应用程序API将一些数据插入数据库。 我该怎么办?

我正在使用Spring 3.1,Hibernate 4.1.1。

[编辑]

感谢AlexR,答案是sublcass ContextLoaderListener ,在contextInitialized调用super,然后执行您需要做的所有事情:

public class MyContextLoaderListener extends ContextLoaderListener {

    public void contextInitialized(final ServletContextEvent event) {

        super.contextInitialized(event);
        // ... doStuff();
    }

}

您可能还需要在web.xml中而不是Spring中进行连接:

<listener>
    <listener-class>com.example.MyContextLoaderListener</listener-class>
</listener>

暂无
暂无

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

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