Spring源码阅读-CustomAutowireConfigurer
一、配置自定义自动装配类,CustomAutowireConfigurer是一个BeanFactoryPostProcessor,允许方便地注册自定义autowire限定符类型。即使没有使用Spring的@Qualifier注解
可以看到CustomAutowireConfigurer实现了BeanFactoryPostProcessor接口:
- BeanFactoryPostProcessor bean工厂后置处理器,为一个 FunctionalInterface,只有一个 postProcessBeanFactory 方法,可以使用函数式编程
@FunctionalInterface
public interface BeanFactoryPostProcessor {
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}
- spring中并没有具体去实现postProcessBeanFactory方法,是提供给想要实现BeanPostProcessor 的三方框架使用的。谁要使用谁就去实现。作用是在BeanFactory准备工作完成后做一些定制化的处理,一般结合BeanPostProcessor接口的实现类一起使用,注入一些重要资源,类似Application的属性和ServletContext的属性
注意:BeanFactoryPostProcessor可以与 bean 定义交互并修改bean 定义,但永远不要与bean实例交互(不要在BeanFactoryPostProcessor进行可能触发bean实例化的操作)这样做可能会导致 bean 实例化过早,会破坏容器造成预估不到的副作用。如果需要该实例化过程,考虑使用BeanPostProcessor
二、接下来我们看看CustomAutowireConfigurer的具体实现:
其中最重要的就是他重写的BeanFactoryPostProcessor的postProcessBeanFactory方法
@Override
@SuppressWarnings("unchecked")
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
if (this.customQualifierTypes != null) {
// 如果 beanFactory 没有继承自 DefaultListableBeanFactory,抛出异常
if (!(beanFactory instanceof DefaultListableBeanFactory)) {
throw new IllegalStateException(
"CustomAutowireConfigurer needs to operate on a DefaultListableBeanFactory");
}
// 将 beanFactory 强制转为 DefaultListableBeanFactory 类型
DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) beanFactory;
// 如果 dlbf 中的用于检查 bean 定义的 autowire 候选者解析器 不是 QualifierAnnotationAutowireCandidateResolver 子类
// 设置它为 QualifierAnnotationAutowireCandidateResolver 类型
if (!(dlbf.getAutowireCandidateResolver() instanceof QualifierAnnotationAutowireCandidateResolver)) {
// 设置 bean 工厂用于检查bean定义是否为 autowired 候选者的解析器
dlbf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
}
// 定义 限定符注解自动装配候选解析器
QualifierAnnotationAutowireCandidateResolver resolver =
(QualifierAnnotationAutowireCandidateResolver) dlbf.getAutowireCandidateResolver();
// 遍历 自定义限定符类型 ( Class<? extends Annotation> 或 String 类型)
for (Object value : this.customQualifierTypes) {
Class<? extends Annotation> customType = null;
// 如果是 Class 类型,强转为 Class<? extends Annotation>
if (value instanceof Class) {
customType = (Class<? extends Annotation>) value;
} else if (value instanceof String) {
String className = (String) value;
// 通过类名得到类的字节码
customType = (Class<? extends Annotation>) ClassUtils.resolveClassName(className, this.beanClassLoader);
} else {
throw new IllegalArgumentException(
"Invalid value [" + value + "] for custom qualifier type: needs to be Class or String.");
}
// 如果 customType 不是 Annotation 类型,抛出异常
if (!Annotation.class.isAssignableFrom(customType)) {
throw new IllegalArgumentException(
"Qualifier type [" + customType.getName() + "] needs to be annotation type");
}
// 将 customType 添加到用于检查bean定义是否为 autowire 候选者的解析器中
resolver.addQualifierType(customType);
}
}
}
这个方法接收一个ConfigurableListableBeanFactory类型的参数,这是一个应用程序上下文使用的bean工厂。该方法用于bean工厂后置处理,在 bean 定义但未初始化时调用,修改应用程序上下文的内部bean工厂,其主要作用是把自定义自动装配类型加入到Spring容器中自动装配表中,此处使用 LinkedHashSet<>(2) 。
现在我们看看 ConfigurableListableBeanFactory
首先是它的继承实现关系图:
首先是 ListableBeanFactory ,Spring 鼓励使用这个接口定义的api,其他的4个接口都是不鼓励使用的。ListableBeanFactory提供容器中bean迭代的功能,不再需要一个个bean地查找。除了 getBeanDefinitionCount 和 containsBeanDefinition ,该接口中的方法不适合频繁调用,实现可能很慢。AutowireCapableBeanFactory在BeanFactory基础上实现了对存在实例的管理.可以使用这个接口集成其它框架,捆绑并填充并不由Spring管理生命 周期并已存在的实例.像集成WebWork的Actions 和Tapestry Page就很实用。ConfigurableBeanFactory定义BeanFactory的配置。
三、测试及使用
@Test
public void testCustomResolver() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
qualifiedResource(CustomAutowireConfigurerTests.class, "context.xml"));
CustomAutowireConfigurer cac = new CustomAutowireConfigurer();
CustomResolver customResolver = new CustomResolver();
bf.setAutowireCandidateResolver(customResolver);
cac.postProcessBeanFactory(bf);
TestBean testBean = (TestBean) bf.getBean("testBean");
assertEquals("#1!", testBean.getName());
}
public static class TestBean {
private String name;
public TestBean(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
<bean id="customAutowireConfigurer"
class="org.springframework.beans.factory.annotation.CustomAutowireConfigurer">
<property name="customQualifierTypes">
<set>
<value>example.CustomQualifier</value>
</set>
</property>
</bean>
AutowireCandidateResolver通过以下方式确定autowire候选者:
- 每个Bean定义的autowire-candidate值
- 元素上可用的任何default-autowire-candidates模式
- @Qualifier注解的存在以及在CustomAutowireConfigurer中注册的任何自定义注解
当多个bean有资格作为autowire候选者时,“primary”的确定如下:如果候选者中只有一个bean定义的primary属性设置为true,则将选择它。