Example usage for org.springframework.beans.factory.support AbstractBeanDefinition INFER_METHOD

List of usage examples for org.springframework.beans.factory.support AbstractBeanDefinition INFER_METHOD

Introduction

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

Prototype

String INFER_METHOD

To view the source code for org.springframework.beans.factory.support AbstractBeanDefinition INFER_METHOD.

Click Source Link

Document

Constant that indicates the container should attempt to infer the #setDestroyMethodName destroy method name for a bean as opposed to explicit specification of a method name.

Usage

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.
 *///from   w w  w. j a va 2 s.  c  om
@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 ww  w. j a  va2  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);
}