Spring 源碼(二)註冊內置的BeanPostProcessor的BeanDefinition到容器

發起註冊

調用鏈路:java

registerAnnotationConfigProcessors:163, AnnotationConfigUtils (org.springframework.context.annotation)
registerAnnotationConfigProcessors:134, AnnotationConfigUtils (org.springframework.context.annotation)
<init>:83, AnnotatedBeanDefinitionReader (org.springframework.context.annotation)
<init>:66, AnnotatedBeanDefinitionReader (org.springframework.context.annotation)
<init>:61, AnnotationConfigApplicationContext (org.springframework.context.annotation)
<init>:82, AnnotationConfigApplicationContext (org.springframework.context.annotation)

源碼:spring

// 註冊 Spring 內置後置處理器的 BeanDefinition
public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
		BeanDefinitionRegistry registry, Object source) {

	DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
	if (beanFactory != null) {
		if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
			beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
		}
		if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
			beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
		}
	}

	Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<BeanDefinitionHolder>(4);

	if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
		RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
		def.setSource(source);
		beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
	}
	// 對註解@Autowired的實現
	if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
		RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
		def.setSource(source);
		beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	}
	// 對註解 @Required 的實現
	if (!registry.containsBeanDefinition(REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
		RootBeanDefinition def = new RootBeanDefinition(RequiredAnnotationBeanPostProcessor.class);
		def.setSource(source);
		beanDefs.add(registerPostProcessor(registry, def, REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
	}

	// Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
	// 對JSR-250的支持,如對 @Resource、@PostConstruct和@PreDestroy 的實現
	if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
		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)) {
		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)) {
		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)) {
		RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
		def.setSource(source);
		beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
	}

	return beanDefs;
}

註冊Bean定義到容器

調用鏈路:ide

registerBeanDefinition:861, DefaultListableBeanFactory (org.springframework.beans.factory.support)
registerBeanDefinition:315, GenericApplicationContext (org.springframework.context.support)
registerPostProcessor:218, AnnotationConfigUtils (org.springframework.context.annotation)
registerAnnotationConfigProcessors:163, AnnotationConfigUtils (org.springframework.context.annotation)
registerAnnotationConfigProcessors:134, AnnotationConfigUtils (org.springframework.context.annotation)
<init>:83, AnnotatedBeanDefinitionReader (org.springframework.context.annotation)
<init>:66, AnnotatedBeanDefinitionReader (org.springframework.context.annotation)
<init>:61, AnnotationConfigApplicationContext (org.springframework.context.annotation)
<init>:82, AnnotationConfigApplicationContext (org.springframework.context.annotation)

源碼:ui

//---------------------------------------------------------------------
// Implementation of BeanDefinitionRegistry interface
//---------------------------------------------------------------------
// 將BeanDefinition註冊到容器
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
		throws BeanDefinitionStoreException {

	...
	if (hasBeanCreationStarted()) {
		// Cannot modify startup-time collection elements anymore (for stable iteration)
		synchronized (this.beanDefinitionMap) {
			this.beanDefinitionMap.put(beanName, beanDefinition);
			List<String> updatedDefinitions = new ArrayList<String>(this.beanDefinitionNames.size() + 1);
			updatedDefinitions.addAll(this.beanDefinitionNames);
			updatedDefinitions.add(beanName);
			this.beanDefinitionNames = updatedDefinitions;
			if (this.manualSingletonNames.contains(beanName)) {
				Set<String> updatedSingletons = new LinkedHashSet<String>(this.manualSingletonNames);
				updatedSingletons.remove(beanName);
				this.manualSingletonNames = updatedSingletons;
			}
		}
	}
	else {
		// Still in startup registration phase
		this.beanDefinitionMap.put(beanName, beanDefinition);
		this.beanDefinitionNames.add(beanName);
		this.manualSingletonNames.remove(beanName);
	}
	...
}

這裏須要注意的兩個後置處理器分別是:this

  • AutowiredAnnotationBeanPostProcessor:對註解@Autowired的實現
  • CommonAnnotationBeanPostProcessor: 對JSR-250的支持,如對 @Resource、@PostConstruct和@PreDestroy 的實現

咱們註冊Bean的定義都是經過 BeanDefinitionRegistry 來實現的。code

相關文章
相關標籤/搜索