the bean shutdown

Use @Bean annotations, and destroyMethod configuration when not, the default value is:

String destroyMethod() default AbstractBeanDefinition.INFER_METHOD;

 

public static final String INFER_METHOD = "(inferred)";

That is, when not configured destroyMethod, spring will be used to infer the destruction method, this method requires inferred meet:

1. public's

2. No argument

3. The method called close or shutdown

If you happen to have a bean when the above method, it will call upon the destruction. For example redis.clients.jedis.BinaryJedis and subclasses to meet the requirements, there is a shutdown method. But his shutdown by sending shutdown commands to redis-server, not destroy a connection. Therefore, when the Bean destroyed, in fact, you do not want to call the shutdown process.

If you want to prevent the destruction of the method call inferred destroyMethod you need to assign to "":

@Bean(destroyMethod = "")

 

Let's look at how the methods of destruction inference is in force.

First of all, when you create a bean (see org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory # doCreateBean method), it will be called:

        // Register bean as disposable.
        try {
            registerDisposableBeanIfNecessary(beanName, bean, mbd);
        }

 

Methods (requiresDestruction Lane) This method checks destruction, and registered DisposableBeanAdapter, DisposableBeanAdapter will end up calling the bean destroyMethod.

protected void registerDisposableBeanIfNecessary(String beanName, Object bean, RootBeanDefinition mbd) {
        AccessControlContext acc = (System.getSecurityManager() != null ? getAccessControlContext() : null);
        if (!mbd.isPrototype() && requiresDestruction(bean, mbd)) {
            if (mbd.isSingleton()) {
                // Register a DisposableBean implementation that performs all destruction
                // work for the given bean: DestructionAwareBeanPostProcessors,
                // DisposableBean interface, custom destroy method.
                registerDisposableBean(beanName,
                        new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc));
            }
            else {
                // A bean with a custom scope...
                Scope scope = this.scopes.get(mbd.getScope());
                if (scope == null) {
                    throw new IllegalStateException("No Scope registered for scope name '" + mbd.getScope() + "'");
                }
                scope.registerDestructionCallback(beanName,
                        new DisposableBeanAdapter(bean, beanName, mbd, getBeanPostProcessors(), acc));
            }
        }
    }

 

Other logic is obvious, the source code is not posted

 

Guess you like

Origin www.cnblogs.com/drizzlewithwind/p/10944074.html