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

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

Introduction

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

Prototype

public boolean isEnforceInitMethod() 

Source Link

Document

Indicate whether the configured init method is the default.

Usage

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

/**
 * Invoke the specified custom init method on the given bean.
 * Called by invokeInitMethods./* w  w  w  .  ja v  a  2  s .  com*/
 * <p>Can be overridden in subclasses for custom resolution of init
 * methods with arguments.
 * @see #invokeInitMethods
 */
protected void invokeCustomInitMethod(String beanName, final Object bean, RootBeanDefinition mbd)
        throws Throwable {

    String initMethodName = mbd.getInitMethodName();
    Assert.state(initMethodName != null, "No init method set");
    final Method initMethod = (mbd.isNonPublicAccessAllowed()
            ? BeanUtils.findMethod(bean.getClass(), initMethodName)
            : ClassUtils.getMethodIfAvailable(bean.getClass(), initMethodName));

    if (initMethod == null) {
        if (mbd.isEnforceInitMethod()) {
            throw new BeanDefinitionValidationException("Couldn't find an init method named '" + initMethodName
                    + "' on bean with name '" + beanName + "'");
        } else {
            if (logger.isTraceEnabled()) {
                logger.trace("No default init method named '" + initMethodName + "' found on bean with name '"
                        + beanName + "'");
            }
            // Ignore non-existent default lifecycle methods.
            return;
        }
    }

    if (logger.isTraceEnabled()) {
        logger.trace("Invoking init method  '" + initMethodName + "' on bean with name '" + beanName + "'");
    }

    if (System.getSecurityManager() != null) {
        AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
            ReflectionUtils.makeAccessible(initMethod);
            return null;
        });
        try {
            AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> initMethod.invoke(bean),
                    getAccessControlContext());
        } catch (PrivilegedActionException pae) {
            InvocationTargetException ex = (InvocationTargetException) pae.getException();
            throw ex.getTargetException();
        }
    } else {
        try {
            ReflectionUtils.makeAccessible(initMethod);
            initMethod.invoke(bean);
        } catch (InvocationTargetException ex) {
            throw ex.getTargetException();
        }
    }
}