春季手动销毁bean而不是ctx.close() [英] spring destroy bean manually instead of ctx.close()

查看:73
本文介绍了春季手动销毁bean而不是ctx.close()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以告诉spring的容器销毁特定的bean(原型)以节省内存?

is it possible to tell the container of spring to destroy a specific bean (prototype) in order to save up memory?

我不想使用ctx.close()来查看此bean被销毁,而是在运行时.

I dont want to user ctx.close() in order to see this bean destroyed, but rather on runtime.

推荐答案

如果我正确理解了您想删除运行时在ctx中定义的bean,我将使用BeanDefinitionRegistry完成此操作,BeanDefinitionRegistry有一个removeBean()方法,该方法以bean名称作为参数.

If I understand you correctly you want to remove bean that was defined in the ctx at runtime, will I use BeanDefinitionRegistry to get this done, BeanDefinitionRegistry has a method of removeBean() which takes the bean name as the parameter.

BeanDefinitionRegistry factory = 
   (BeanDefinitionRegistry) applicationCtx.getAutowireCapableBeanFactory();

然后

((DefaultListableBeanFactory) beanFactory).destroySingleton("myBean");

编辑

参考 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆