List of usage examples for org.springframework.beans.factory DisposableBean DisposableBean
DisposableBean
From source file:org.springframework.beans.factory.support.AbstractBeanFactory.java
/** * Add the given bean to the list of disposable beans in this factory, * registering its DisposableBean interface and/or the given destroy method * to be called on factory shutdown (if applicable). Only applies to singletons. * <p>Also registers bean as dependent on other beans, according to the * "depends-on" configuration in the bean definition. * @param beanName the name of the bean/*from w w w . j av a2 s . co m*/ * @param bean the bean instance * @param mergedBeanDefinition the bean definition for the bean * @see RootBeanDefinition#isSingleton * @see RootBeanDefinition#getDependsOn * @see #registerDisposableBean * @see #registerDependentBean */ protected void registerDisposableBeanIfNecessary(final String beanName, final Object bean, final RootBeanDefinition mergedBeanDefinition) { if (mergedBeanDefinition.isSingleton()) { final boolean isDisposableBean = (bean instanceof DisposableBean); final boolean hasDestroyMethod = (mergedBeanDefinition.getDestroyMethodName() != null); if (isDisposableBean || hasDestroyMethod || hasDestructionAwareBeanPostProcessors()) { // Determine unique key for registration of disposable bean int counter = 1; String id = beanName; synchronized (this.disposableBeans) { while (this.disposableBeans.containsKey(id)) { counter++; id = beanName + "#" + counter; } } // Register a DisposableBean implementation that performs all destruction // work for the given bean: DestructionAwareBeanPostProcessors, // DisposableBean interface, custom destroy method. registerDisposableBean(id, new DisposableBean() { public void destroy() throws Exception { if (hasDestructionAwareBeanPostProcessors()) { if (logger.isDebugEnabled()) { logger.debug("Applying DestructionAwareBeanPostProcessors to bean with name '" + beanName + "'"); } for (int i = getBeanPostProcessors().size() - 1; i >= 0; i--) { Object beanProcessor = getBeanPostProcessors().get(i); if (beanProcessor instanceof DestructionAwareBeanPostProcessor) { ((DestructionAwareBeanPostProcessor) beanProcessor) .postProcessBeforeDestruction(bean, beanName); } } } if (isDisposableBean) { if (logger.isDebugEnabled()) { logger.debug("Invoking destroy() on bean with name '" + beanName + "'"); } ((DisposableBean) bean).destroy(); } if (hasDestroyMethod) { if (logger.isDebugEnabled()) { logger.debug("Invoking custom destroy method on bean with name '" + beanName + "'"); } invokeCustomDestroyMethod(beanName, bean, mergedBeanDefinition.getDestroyMethodName(), mergedBeanDefinition.isEnforceDestroyMethod()); } } }); } // Register bean as dependent on other beans, if necessary, // for correct shutdown order. String[] dependsOn = mergedBeanDefinition.getDependsOn(); if (dependsOn != null) { for (int i = 0; i < dependsOn.length; i++) { registerDependentBean(dependsOn[i], beanName); } } } }