2016-08-22 44 views
1

我的简单域类:地图的构造不会对应用程序的工作摧毁

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 

在应用sh上调用destroy时出现此问题utdown,输出为:

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

出现这种情况仅适用于域类,其他常规类的构造函数的作品,因为它应该。如果我使用setter来设置属性,它就会起作用。

它一般针对使用域类地图的构造,并从destroy()

设置称为码危险:

groovy 2.4.4 
hibernate4 4.3.6.1 
jetty 8.1.9 

回答

0

你为什么不使用Grails的引导方法init和destroy。

def init = { 
    //init code here 
} 

def destroy = { 
//destroy code here 
} 

在销毁方法,你可以执行任何action.Also,如果你想从摧毁创建一些服务,然后调用到seggregate你的代码。

+0

destroy方法被执行,所以我不需要从bootstrap调用它。 DisposableBean的'destroy()'回调方法更加可读,是Spring实现正常关闭的标准方式。问题是Groovy的Map构造函数在销毁后不起作用。 –