我是靠谱客的博主 甜美裙子,最近开发中收集的这篇文章主要介绍RequestMappingHandler分析,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

获取BeanDefinition信息并注册

AbstractApplicationContext: refresh
|
AbstractApplicationContext: invokeBeanFactoryPostProcessors
|
PostProcessorRegistrationDelegate: invokeBeanDefinitionRegistryPostProcessors
|
BeanDefinitionRegistryPostProcessor: postProcessBeanDefinitionRegistry
|
ConfigurationClassPostProcessor:processConfigBeanDefinitions
|
ConfigurationClassParser: parse
|
ConfigurationClassParser: doProcessConfigurationClass
|
ComponentScanAnnotationParser: parse
|
ClassPathBeanDefinitionScanner: doScan

ConfigurationClassPostProcessor

public void processConfigBeanDefinitions(BeanDefinitionRegistry registry) {
    // Parse each @Configuration class
	ConfigurationClassParser parser = new ConfigurationClassParser(
			this.metadataReaderFactory, this.problemReporter, this.environment,
			this.resourceLoader, this.componentScanBeanNameGenerator, registry);

	Set<BeanDefinitionHolder> candidates = new LinkedHashSet<>(configCandidates);
	Set<ConfigurationClass> alreadyParsed = new HashSet<>(configCandidates.size());
	do {
		parser.parse(candidates);
		parser.validate();
        ...
        // parser.getConfigurationClasses()中含有@Configuration的WebMvcAutoConfiguration
		Set<ConfigurationClass> configClasses = new LinkedHashSet<>(parser.getConfigurationClasses());
		configClasses.removeAll(alreadyParsed);

        ...
		if (registry.getBeanDefinitionCount() > candidateNames.length) {
		
		    // registry.getBeanDefinitionNames()中含有@Bean的requestMappingHandlerMapping和requestMappingHandlerAdapter
		    // 仅仅获取的是名称
			String[] newCandidateNames = registry.getBeanDefinitionNames();
			
			Set<String> oldCandidateNames = new HashSet<>(Arrays.asList(candidateNames));
			Set<String> alreadyParsedClasses = new HashSet<>();
			for (ConfigurationClass configurationClass : alreadyParsed) {
				alreadyParsedClasses.add(configurationClass.getMetadata().getClassName());
			}
			for (String candidateName : newCandidateNames) {
				if (!oldCandidateNames.contains(candidateName)) {
					BeanDefinition bd = registry.getBeanDefinition(candidateName);
					if (ConfigurationClassUtils.checkConfigurationClassCandidate(bd, this.metadataReaderFactory) &&
							!alreadyParsedClasses.contains(bd.getBeanClassName())) {
						candidates.add(new BeanDefinitionHolder(bd, candidateName));
					}
				}
			}
			candidateNames = newCandidateNames;
		}
	}
	while (!candidates.isEmpty());

}

实例化Bean

AbstractApplicationContext: refresh
|
AbstractApplicationContext: finishBeanFactoryInitialization
|
DefaultListableBeanFactory: preInstantiateSingletons
|
AbstractBeanFactory: getBean
|
AbstractBeanFactory: doGetBean
|
AbstractBeanFactory: getSingleton
|
AbstractBeanFactory: getObject
|
AbstractAutowireCapableBeanFactory: createBean
|
AbstractAutowireCapableBeanFactory: doCreateBean
|
AbstractAutowireCapableBeanFactory: initializeBean
|
AbstractAutowireCapableBeanFactory: invokeInitMethods
|
RequestMappingHandlerMapping: afterPropertiesSet
RequestMappingHandlerAdapter: afterPropertiesSet

RequestMappingHandlerAdapter

public void afterPropertiesSet() {

    // 初始化Controller通用切面
    initControllerAdviceCache();

    // 初始化参数解析器
    if (this.argumentResolvers == null) {
        List<HandlerMethodArgumentResolver> resolvers = getDefaultArgumentResolvers();
        this.argumentResolvers = new HandlerMethodArgumentResolverComposite().addResolvers(resolvers);
    }
    // 初始化InitBinder解析器
    if (this.initBinderArgumentResolvers == null) {
        List<HandlerMethodArgumentResolver> resolvers = getDefaultInitBinderArgumentResolvers();
        this.initBinderArgumentResolvers = new HandlerMethodArgumentResolverComposite().addResolvers(resolvers);
    }
    // 初始化返回值处理器
    if (this.returnValueHandlers == null) {
        List<HandlerMethodReturnValueHandler> handlers = getDefaultReturnValueHandlers();
        this.returnValueHandlers = new HandlerMethodReturnValueHandlerComposite().addHandlers(handlers);
    }
}

RequestMappingHandlerMapping

@Override
public void afterPropertiesSet() {
	initHandlerMethods();
}

// Scan beans in the ApplicationContext, detect and register handler methods.
// 注册handler方法信息
protected void initHandlerMethods() {
	for (String beanName : getCandidateBeanNames()) {
		if (!beanName.startsWith(SCOPED_TARGET_NAME_PREFIX)) {
			processCandidateBean(beanName);
		}
	}
	handlerMethodsInitialized(getHandlerMethods());
}

最后

以上就是甜美裙子为你收集整理的RequestMappingHandler分析的全部内容,希望文章能够帮你解决RequestMappingHandler分析所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(31)

评论列表共有 0 条评论

立即
投稿
返回
顶部