我不确定您所说的 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()。