0

我正在使用 Spring3.1(独立环境)

我通过实现通过模板连接到主题的 MessageListener 创建了 MDB。

这个 bean 范围是单例的。

有时我想处理那个 Listener Bean。当我说 dispose 时,我的意思是我希望 ioc 释放该资源并从容器中清除该 bean。(这最终将使该 bean 停止侦听消息并释放未使用的内存。)

  1. 我需要使用 getBean(..) 方法通过它的 id 检索这个 bean 来执行它的处理。我听说使用 getBean(..) 会导致内存泄漏。否则我该怎么做?

  2. 我应该为此目的使用单例范围还是原型?

4

1 回答 1

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()。

于 2012-07-15T13:18:23.213 回答