BeanFactoryPostProcessor是在bean实例化前,可以对其bean的操作,和BeanPostProcessor类似,可以对bean的定义(配置元数据)进行处理,并且可以配置多个BeanFactoryPostProcessor,可以通过设置“order”属性来控制BeanFactoryPostProcessor的执行次序(仅当BeanFactoryPostProcessor实现了Order接口时才可以设置此属性,因此在使用BeanFactoryPostProcessor的时候,就应该考虑实现Order接口)
如果想改变实际的bean实例,(例如从配置元数据创建的对象),最好使用BeanFactoryPostProcessor。其作用域范围是容器级的,只和你所使用的容器有关。如果在容器定义一个BeanFactoryPostProcessor,他仅仅对此容器的bean进行h后置处理。BeanFactoryPostProcessor不会对定义在另一个容器中的bean进行后置处理,即使这两个容器都是在同一个层次上。
以下是BeanFactoryPostProcessor的demo
package springSourseAnalyes; import java.util.List; import org.springframework.beans.BeansException; import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.PropertyValue; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinitionVisitor; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.TypedStringValue; import org.springframework.util.StringValueResolver; public class BeanFactoryPostProcessDemo implements BeanFactoryPostProcessor{ @Override public void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory) throws BeansException { /** * 可以在任何bean实例化之前都能获取bean 的信息,可以任意修改 */ String[] beanDefinitionNames = beanFactory.getBeanDefinitionNames(); for(String beanString:beanDefinitionNames){ BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanString); MutablePropertyValues propertyValues = beanDefinition.getPropertyValues(); //获取实例的属性,以及属性值 System.out.println("propertyValues--->"+propertyValues); StringValueResolver valueResolver = null; // if(propertyValues.contains("nameString")){//判断某个类如果有name属性的 // propertyValues.addPropertyValue("name","崔春驰");//修改属性值 // System.out.println("修改了属性值"); // } /** * 两种方式: 1、使用MutablePropertyValues属性类,直接修改指定的属性值,不需要指定对象BeanDefinitionVisitor * 2、使用StringValueResolver接口来将获取的属性值作修改 ,,并且需要指定BeanDefinitionVisitor */ if(!propertyValues.isEmpty() ){ //如果包含某个属性, List<PropertyValue> propertyValueList = propertyValues.getPropertyValueList(); System.out.println("属性:"+propertyValueList.toString()); for(PropertyValue propertyValue : propertyValueList){ System.out.println("propertyValue---->"+propertyValue.getName()); if("nameString".equals(propertyValue.getName())){ TypedStringValue value1 = (TypedStringValue) propertyValue.getValue(); String value = value1.getValue(); /** * 从源码AbstractAutowireCapableBeanFactory 的方法applyPropertyValues看的出填充数据的逻辑代码 */ //优先这种方式重写copy propertyValues.addPropertyValue("nameString", value != null ? value.substring(0, 1).concat("****"):""); //后执行这个, propertyValue.setConvertedValue("weqwew"); } } //获取属性值 valueResolver = new StringValueResolver() { @Override public String resolveStringValue(String strVal) { System.out.println("StringValueResolver接口------值:"+strVal); if("崔春驰".equals(strVal)) return strVal.substring(0,1).concat("&&&&&"); return strVal; } }; }else{ valueResolver = new StringValueResolver() { @Override public String resolveStringValue(String strVal) { // TODO Auto-generated method stub return strVal; } }; } BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver ); visitor.visitBeanDefinition(beanDefinition); } } }方法invokeBeanFactoryPostProcessors为激活BeanFactoryPostProcessor
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) { // Invoke BeanDefinitionRegistryPostProcessors first, if any. Set<String> processedBeans = new HashSet<String>(); //对BeanDefinitionRegistry类型的处理 if (beanFactory instanceof BeanDefinitionRegistry) { BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; List<BeanFactoryPostProcessor> regularPostProcessors = new LinkedList<BeanFactoryPostProcessor>(); List<BeanDefinitionRegistryPostProcessor> registryPostProcessors = new LinkedList<BeanDefinitionRegistryPostProcessor>(); //硬编码注册的后处理器 for (BeanFactoryPostProcessor postProcessor : getBeanFactoryPostProcessors()) { if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) { BeanDefinitionRegistryPostProcessor registryPostProcessor = (BeanDefinitionRegistryPostProcessor) postProcessor; //对于BeanDefinitionRegistryPostProcessor类型,在 //BeanFactoryPostProcessor的基础上还有自己定义的的方法,需要先调用 registryPostProcessor.postProcessBeanDefinitionRegistry(registry); registryPostProcessors.add(registryPostProcessor); } else { //记录常规BeanFactoryPostProcessor regularPostProcessors.add(postProcessor); } } //配置注册的后处理器 Map<String, BeanDefinitionRegistryPostProcessor> beanMap = beanFactory.getBeansOfType(BeanDefinitionRegistryPostProcessor.class, true, false); List<BeanDefinitionRegistryPostProcessor> registryPostProcessorBeans = new ArrayList<BeanDefinitionRegistryPostProcessor>(beanMap.values()); OrderComparator.sort(registryPostProcessorBeans); for (BeanDefinitionRegistryPostProcessor postProcessor : registryPostProcessorBeans) { //BeanDefinitionRegistryPostProcessor的特殊处理 postProcessor.postProcessBeanDefinitionRegistry(registry); } //激活postProcessorBeanFactory方法,之前激活的是 //postprocessorBeanfinitionRegistry,硬编码设置的 //BeanDefinitionRegistryPostProcessor invokeBeanFactoryPostProcessors(registryPostProcessors, beanFactory); //配置的BeanDefinitionRegistryPostProcessor invokeBeanFactoryPostProcessors(registryPostProcessorBeans, beanFactory); //常规BeanFactoryPostProcessor invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory); processedBeans.addAll(beanMap.keySet()); } else { // Invoke factory processors registered with the context instance. invokeBeanFactoryPostProcessors(getBeanFactoryPostProcessors(), beanFactory); } // Do not initialize FactoryBeans here: We need to leave all regular beans // uninitialized to let the bean factory post-processors apply to them! //对于配置中读取的BeanFactoryPostProcessor的处理,自己在xml中定义的,实现 //BeanFactoryPostProcessor接口的 String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false); // Separate between BeanFactoryPostProcessors that implement PriorityOrdered, // Ordered, and the rest. List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>(); List<String> orderedPostProcessorNames = new ArrayList<String>(); List<String> nonOrderedPostProcessorNames = new ArrayList<String>(); //对后处理器进行分类 for (String ppName : postProcessorNames) { if (processedBeans.contains(ppName)) { // skip - already processed in first phase above //已经处理过的, } else if (isTypeMatch(ppName, PriorityOrdered.class)) { priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class)); } else if (isTypeMatch(ppName, Ordered.class)) { orderedPostProcessorNames.add(ppName); } else { nonOrderedPostProcessorNames.add(ppName); } } // First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered. //按照优先级进行排序 OrderComparator.sort(priorityOrderedPostProcessors); invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory); // Next, invoke the BeanFactoryPostProcessors that implement Ordered. List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>(); for (String postProcessorName : orderedPostProcessorNames) { orderedPostProcessors.add(getBean(postProcessorName, BeanFactoryPostProcessor.class)); } //按照order排序 OrderComparator.sort(orderedPostProcessors); invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory); // Finally, invoke all other BeanFactoryPostProcessors. //无序,去执行调用 实现BeanFactoryPostProcessor的接口的实现类。 List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<BeanFactoryPostProcessor>(); for (String postProcessorName : nonOrderedPostProcessorNames) { nonOrderedPostProcessors.add(getBean(postProcessorName, BeanFactoryPostProcessor.class)); } invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory); }对于BeanFactoryPostProcessor的处理主要分为两种:
1:对于BeanDefinitionRegistry类的特殊处理。
2:对普通的BeanFactoryPostProcessor进行处理。