这个方法和第五步的方法相似,对BeanPostProcess按照排序优先级加入BeanFactory中,但是少了执行(invok)的步骤,用以Bean创建过程中调用(可参考我Bean生命周期那个文章).其中注入的MergedBeanDefinitionPostProcessor涉及到Bean生命周期和IOC功能可以结合流程分明的Spring的Bean生命周期、Spring控制反转IOC和依赖注入DI结构化简述看一下
public static void registerBeanPostProcessors( ConfigurableListableBeanFactory beanFactory, AbstractApplicationContext applicationContext) { String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false); int beanProcessorTargetCount = beanFactory.getBeanPostProcessorCount() + 1 + postProcessorNames.length; // 加了一个自定义的BeanPostProcessor.大概意思是检测BeanPostProcessor是否符合规范 beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount)); // 创建不同优先级BeanPostProcessor容器,在for循环中进行分类 List<BeanPostProcessor> priorityOrderedPostProcessors = new ArrayList<>(); List<BeanPostProcessor> internalPostProcessors = new ArrayList<>(); List<String> orderedPostProcessorNames = new ArrayList<>(); List<String> nonOrderedPostProcessorNames = new ArrayList<>(); for (String ppName : postProcessorNames) { if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) { BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class); priorityOrderedPostProcessors.add(pp); if (pp instanceof MergedBeanDefinitionPostProcessor) { internalPostProcessors.add(pp); } } else if (beanFactory.isTypeMatch(ppName, Ordered.class)) { orderedPostProcessorNames.add(ppName); } else { nonOrderedPostProcessorNames.add(ppName); } } // 注册加入高优先级BeanPostProcessor sortPostProcessors(priorityOrderedPostProcessors, beanFactory); registerBeanPostProcessors(beanFactory, priorityOrderedPostProcessors); // 注册加入中优先级BeanPostProcessor List<BeanPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size()); for (String ppName : orderedPostProcessorNames) { BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class); orderedPostProcessors.add(pp); if (pp instanceof MergedBeanDefinitionPostProcessor) { internalPostProcessors.add(pp); } } sortPostProcessors(orderedPostProcessors, beanFactory); registerBeanPostProcessors(beanFactory, orderedPostProcessors); // 注册加入常规BeanPostProcessor List<BeanPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size()); for (String ppName : nonOrderedPostProcessorNames) { BeanPostProcessor pp = beanFactory.getBean(ppName, BeanPostProcessor.class); nonOrderedPostProcessors.add(pp); if (pp instanceof MergedBeanDefinitionPostProcessor) { internalPostProcessors.add(pp); } } registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors); // 上面将所有的internal BeanPostProcessors筛选出来统一放到最低优先级加入 sortPostProcessors(internalPostProcessors, beanFactory); registerBeanPostProcessors(beanFactory, internalPostProcessors); // 注册检查BeanPostProcessor是否是ApplicationListener并加入上下文的Listener容器中 beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext)); }
registerBeanPostProcessors注册方法
注册加入方法
private static void registerBeanPostProcessors( ConfigurableListableBeanFactory beanFactory, List<BeanPostProcessor> postProcessors) { if (beanFactory instanceof AbstractBeanFactory) { // Bulk addition is more efficient against our CopyOnWriteArrayList there ((AbstractBeanFactory) beanFactory).addBeanPostProcessors(postProcessors); } else { for (BeanPostProcessor postProcessor : postProcessors) { beanFactory.addBeanPostProcessor(postProcessor); } } }
MergedBeanDefinitionPostProcessor接口
该接口涉及到Bean生命周期***能.在AbstractAutowireCapableBeanFactory类的doGreateBean中有调用使用,主要是两个对Bean内部属性注解扫描类,用于在Bean实例化后给Bean属性赋值.
在将MergedBeanDefinitionPostProcessor加入BeanFactory中后,创建Bean时对Bean的类属性信息解析,并将解析内容添加到了BeanDefinition上(在AbstractAutowireCapableBeanFactory类的576行).
解析执行代码
- AutowiredAnnotationBeanPostProcessor负责查找解析@Autowire和@Value标注的属性添加到BeanDefinition中
public AutowiredAnnotationBeanPostProcessor() { this.autowiredAnnotationTypes.add(Autowired.class); this.autowiredAnnotationTypes.add(Value.class); } public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) { InjectionMetadata metadata = findAutowiringMetadata(beanName, beanType, null); metadata.checkConfigMembers(beanDefinition); }
- CommonAnnotationBeanPostProcessor负责解析@Resource和@PostConstruct、@PreDestroy注解(继承了InitDestroyAnnotationBeanPostProcessor).并将标注的属性和初始化、销毁方法加入BeanDefinition中
static { webServiceRefClass = loadAnnotationType("javax.xml.ws.WebServiceRef"); ejbClass = loadAnnotationType("javax.ejb.EJB"); resourceAnnotationTypes.add(Resource.class); if (webServiceRefClass != null) { resourceAnnotationTypes.add(webServiceRefClass); } if (ejbClass != null) { resourceAnnotationTypes.add(ejbClass); } } public CommonAnnotationBeanPostProcessor() { setOrder(Ordered.LOWEST_PRECEDENCE - 3); setInitAnnotationType(PostConstruct.class); setDestroyAnnotationType(PreDestroy.class); ignoreResourceType("javax.xml.ws.WebServiceContext"); } // 先执行父类的查找注解方法,再查找@Resource注解标注的属性信息加入BeanDefinition中 public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) { super.postProcessMergedBeanDefinition(beanDefinition, beanType, beanName); InjectionMetadata metadata = findResourceMetadata(beanName, beanType, null); metadata.checkConfigMembers(beanDefinition); } // 查找Bean生命周期涉及到初始化和销毁方法注解.添加到BeanDefinition中 public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) { LifecycleMetadata metadata = findLifecycleMetadata(beanType); metadata.checkConfigMembers(beanDefinition); }
扫描信息使用
这些被扫描到BeanDefinition中后在AbstractAutowireCapableBeanFactory类的601行读取BeanDefinition添加对应的Bean依赖进去
参考
MergedBeanDefinitionPostProcessor和@Autowired,@Value的关系
Spring中@Value注解有多强大