javaSpringBeanContainer的启动加载初始化流程是什么
本篇内容介绍了“java Spring Bean Container的启动加载初始化流程是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
创新互联公司服务项目包括鹿寨网站建设、鹿寨网站制作、鹿寨网页制作以及鹿寨网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,鹿寨网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到鹿寨省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
1. DESC
Spring对beanFactory的处理
2. CODE
public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // Prepare this context for refreshing. prepareRefresh(); // Tell the subclass to refresh the internal bean factory. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context. prepareBeanFactory(beanFactory); try { // Allows post-processing of the bean factory in context subclasses. postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation. registerBeanPostProcessors(beanFactory); // Initialize message source for this context. initMessageSource(); // Initialize event multicaster for this context. initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses. onRefresh(); // Check for listener beans and register them. registerListeners(); // Instantiate all remaining (non-lazy-init) singletons. finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event. finishRefresh(); }catch (BeansException ex) { if (logger.isWarnEnabled()) { logger.warn("Exception encountered during context initialization-cancelling refresh attempt: " + ex); } // Destroy already created singletons to avoid dangling resources. destroyBeans(); // Reset 'active' flag. cancelRefresh(ex); // Propagate exception to caller. throw ex; }finally { // Reset common introspection caches in Spring's core, since we // might not ever need metadata for singleton beans anymore... resetCommonCaches(); } } }
3. 从上往下RUSH
3.1 prepareRefresh()
这里不用细看就是自身进行些配置和参数校验什么的
3.2 obtainFreshBeanFactory()
加载NameSpacehandler
使用各个NameSpcaceHandler生成Beandifinitions并注册到beanfactory里面去
3.2.1 加载NameSpaceHandler
NameSpaceHandler
加载过程的解析已经写过就不再写了; 参看前面的文章
3.2.2 NameSpaceHandler处理生成bean的定义(注意只是生成beanDefinition而不是生成具体的bean而且相关的Class文件也没有加载)
NamespaceHandlerSupport
//返回也没用,应该定义为void的,parserContext的registry就是beanFactory public BeanDefinition parse(Element element, ParserContext parserContext) { //获取具体的BeanDefinitionParser,比如说ComponentScanBeanDefinitionParser return findParserForElement(element, parserContext).parse(element, parserContext); }
ComponentScanBeanDefinitionParser
这是个样板例子,一看就知道怎么回事
public BeanDefinition parse(Element element, ParserContext parserContext) { String basePackage = element.getAttribute(BASE_PACKAGE_ATTRIBUTE);//获取basepackage设定的包名 basePackage = parserContext.getReaderContext().getEnvironment().resolvePlaceholders(basePackage);//有可能使用了$ String[] basePackages = StringUtils.tokenizeToStringArray(basePackage,ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS);//解析出全部的package // Actually scan for bean definitions and register them. ClassPathBeanDefinitionScanner scanner = configureScanner(parserContext, element);//配置扫描器 SetbeanDefinitions = scanner.doScan(basePackages);//扫包 registerComponents(parserContext.getReaderContext(), beanDefinitions, element);//向beanFactory注册 return null; }
3.3 prepareBeanFactory(beanFactory)
主要是完成BeanFactoryPostProcessors的初始化
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { ... //注意这是LTW,加载时代码织入进行增强AOP if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); } ... }
3.4 invokeBeanFactoryPostProcessors(beanFactory) 执行BeanFactoryPostProcessors,
对BeanFactory启动处理流程
protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) { PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors()); //需要注意的有个AspectJWeavingEnabler比较特殊,这个类的目的在于启用ClassLoader级别的Aop,和以前的Instrument In Jvm 有点类似, // 检测是否有LTW支持, if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader())); } }
“java Spring Bean Container的启动加载初始化流程是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
文章标题:javaSpringBeanContainer的启动加载初始化流程是什么
文章出自:http://myzitong.com/article/gosgsh.html