今天在看源码的时候,有些后置处理器莫名其妙就加了进来,因此我就看看它是从哪来的?
Spring内部会加载的选项如下
/**
* Register all relevant annotation post processors in the given registry.
* @param registry the registry to operate on
* @param source the configuration source element (already extracted)
* that this registration was triggered from. May be {@code null}.
* @return a Set of BeanDefinitionHolders, containing all bean definitions
* that have actually been registered by this call
*/
public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
BeanDefinitionRegistry registry, @Nullable Object source) {
//得到DefaultListableBeanFactory工厂
DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
if (beanFactory != null) {
//判断有没有排序相关的注解
if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
//(1)AnnotationAwareOrderComparator主要解析@Priority和@Order等排序注解
beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
}
if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
//(2)提供处理延迟加载功能ContextAnnotationAutowireCandidateResolver
beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
}
}
//BeanDefinitionHolder是存的beanName,alias和BeanDefinition的一个对应关系的数据结构
Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(8);
if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
//(3)internalConfigurationAnnotationProcessor,处理配置类
RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
def.setSource(source);
//间接调用BDR的registryBeanDefinition方法将这个BD注册进Map去
beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
}
// 注册AutowiredAnnotationProcessor
if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
//(4)internalAutowiredAnnotationProcessor 处理@Autowired
RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
}
// Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
// (5)internalCommonAnnotationProcessor 在bean实例化那块有用到
RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
}
// Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
// (6)internalPersistenceAnnotationProcessor 如果存在jpa支持就注册
RootBeanDefinition def = new RootBeanDefinition();
try {
def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,
AnnotationConfigUtils.class.getClassLoader()));
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);
}
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
}
if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {
//(7)internalEventListenerProcessor 事件监听后置处理器
RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
}
if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
//(8)internalEventListenerFactory 事件监听工厂
RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
}
return beanDefs;
}
可以看到在单纯的Spring会默认去注册五个Bean
就是如下的五个
- internalConfigurationAnnotationProcessor
- internalAutowiredAnnotationProcessor
- internalCommonAnnotationProcessor
- internalEventListenerProcessor
- internalEventListenerFactory
最后跟踪发现,它的注册是在AnnotationConfigApplicationContext中的 new AnnotatedBeanDefinitionReader(this)进行的
public AnnotationConfigApplicationContext() {
/**
* 实例化reader
* AnnotatedBeanDefinition 被注解的Bean
* 读取那些被加注解了的Bean 会默认去注册那些Spring的内部类
*/
this.reader = new AnnotatedBeanDefinitionReader(this);
/**
* 实例化一个scanner
* 能够扫描类,包,并转换为bd
* 这个scan存在的意义就是手动扫描,平时用的@ComponentScan并没有用这个scanner而是又重新new了一个ClassPathBeanDefinitionScanner
*/
this.scanner = new ClassPathBeanDefinitionScanner(this);
}
调用它的构造方法
public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry) {
this(registry, getOrCreateEnvironment(registry));
}
public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
Assert.notNull(environment, "Environment must not be null");
this.registry = registry;
this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);
//AnnotatedBeanDefinitionReader将具体任务交给AnnotationConfigUtils.registerAnnotationConfigProcessors
// 注册Spring内部Bean
AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
}
从而调用AnnotationConfigUtils的registerAnnotationConfigProcessors
/**
* Register all relevant annotation post processors in the given registry.
* @param registry the registry to operate on
*
* 在给定注册表中注册所有相关的注解类的处理器,就是Spring内部的8个内部bean,如果是原生Spring会是5个
*
*/
public static void registerAnnotationConfigProcessors(BeanDefinitionRegistry registry) {
registerAnnotationConfigProcessors(registry, null);
}
接下来的代码就是最上面的代码了!