001/*
002 * Copyright 2002-2019 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;
020import org.springframework.lang.Nullable;
021
022/**
023 * Factory hook that allows for custom modification of new bean instances —
024 * for example, checking for marker interfaces or wrapping beans with proxies.
025 *
026 * <p>Typically, post-processors that populate beans via marker interfaces
027 * or the like will implement {@link #postProcessBeforeInitialization},
028 * while post-processors that wrap beans with proxies will normally
029 * implement {@link #postProcessAfterInitialization}.
030 *
031 * <h3>Registration</h3>
032 * <p>An {@code ApplicationContext} can autodetect {@code BeanPostProcessor} beans
033 * in its bean definitions and apply those post-processors to any beans subsequently
034 * created. A plain {@code BeanFactory} allows for programmatic registration of
035 * post-processors, applying them to all beans created through the bean factory.
036 *
037 * <h3>Ordering</h3>
038 * <p>{@code BeanPostProcessor} beans that are autodetected in an
039 * {@code ApplicationContext} will be ordered according to
040 * {@link org.springframework.core.PriorityOrdered} and
041 * {@link org.springframework.core.Ordered} semantics. In contrast,
042 * {@code BeanPostProcessor} beans that are registered programmatically with a
043 * {@code BeanFactory} will be applied in the order of registration; any ordering
044 * semantics expressed through implementing the
045 * {@code PriorityOrdered} or {@code Ordered} interface will be ignored for
046 * programmatically registered post-processors. Furthermore, the
047 * {@link org.springframework.core.annotation.Order @Order} annotation is not
048 * taken into account for {@code BeanPostProcessor} beans.
049 *
050 * @author Juergen Hoeller
051 * @author Sam Brannen
052 * @since 10.10.2003
053 * @see InstantiationAwareBeanPostProcessor
054 * @see DestructionAwareBeanPostProcessor
055 * @see ConfigurableBeanFactory#addBeanPostProcessor
056 * @see BeanFactoryPostProcessor
057 */
058public interface BeanPostProcessor {
059
060        /**
061         * Apply this {@code BeanPostProcessor} to the given new bean instance <i>before</i> any bean
062         * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
063         * or a custom init-method). The bean will already be populated with property values.
064         * The returned bean instance may be a wrapper around the original.
065         * <p>The default implementation returns the given {@code bean} as-is.
066         * @param bean the new bean instance
067         * @param beanName the name of the bean
068         * @return the bean instance to use, either the original or a wrapped one;
069         * if {@code null}, no subsequent BeanPostProcessors will be invoked
070         * @throws org.springframework.beans.BeansException in case of errors
071         * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
072         */
073        @Nullable
074        default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
075                return bean;
076        }
077
078        /**
079         * Apply this {@code BeanPostProcessor} to the given new bean instance <i>after</i> any bean
080         * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
081         * or a custom init-method). The bean will already be populated with property values.
082         * The returned bean instance may be a wrapper around the original.
083         * <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
084         * instance and the objects created by the FactoryBean (as of Spring 2.0). The
085         * post-processor can decide whether to apply to either the FactoryBean or created
086         * objects or both through corresponding {@code bean instanceof FactoryBean} checks.
087         * <p>This callback will also be invoked after a short-circuiting triggered by a
088         * {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
089         * in contrast to all other {@code BeanPostProcessor} callbacks.
090         * <p>The default implementation returns the given {@code bean} as-is.
091         * @param bean the new bean instance
092         * @param beanName the name of the bean
093         * @return the bean instance to use, either the original or a wrapped one;
094         * if {@code null}, no subsequent BeanPostProcessors will be invoked
095         * @throws org.springframework.beans.BeansException in case of errors
096         * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
097         * @see org.springframework.beans.factory.FactoryBean
098         */
099        @Nullable
100        default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
101                return bean;
102        }
103
104}