spring destroy bean manually instead of ctx.close()

二次信任 提交于 2019-12-25 05:46:12

问题


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

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


回答1:


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

Then

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

EDIT

reference http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/beans/factory/support/BeanDefinitionRegistry.html#removeBeanDefinition%28java.lang.String%29



来源:https://stackoverflow.com/questions/16667957/spring-destroy-bean-manually-instead-of-ctx-close

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!