Example usage for org.springframework.beans.factory.support RootBeanDefinition getDestroyMethodName

List of usage examples for org.springframework.beans.factory.support RootBeanDefinition getDestroyMethodName

Introduction

In this page you can find the example usage for org.springframework.beans.factory.support RootBeanDefinition getDestroyMethodName.

Prototype

@Override
@Nullable
public String getDestroyMethodName() 

Source Link

Document

Return the name of the destroy method.

Usage

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. jav  a2  s.c o  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);
            }
        }
    }
}

From source file:org.springframework.beans.factory.support.DisposableBeanAdapter.java

/**
 * If the current value of the given beanDefinition's "destroyMethodName" property is
 * {@link AbstractBeanDefinition#INFER_METHOD}, then attempt to infer a destroy method.
 * Candidate methods are currently limited to public, no-arg methods named "close" or
 * "shutdown" (whether declared locally or inherited). The given BeanDefinition's
 * "destroyMethodName" is updated to be null if no such method is found, otherwise set
 * to the name of the inferred method. This constant serves as the default for the
 * {@code @Bean#destroyMethod} attribute and the value of the constant may also be
 * used in XML within the {@code <bean destroy-method="">} or {@code
 * <beans default-destroy-method="">} attributes.
 * <p>Also processes the {@link java.io.Closeable} and {@link java.lang.AutoCloseable}
 * interfaces, reflectively calling the "close" method on implementing beans as well.
 *//*w ww  .  java 2s .co  m*/
@Nullable
private String inferDestroyMethodIfNecessary(Object bean, RootBeanDefinition beanDefinition) {
    String destroyMethodName = beanDefinition.getDestroyMethodName();
    if (AbstractBeanDefinition.INFER_METHOD.equals(destroyMethodName)
            || (destroyMethodName == null && bean instanceof AutoCloseable)) {
        // Only perform destroy method inference or Closeable detection
        // in case of the bean not explicitly implementing DisposableBean
        if (!(bean instanceof DisposableBean)) {
            try {
                return bean.getClass().getMethod(CLOSE_METHOD_NAME).getName();
            } catch (NoSuchMethodException ex) {
                try {
                    return bean.getClass().getMethod(SHUTDOWN_METHOD_NAME).getName();
                } catch (NoSuchMethodException ex2) {
                    // no candidate destroy method found
                }
            }
        }
        return null;
    }
    return (StringUtils.hasLength(destroyMethodName) ? destroyMethodName : null);
}

From source file:org.springframework.beans.factory.support.DisposableBeanAdapter.java

/**
 * Check whether the given bean has any kind of destroy method to call.
 * @param bean the bean instance//from w w w .j  a v a  2 s  . co m
 * @param beanDefinition the corresponding bean definition
 */
public static boolean hasDestroyMethod(Object bean, RootBeanDefinition beanDefinition) {
    if (bean instanceof DisposableBean || bean instanceof AutoCloseable) {
        return true;
    }
    String destroyMethodName = beanDefinition.getDestroyMethodName();
    if (AbstractBeanDefinition.INFER_METHOD.equals(destroyMethodName)) {
        return (ClassUtils.hasMethod(bean.getClass(), CLOSE_METHOD_NAME)
                || ClassUtils.hasMethod(bean.getClass(), SHUTDOWN_METHOD_NAME));
    }
    return StringUtils.hasLength(destroyMethodName);
}