【问题标题】:Map constructor doesn't work on app destroy地图构造函数不适用于应用程序销毁
【发布时间】:2016-08-22 09:46:44
【问题描述】:

我的简单域类:

class TestDestroy {
    String x
}

还有一项服务:

class TestDestroyService implements DisposableBean {
    @Override
    void destroy() throws Exception {
        TestDestroy d = new TestDestroy(x: "x")
        println("Test destroy: ${d.x}")
    }
}

如果我从控制器调用destroy() 方法,一切都很好,我得到了输出:

Test destroy: x

在应用关闭时调用destroy时出现问题,输出为:

2016-08-22 11:20:14.186:INFO:t.1:Destroying Spring FrameworkServlet 'grails'
Test destroy: null
2016-08-22 11:20:14.487:INFO:t.1:Closing Spring root WebApplicationContext
2016-08-22 11:20:14.496:INFO:oejsh.ContextHandler:stopped o.e.j.w.WebAppContext{ ...

这仅发生在领域类中,对于其他 groovy 类,构造函数可以正常工作。如果我用 setter 设置属性,它就可以工作。

对于使用带有映射构造函数的域类并从 destroy() 调用的代码来说,这通常是危险的

设置:

groovy 2.4.4
hibernate4 4.3.6.1
jetty 8.1.9

【问题讨论】:

    标签: hibernate grails grails-orm


    【解决方案1】:

    为什么不使用 grails 引导方法 init 和 destroy。

    def init = {
      //init code here
    }
    
    def destroy = {
    //destroy code here
    }
    

    在destroy方法中,您可以执行任何操作。此外,如果您想将代码与destroy分离,请创建一些服务,然后调用它。

    【讨论】:

    • destroy方法被执行了,所以我不需要从bootstrap调用它。 DisposableBean 的destroy() 回调方法更易读,并且是spring 实现优雅关闭的标准方法。问题是groovy的Map构造函数在destroy后不起作用。
    猜你喜欢
    • 2014-10-20
    • 1970-01-01
    • 2022-09-23
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 2017-12-07
    • 2015-07-02
    相关资源
    最近更新 更多