List of usage examples for org.springframework.beans.factory.support RootBeanDefinition isEnforceDestroyMethod
public boolean isEnforceDestroyMethod()
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 ww . j a va2s . c om*/ * @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); } } } }
From source file:org.springframework.beans.factory.support.DisposableBeanAdapter.java
/** * Create a new DisposableBeanAdapter for the given bean. * @param bean the bean instance (never {@code null}) * @param beanName the name of the bean//from w w w. ja v a2 s . c o m * @param beanDefinition the merged bean definition * @param postProcessors the List of BeanPostProcessors * (potentially DestructionAwareBeanPostProcessor), if any */ public DisposableBeanAdapter(Object bean, String beanName, RootBeanDefinition beanDefinition, List<BeanPostProcessor> postProcessors, @Nullable AccessControlContext acc) { Assert.notNull(bean, "Disposable bean must not be null"); this.bean = bean; this.beanName = beanName; this.invokeDisposableBean = (this.bean instanceof DisposableBean && !beanDefinition.isExternallyManagedDestroyMethod("destroy")); this.nonPublicAccessAllowed = beanDefinition.isNonPublicAccessAllowed(); this.acc = acc; String destroyMethodName = inferDestroyMethodIfNecessary(bean, beanDefinition); if (destroyMethodName != null && !(this.invokeDisposableBean && "destroy".equals(destroyMethodName)) && !beanDefinition.isExternallyManagedDestroyMethod(destroyMethodName)) { this.destroyMethodName = destroyMethodName; this.destroyMethod = determineDestroyMethod(destroyMethodName); if (this.destroyMethod == null) { if (beanDefinition.isEnforceDestroyMethod()) { throw new BeanDefinitionValidationException("Couldn't find a destroy method named '" + destroyMethodName + "' on bean with name '" + beanName + "'"); } } else { Class<?>[] paramTypes = this.destroyMethod.getParameterTypes(); if (paramTypes.length > 1) { throw new BeanDefinitionValidationException("Method '" + destroyMethodName + "' of bean '" + beanName + "' has more than one parameter - not supported as destroy method"); } else if (paramTypes.length == 1 && boolean.class != paramTypes[0]) { throw new BeanDefinitionValidationException("Method '" + destroyMethodName + "' of bean '" + beanName + "' has a non-boolean parameter - not supported as destroy method"); } } } this.beanPostProcessors = filterPostProcessors(postProcessors, bean); }