如何以编程方式处理/清理MessageListener bean

问题描述 投票:0回答:1

我正在使用Spring3.1(standalone env)

我已经通过实现MessageListener创建了MDB,该MessageListener通过模板连接到主题。

此bean范围是单例。

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

  1. 我需要使用getBean(..)方法通过其ID检索此bean以执行其处置。我听说使用getBean(..)可能会导致内存泄漏。我应该怎么做呢?

  2. 我应该为此使用Singleton Scope或原型吗?

java spring spring-integration imdb
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()。

© www.soinside.com 2019 - 2024. All rights reserved.