001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.beans.factory.config;
018
019import org.springframework.beans.BeansException;
020
021/**
022 * Subinterface of {@link BeanPostProcessor} that adds a before-destruction callback.
023 *
024 * <p>The typical usage will be to invoke custom destruction callbacks on
025 * specific bean types, matching corresponding initialization callbacks.
026 *
027 * @author Juergen Hoeller
028 * @since 1.0.1
029 */
030public interface DestructionAwareBeanPostProcessor extends BeanPostProcessor {
031
032        /**
033         * Apply this BeanPostProcessor to the given bean instance before its
034         * destruction, e.g. invoking custom destruction callbacks.
035         * <p>Like DisposableBean's {@code destroy} and a custom destroy method, this
036         * callback will only apply to beans which the container fully manages the
037         * lifecycle for. This is usually the case for singletons and scoped beans.
038         * @param bean the bean instance to be destroyed
039         * @param beanName the name of the bean
040         * @throws org.springframework.beans.BeansException in case of errors
041         * @see org.springframework.beans.factory.DisposableBean#destroy()
042         * @see org.springframework.beans.factory.support.AbstractBeanDefinition#setDestroyMethodName(String)
043         */
044        void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException;
045
046        /**
047         * Determine whether the given bean instance requires destruction by this
048         * post-processor.
049         * <p><b>NOTE:</b> Even as a late addition, this method has been introduced on
050         * {@code DestructionAwareBeanPostProcessor} itself instead of on a SmartDABPP
051         * subinterface. This allows existing {@code DestructionAwareBeanPostProcessor}
052         * implementations to easily provide {@code requiresDestruction} logic while
053         * retaining compatibility with Spring <4.3, and it is also an easier onramp to
054         * declaring {@code requiresDestruction} as a Java 8 default method in Spring 5.
055         * <p>If an implementation of {@code DestructionAwareBeanPostProcessor} does
056         * not provide a concrete implementation of this method, Spring's invocation
057         * mechanism silently assumes a method returning {@code true} (the effective
058         * default before 4.3, and the to-be-default in the Java 8 method in Spring 5).
059         * @param bean the bean instance to check
060         * @return {@code true} if {@link #postProcessBeforeDestruction} is supposed to
061         * be called for this bean instance eventually, or {@code false} if not needed
062         * @since 4.3
063         */
064        boolean requiresDestruction(Object bean);
065
066}