本篇内容介绍了“java Spring Bean Container的启动加载初始化流程是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
Spring对beanFactory的处理
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(); } } }
prepareRefresh()
这里不用细看就是自身进行些配置和参数校验什么的
obtainFreshBeanFactory()
加载NameSpacehandler
使用各个NameSpcaceHandler生成Beandifinitions并注册到beanfactory里面去
NameSpaceHandler
加载过程的解析已经写过就不再写了; 参看前面的文章
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);//配置扫描器 Set<BeanDefinitionHolder> beanDefinitions = scanner.doScan(basePackages);//扫包 registerComponents(parserContext.getReaderContext(), beanDefinitions, element);//向beanFactory注册 return null; }
主要是完成BeanFactoryPostProcessors的初始化
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { ... //注意这是LTW,加载时代码织入进行增强AOP if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); } ... }
对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的启动加载初始化流程是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。