2012-07-15 75 views
0

我使用Spring3.1(獨立ENV)如何處置/潔淨消息監聽豆programmaticly

我已經通過通過模板連接到一個話題器具的MessageListener創建MDB。

這個bean作用域是單例。

有時候我想要處理Listener Bean。當我說處理我的意思是我希望國際奧委會來釋放資源並清理從容器這個bean。(這最終將使這個bean停止收聽消息和不含未使用的內存。)

  1. 我需要用getBean(..)方法通過它的id檢索這個bean來執行它的處理。我聽說使用getBean(..)會導致內存泄漏。我應該怎麼做呢?

  2. 我應該使用單例作用域還是原型作爲此目的?

感謝, 射線。

回答

0

我不知道你的意思通過getBean()方法和內存泄漏,但...

如果你想徹底刪除它,而不是隻是阻止它,你可以在自己的「孩子」其聲明應用上下文。將主要上下文作爲父項,以便它可以在主要上下文中引用bean(如有必要)。

/** 
* Create a new ClassPathXmlApplicationContext with the given parent, 
* loading the definitions from the given XML files and automatically 
* refreshing the context. 
* @param configLocations array of resource locations 
* @param parent the parent context 
* @throws BeansException if context creation failed 
*/ 
public ClassPathXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException { 
    this(configLocations, true, parent); 
} 

當你想刪除它;調用context.destroy()。

+0

那麼如果我需要50毫秒的生病需要創建50個上下文?沒有意義。 – rayman 2012-07-15 13:27:26

+0

我只是喜歡這樣做;保持清潔;使用子上下文允許您使用屬性佔位符配置bean並設置上下文的環境(在3.1中)。如果你不想這樣做,你可以使用AbstractBeanFactory factory =(AbstractBeanFactory)ctx.getBeanFactory(); \t \t factory.destroySingleton(「dmlc」); – 2012-07-15 17:12:05

+0

如果我使用這種方式(AbstractBeanFactory方式),性能/內存泄漏怎麼樣? – rayman 2012-07-16 07:11:25