Listener_Thing_List_Editor_Button Listener_Refresh=Application_Context.getBean(Listener_Thing_List_Editor_Button.class);
.......
請問如何在不關閉容器的情形下銷毀某個bean(例如 Listener_Refresh) ?
謝謝兩位關注,不過也許沒有把問題說清楚,兩位的建議無法解決問題。
我試再把問題詳述如下:
上述的listener 放在一個 button 上,button放在splitpane上
以下是關閉splitpane的代碼:
public Boolean Set_component_user_wants_to_cancel_all()
{
Boolean OK=true;
if(Sure_to_cancel)
{
this.removeAll(); //"this"=splitpane
Listener_Refresh=null;
}
else
{
...........
OK=false;
}
return OK;
}
以下是listener的代碼:
@org.springframework.stereotype.Component
@Scope("prototype")
public class Listener_Thing_List_Editor_Button implements ActionListener
{
private Mirror_World_Dispatcher Mirror_World_Dispatcher=null;
public Listener_Thing_List_Editor_Button()
{
}
public void setMirror_World_Dispatcher(Mirror_World_Dispatcher Mirror_World_Dispatcher)
{
this.Mirror_World_Dispatcher=Mirror_World_Dispatcher;
}
public void actionPerformed(ActionEvent e)
{
this.Mirror_World_Dispatcher.React_to_thing_list_editor_action_event(e,null);
}
}
問題是,如果我重複 “關閉splitpane,打開splitpane,按button”過程 n次,actionPerformed則會被執行n次。
所以我懷疑執行上述Set_component_user_wants_to_cancel_all()后,Listener_Refresh實例依然存在。
BeanDefinitionRegistry beanDefinitionRegistry = (DefaultListableBeanFactory) configurableContext.getBeanFactory();
Spring 提供了 Bean 工厂管理工具类,可以移除注册的 Bean ,方法是:
beanDefinitionRegistry.removeBeanDefinition(beanId);
这样可以讲对应的 Bean 移除容器的托管范围外。
防在局部变量等生命周期短的地方,设置为null或者用新的对象替换,老的对象自然就被回收了
对象使用完后 直接赋值为空 虚拟机检测到对象未被引用 会自动进入回收 慎用gc()