001/* 002 * Copyright 2002-2015 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 * Factory hook that allows for custom modification of new bean instances, 023 * e.g. checking for marker interfaces or wrapping them with proxies. 024 * 025 * <p>ApplicationContexts can autodetect BeanPostProcessor beans in their 026 * bean definitions and apply them to any beans subsequently created. 027 * Plain bean factories allow for programmatic registration of post-processors, 028 * applying to all beans created through this factory. 029 * 030 * <p>Typically, post-processors that populate beans via marker interfaces 031 * or the like will implement {@link #postProcessBeforeInitialization}, 032 * while post-processors that wrap beans with proxies will normally 033 * implement {@link #postProcessAfterInitialization}. 034 * 035 * @author Juergen Hoeller 036 * @since 10.10.2003 037 * @see InstantiationAwareBeanPostProcessor 038 * @see DestructionAwareBeanPostProcessor 039 * @see ConfigurableBeanFactory#addBeanPostProcessor 040 * @see BeanFactoryPostProcessor 041 */ 042public interface BeanPostProcessor { 043 044 /** 045 * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean 046 * initialization callbacks (like InitializingBean's {@code afterPropertiesSet} 047 * or a custom init-method). The bean will already be populated with property values. 048 * The returned bean instance may be a wrapper around the original. 049 * @param bean the new bean instance 050 * @param beanName the name of the bean 051 * @return the bean instance to use, either the original or a wrapped one; 052 * if {@code null}, no subsequent BeanPostProcessors will be invoked 053 * @throws org.springframework.beans.BeansException in case of errors 054 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet 055 */ 056 Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; 057 058 /** 059 * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean 060 * initialization callbacks (like InitializingBean's {@code afterPropertiesSet} 061 * or a custom init-method). The bean will already be populated with property values. 062 * The returned bean instance may be a wrapper around the original. 063 * <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean 064 * instance and the objects created by the FactoryBean (as of Spring 2.0). The 065 * post-processor can decide whether to apply to either the FactoryBean or created 066 * objects or both through corresponding {@code bean instanceof FactoryBean} checks. 067 * <p>This callback will also be invoked after a short-circuiting triggered by a 068 * {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method, 069 * in contrast to all other BeanPostProcessor callbacks. 070 * @param bean the new bean instance 071 * @param beanName the name of the bean 072 * @return the bean instance to use, either the original or a wrapped one; 073 * if {@code null}, no subsequent BeanPostProcessors will be invoked 074 * @throws org.springframework.beans.BeansException in case of errors 075 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet 076 * @see org.springframework.beans.factory.FactoryBean 077 */ 078 Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException; 079 080}