2016-08-22 43 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構造函數在銷燬後不起作用。 –