在Spring 3中注册关闭钩子的正确方法是什么?

chr*_*leu 5 spring neo4j

有关如何在Java应用程序中嵌入Neo4j的教程建议注册一个关闭钩子,如下所示:

Runtime.getRuntime().addShutdownHook( new Thread() {
    // do shutdown work here
});
Run Code Online (Sandbox Code Playgroud)

我想知道放置此代码的最佳位置 - 或者实际上在Spring启动时需要运行一次的任何代码.这只是一个用init方法注册bean并将代码放入其中的情况吗?

我有兴趣知道这一点,更具体地说,当他们在Spring应用程序中使用嵌入式Neo4j时,其他人如何注册了一个关闭钩子.

Mic*_*ger 8

只需使用正确的方法声明graphdatabase-service的bean destroy-method:

<bean id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase"
        destroy-method="shutdown">
    <constructor-arg index="0" value="data/testdb.db"/>
    <constructor-arg index="1">
        <map>
            <entry key="allow_store_upgrade" value="true"/>
        </map>
    </constructor-arg>
</bean>
Run Code Online (Sandbox Code Playgroud)