后置bean工厂处理器 BeanFactoryPostProcessor 理解

spring初始化bean时对外暴露的扩展点有很多,比如BeanPostProcessor和BeanFactoryPostProcessor,它在spring容器加载了bean的定义文件之后,在bean实例化之前执行的。也就是说,Spring允许BeanFactoryPostProcessor在容器创建bean之前读取bean配置元数据,并可进行修改。例如增加bean的属性和值,重新设置bean是否作为自动装配的侯选者,重设bean的依赖项等等。
/*
 * Copyright 2002-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.beans.factory.config;

import org.springframework.beans.BeansException;

/**
 * Allows for custom(定制的) modification of an application context's bean definitions,
 * adapting the bean property(属性) values of the context's underlying(底层) bean factory.
 *允许对应用上下文中bean定义做定制的修改,来适应bean的合适的上下文的底层bean工厂。
 * 

Application contexts can auto-detect BeanFactoryPostProcessor beans in * their bean definitions and apply them before any other beans get created. *应用上下文可以自动检测到BeanFactoryPostProcessor 的beans在它们的bean定义中并且会将在任何其他bean创建之前应用它(BeanFactoryPostProcessor )。 *

Useful for custom config files targeted at system administrators that * override bean properties configured in the application context. * *

See PropertyResourceConfigurer and its concrete implementations * for out-of-the-box solutions that address such configuration needs. * *

A BeanFactoryPostProcessor may interact(相互作用) with and modify bean * definitions, but never bean instances. Doing so may cause premature(过早) bean * instantiation, violating(违反) the container and causing unintended(意外的) side-effects. * If bean instance interaction is required, consider implementing * {@link BeanPostProcessor} instead. * * @author Juergen Hoeller * @since 06.07.2003 * @see BeanPostProcessor * @see PropertyResourceConfigurer */ public interface BeanFactoryPostProcessor { /** * Modify the application context's internal bean factory after its standard * initialization. All bean definitions will have been loaded, but no beans * will have been instantiated yet. This allows for overriding or adding * properties even to eager-initializing beans. * @param beanFactory the bean factory used by the application context * @throws org.springframework.beans.BeansException in case of errors */ //在bean标准初始化后,修改应用上下文的内部bean工厂。所有的bean定义都将被加载到这里。但是直到此时no beans已经完成初始化。这个方法允许overriding或者添加properties甚至紧急初始化beans。 //这个方法的参数是ConfigurableListableBeanFactory ,它可以获取bean工厂,而工厂是可以根据bean的name或者class获取指定的bean的; void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException; }

你可能感兴趣的:(Spring)