问题
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