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;
018
019/**
020 * Interface to be implemented by beans that want to release resources on destruction.
021 * A {@link BeanFactory} will invoke the destroy method on individual destruction of a
022 * scoped bean. An {@link org.springframework.context.ApplicationContext} is supposed
023 * to dispose all of its singletons on shutdown, driven by the application lifecycle.
024 *
025 * <p>A Spring-managed bean may also implement Java's {@link AutoCloseable} interface
026 * for the same purpose. An alternative to implementing an interface is specifying a
027 * custom destroy method, for example in an XML bean definition. For a list of all
028 * bean lifecycle methods, see the {@link BeanFactory BeanFactory javadocs}.
029 *
030 * @author Juergen Hoeller
031 * @since 12.08.2003
032 * @see InitializingBean
033 * @see org.springframework.beans.factory.support.RootBeanDefinition#getDestroyMethodName()
034 * @see org.springframework.beans.factory.config.ConfigurableBeanFactory#destroySingletons()
035 * @see org.springframework.context.ConfigurableApplicationContext#close()
036 */
037public interface DisposableBean {
038
039        /**
040         * Invoked by the containing {@code BeanFactory} on destruction of a bean.
041         * @throws Exception in case of shutdown errors. Exceptions will get logged
042         * but not rethrown to allow other beans to release their resources as well.
043         */
044        void destroy() throws Exception;
045
046}