2012-07-15 68 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