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>The default implementation returns {@code true}. If a pre-5 implementation 050 * of {@code DestructionAwareBeanPostProcessor} does not provide a concrete 051 * implementation of this method, Spring silently assumes {@code true} as well. 052 * @param bean the bean instance to check 053 * @return {@code true} if {@link #postProcessBeforeDestruction} is supposed to 054 * be called for this bean instance eventually, or {@code false} if not needed 055 * @since 4.3 056 */ 057 default boolean requiresDestruction(Object bean) { 058 return true; 059 } 060 061}