您好,登錄后才能下訂單哦!
本篇內容介紹了“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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。