什么是BeanFactory的定制化呢?先来看看这个接口:BeanFactoryPostProcessor
public interface BeanFactoryPostProcessor {
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}
使用的时候可以得到beanFactory,那么beanFactory中的所有东西都能得到。beanFactory有spring提供的默认配置。比如实例化策略呀,类加载器,资源加载器,bean名称的生成器等等。当然包括注册bf的map和实例化bean的map。这样我们可以设置我们们自定义的配置。或者手动向IOC容器中添加bf或者实例,不在通过spring。
还有一层意思是当我们调用到这个接口的时候,我们向IOC注册的bean,都已经解析为BeaDefinition保存再beanFactory中了。
spring是何时调用的这个接口呢?
找到refresh方法,找到这个方法:org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors
public static void invokeBeanFactoryPostProcessors(
ConfigurableListableBeanFactory beanFactory, List<BeanFactoryPostProcessor> beanFactoryPostProcessors) {
// beanFactoryPostProcessors是spring内部直接添加的处理器
// 所以得先处理spring内部的。
Set<String> processedBeans = new HashSet<>();
// 通常beanFactory 都会是BeanDefinitionRegistry,
// 既是BeanDefinition注册器,又是bean对象的实例化保存工厂。
if (beanFactory instanceof BeanDefinitionRegistry) {
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
List<BeanFactoryPostProcessor> regularPostProcessors = new ArrayList<>();
List<BeanDefinitionRegistryPostProcessor> registryProcessors = new ArrayList<>();
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
// 如果是BeanDefinitionRegistryPostProcessor就先调用postProcessBeanDefinitionRegistry方法。
registryProcessor.postProcessBeanDefinitionRegistry(registry);
registryProcessors.add(registryProcessor);
}
else {
regularPostProcessors.add(postProcessor);
}
}
// 上面都是先处理spring内部直接添加的处理器,这个处理器不会走spring的实例化过程
// 下面才是走spring实例化过程的。
List<BeanDefinitionRegistryPostProcessor> currentRegistryProcessors = new ArrayList<>();
// 1. 先找PriorityOrdered接口的BeanDefinitionRegistryPostProcessor
// 实现PriorityOrdered有高优先级
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
// 这里实例化了处理器
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
// 排序是根据order值排序的,从小到大,越小优先级越高。
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
//调用postProcessBeanDefinitionRegistry方法。
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
// 2. 还是同样的逻辑:只不过是找Ordered接口的BeanDefinitionRegistryPostProcessor
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName) && beanFactory.isTypeMatch(ppName, Ordered.class)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
// 最后找的是普通的
boolean reiterate = true;
while (reiterate) {
reiterate = false;
postProcessorNames = beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
for (String ppName : postProcessorNames) {
if (!processedBeans.contains(ppName)) {
currentRegistryProcessors.add(beanFactory.getBean(ppName, BeanDefinitionRegistryPostProcessor.class));
processedBeans.add(ppName);
reiterate = true;
}
}
sortPostProcessors(currentRegistryProcessors, beanFactory);
registryProcessors.addAll(currentRegistryProcessors);
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
currentRegistryProcessors.clear();
}
// 由于BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor
// 所以这里调用了BeanFactoryPostProcessor的方法
invokeBeanFactoryPostProcessors(registryProcessors, beanFactory);
invokeBeanFactoryPostProcessors(regularPostProcessors, beanFactory);
}
else {
// Invoke factory processors registered with the context instance.
invokeBeanFactoryPostProcessors(beanFactoryPostProcessors, beanFactory);
}
//以上是处理BeanDefinitionRegistryPostProcessor ,下面是处理BeanFactoryPostProcessor
// 还是同样的逻辑:
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
// Separate between BeanFactoryPostProcessors that implement PriorityOrdered,
// Ordered, and the rest.
List<BeanFactoryPostProcessor> priorityOrderedPostProcessors = new ArrayList<>();
List<String> orderedPostProcessorNames = new ArrayList<>();
List<String> nonOrderedPostProcessorNames = new ArrayList<>();
for (String ppName : postProcessorNames) {
if (processedBeans.contains(ppName)) {
// skip - already processed in first phase above
}
else if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
priorityOrderedPostProcessors.add(beanFactory.getBean(ppName, BeanFactoryPostProcessor.class));
}
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
orderedPostProcessorNames.add(ppName);
}
else {
nonOrderedPostProcessorNames.add(ppName);
}
}
// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
sortPostProcessors(priorityOrderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
List<BeanFactoryPostProcessor> orderedPostProcessors = new ArrayList<>(orderedPostProcessorNames.size());
for (String postProcessorName : orderedPostProcessorNames) {
orderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
sortPostProcessors(orderedPostProcessors, beanFactory);
invokeBeanFactoryPostProcessors(orderedPostProcessors, beanFactory);
// Finally, invoke all other BeanFactoryPostProcessors.
List<BeanFactoryPostProcessor> nonOrderedPostProcessors = new ArrayList<>(nonOrderedPostProcessorNames.size());
for (String postProcessorName : nonOrderedPostProcessorNames) {
nonOrderedPostProcessors.add(beanFactory.getBean(postProcessorName, BeanFactoryPostProcessor.class));
}
invokeBeanFactoryPostProcessors(nonOrderedPostProcessors, beanFactory);
// Clear cached merged bean definitions since the post-processors might have
// modified the original metadata, e.g. replacing placeholders in values...
beanFactory.clearMetadataCache();
}
该方法会先处理BeanDefinitionRegistryPostProcessor接口的类。
- 找到实现了PriorityOrdered接口的,根据order值从小到大排序。执行postProcessBeanDefinitionRegistry方法。
- 找到实现了Ordered接口的,根据order值从小到大排序,执行postProcessBeanDefinitionRegistry方法。
- 找到普通的,依旧排序,按什么排序呢?会找Order注解,根据注解的值从小到大排序;如果没有order注解,就默认是只是Integer.MAX_VALUE,最小优先级。 执行postProcessBeanDefinitionRegistry方法
- postProcessBeanDefinitionRegistry执行完成后,以上的所有实例会执行postProcessBeanFactory,因为BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor,要执行BeanFactoryPostProcessor的方法。
执行完BeanDefinitionRegistryPostProcessor类型的,下面才是执行BeanFactoryPostProcessor类型的。
逻辑是一样的。
所以如果你想定制BeanFactory中的处理器,那么实现BeanFactoryPostProcessor接口,如果你想更改beanDefinition中的值,实现BeanDefinitionRegistryPostProcessor即可偶是更好的选择。