前言
上篇博客主要以ClassPathXmlApplicationContext爲線,debug走了一遍構造函數。本篇嚴格來說是上一篇博客的繼續,因此本文將繼續以ClassPathXmlApplicationContext爲線走進AbstractApplicationContext.refresh()。
概述
refresh方法位於ConfigurableApplicationContext接口類中,被AbstractApplicationContext抽象類繼承,初步實現了ApplicationContext的一般功能,並且抽象類中使用了模板方法模式,給以後要實現的子類提供了統一的模板。
// 注代碼中的中文只是單純的翻譯,以及簡單的個人理解。
/**
* Load or refresh the persistent representation of the configuration,
* which might an XML file, properties file, or relational database schema.
* <p>As this is a startup method, it should destroy already created singletons
* if it fails, to avoid dangling resources. In other words, after invocation
* of that method, either all or no singletons at all should be instantiated.
* @throws BeansException if the bean factory could not be initialized
* @throws IllegalStateException if already initialized and multiple refresh
* attempts are not supported
*/
/**
* 加載或則刷新持久表示的配置,這個配置可能來自一個xml文件,配置文件或則關係型數據庫對象的集合。由於這是一個 * 啓動方法,它應該銷燬已經創建的單例。 如果失敗了,就避免了懸空的資源。 換句話說,在調用該方法之後,應該
* 實例化所有或者全部的no singletons(這裏不知道怎麼翻譯)。
*/
void refresh() throws BeansException, IllegalStateException;
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
// 爲刷新準備上下文,獲取容器的當時時間,同時給容器設置同步標識
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
// 告訴子類需要刷新的內部bean工廠
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
// 在上下文中準備bean工廠爲了之後使用。
// 爲bean工廠配置容器特徵,例如類加載器,事件處理器等
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
// 在上下文子類中,允許對bean工廠進行後期處理
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
// 在上下文中,調用工廠處理器註冊成爲bean
// 調用bean工廠的後處理器,這些處理器是在bean定義中想容器註冊的。
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
// 註冊用於攔截bean創建的bean處理器
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
// 爲上下文初始化消息資源
initMessageSource();
// Initialize event multicaster for this context.
// 初始化上下文中的事件多路廣播--->事件多播器
// 初始化上下文中的事件機制
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
// 初始化指定context子類中的特殊bean
onRefresh();
// Check for listener beans and register them.
// 註冊並檢查bean監聽器
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
// 初始化剩下的非延遲加載(non-lazy-init)單例beans
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 singleßtons to avoid dangling resources.
// 銷燬已經創建的單例bean
destroyBeans();
// Reset 'active' flag.
// 取消refresh操作,充值‘activity’標誌位
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();
先來看看一下prepareRefresh的時序圖
再看看代碼
/** * Prepare this context for refreshing, setting its startup date and * active flag as well as performing any initialization of property sources. */ /* 爲刷新準備上下文,設置它的開始時間和活動標誌以及執行所有屬性資源的初始化。 */ protected void prepareRefresh() { // 設置啓動時間 this.startupDate = System.currentTimeMillis(); // 設置關閉標誌位false,運行標誌位true. // close 和activityAtomicBoolean this.closed.set(false); this.active.set(true); if (logger.isInfoEnabled()) { logger.info("Refreshing " + this); } // Initialize any placeholder property sources in the context environment // 初始化上下文環境中的所有佔位符屬性元 // 默認爲空 initPropertySources(); // Validate that all properties marked as required are resolvable // see ConfigurablePropertyResolver#setRequiredProperties // 驗證被標記爲必要屬性的可解析性 getEnvironment().validateRequiredProperties(); // 上面方法單步進去,看到如下代碼。這裏對requiredProperties屬性的set還不知道在哪。 // public void validateRequiredProperties() { // MissingRequiredPropertiesException ex = new MissingRequiredPropertiesException(); // for (String key : this.requiredProperties) { // if (this.getProperty(key) == null) { // ex.addMissingRequiredProperty(key); // } // } // if (!ex.getMissingRequiredProperties().isEmpty()) { // throw ex; // } // } // Allow for the collection of early ApplicationEvents, // to be published once the multicaster is available... // 在早期就建立起ApplicationEvent事件容器,一旦多路廣播可用就可以發佈了。 this.earlyApplicationEvents = new LinkedHashSet<>(); }
obtainFreshBeanFactory()—— 告訴子類刷新內部bean工廠
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() { /** * This implementation performs an actual refresh of this context's underlying * bean factory, shutting down the previous bean factory (if any) and * initializing a fresh bean factory for the next phase of the context's lifecycle. */ // 這個實現執行一個上下文的底層bean工廠刷新,關閉之前已有的bean工廠,併爲上下文生命週期的下一個階段初始化bean工廠 refreshBeanFactory(); // 獲取bean工廠 ConfigurableListableBeanFactory beanFactory = getBeanFactory(); if (logger.isDebugEnabled()) { logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory); }ß return beanFactory; }
##### 2.1 refreshBeanFactory()
首先來看看時序圖:
/** * This implementation performs an actual refresh of this context's underlying * bean factory, shutting down the previous bean factory (if any) and * initializing a fresh bean factory for the next phase of the context's lifecycle. */ @Override protected final void refreshBeanFactory() throws BeansException { if (hasBeanFactory()) { destroyBeans(); closeBeanFactory(); } try { DefaultListableBeanFactory beanFactory = createBeanFactory(); beanFactory.setSerializationId(getId()); customizeBeanFactory(beanFactory); // 抽象方法,交由子類實現。 loadBeanDefinitions(beanFactory); synchronized (this.beanFactoryMonitor) { this.beanFactory = beanFactory; } } catch (IOException ex) { throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex); } }
refreshBeanFactory中主要分爲以下幾步:
- 判斷是否之前是否存在bean工廠,如果存在,將bean工廠、緩存等信息清空
- 創建一個DefaultListableBeanFactory工廠
- 定製當前bean工廠,能否進行重寫及循環引用 –customizeBeanFactory(beanFactory)
加載所有bean到beanFactory
2.2 loadBeanDefinitions(beanFactory)
在2.1中refreshBeanFactory中,loadBeanDefinitions是一個抽象方法,其具體實現交由子類。現在來看看AbstractXmlApplicationContext是如何實現的。
/** * Loads the bean definitions via an XmlBeanDefinitionReader. * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader * @see #initBeanDefinitionReader * @see #loadBeanDefinitions */ // 通過XmlBeanDefinitionReader加載bean定義 @Override protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException { // Create a new XmlBeanDefinitionReader for the given BeanFactory. XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); // 創建一個XmlBeanDefinitionReader // Configure the bean definition reader with this context's // resource loading environment. beanDefinitionReader.setEnvironment(this.getEnvironment()); // 設置當前的environment的對象 beanDefinitionReader.setResourceLoader(this); // 設置對應的ResourceLoader.ApplicationContext是ResourceLoader的子類 beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this)); // Allow a subclass to provide custom initialization of the reader, // then proceed with actually loading the bean definitions. // 鉤子方法,子類不重載就不做任何操作 initBeanDefinitionReader(beanDefinitionReader); loadBeanDefinitions(beanDefinitionReader); // 加載對應BeanDefinition對象 }
2.2.1 loadBeanDefinitions
上面loadBeanDefinitions對應的實現
// 在AbstractXmlApplicationContext中loadBeanDefinitions的實現 protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException { Resource[] configResources = getConfigResources(); // 爲空 if (configResources != null) { reader.loadBeanDefinitions(configResources); } // 在AbstractRefreshableConfigApplicationContext中的getConfigLocations獲取配置路徑 String[] configLocations = getConfigLocations(); // configLocations=/org/springframework/context/support/test/context*.xml if (configLocations != null) { // 調用xml reader.loadBeanDefinitions(configLocations); } } // 在AbstractBeanDefinitionReader中loadBeanDefinitions()方法解析lications數組 @Override public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException { Assert.notNull(locations, "Location array must not be null"); int counter = 0; for (String location : locations) { counter += loadBeanDefinitions(location); } return counter; }
具體的加載beanDefinition的實現在AbstractBeanDefinitionReader中
下面是它的時序圖
// 上面for循環中loadBeanDefinitions(location)方法的在AbstractBeanDefinitionReader中的 public int loadBeanDefinitions(String location, @Nullable Set<Resource> actualResources) throws BeanDefinitionStoreException { ResourceLoader resourceLoader = getResourceLoader(); // 獲取資源加載器 if (resourceLoader == null) { throw new BeanDefinitionStoreException( "Cannot import bean definitions from location [" + location + "]: no ResourceLoader available"); } if (resourceLoader instanceof ResourcePatternResolver) { // Resource pattern matching available. try { Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location); // 將給定的位置模式解析爲資源對象 int loadCount = loadBeanDefinitions(resources); if (actualResources != null) { for (Resource resource : resources) { actualResources.add(resource); } } if (logger.isDebugEnabled()) { logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]"); } return loadCount; } catch (IOException ex) { throw new BeanDefinitionStoreException( "Could not resolve bean definition resource pattern [" + location + "]", ex); } } else { // Can only load single resources by absolute URL. Resource resource = resourceLoader.getResource(location); int loadCount = loadBeanDefinitions(resource); if (actualResources != null) { actualResources.add(resource); } if (logger.isDebugEnabled()) { logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]"); } return loadCount; } }
在上面的getResources()方法之後又調用了一下這些方法:
由上圖可知從refresh—>getResourses—>doRetrievenMatchingFiles.其中從getResourses到doRetrievenMatchingFiles主要做了以下這些事: 1) 看location裏面是否以”classpath*:“,”war:“,”*/“開頭,會做一系列事(這裏流程沒有進去)。這裏主要在PathMatchingResourcePatternResolver#getResources(String locationPattern)
2) 找到location的上層路徑rootDirResource,同時分離出外層子路徑subPattern
3)轉而根據rootDirResource獲取完整文件路徑rootDir
4)再將完整文件路徑rootDir和外層自路徑subPattern拼接得到fullPattern
5)最後找出rootDir下所有文件的全路徑進行遍歷,與fullPattern進行匹配.
protected void doRetrieveMatchingFiles(String fullPattern, File dir, Set<File> result) throws IOException { if (logger.isDebugEnabled()) { logger.debug("Searching directory [" + dir.getAbsolutePath() + "] for files matching pattern [" + fullPattern + "]"); } File[] dirContents = dir.listFiles(); if (dirContents == null) { if (logger.isWarnEnabled()) { logger.warn("Could not retrieve contents of directory [" + dir.getAbsolutePath() + "]"); } return; } Arrays.sort(dirContents); for (File content : dirContents) { String currPath = StringUtils.replace(content.getAbsolutePath(), File.separator, "/"); if (content.isDirectory() && getPathMatcher().matchStart(fullPattern, currPath + "/")) { if (!content.canRead()) { if (logger.isDebugEnabled()) { logger.debug("Skipping subdirectory [" + dir.getAbsolutePath() + "] because the application is not allowed to read the directory"); } } else { doRetrieveMatchingFiles(fullPattern, content, result); } } if (getPathMatcher().match(fullPattern, currPath)) { result.add(content); } } }
上述方法中匹配方法調用 getPathMatcher().match(fullPattern, currPath)
最終匹配算法:
AntPathMatcher#doMatch( String pattern, String path,boolean fullMatch, @Nullable Map<String, String> uriTemplateVariables)
匹配算法的大致步驟(中間省略了一些):
1)將需要匹配的兩個路徑按照路徑分割符分割,得到兩個有序數組pathDirs(路徑下的子文件全路徑分割後的字符數組),pattDirs(要匹配的模式分割後的字符串數組)
2)在得到pttDirs後會去看這裏面是否存在潛在的匹配,主要是爲了去匹配正則表達式中的{ ‘*’, ‘?’, ‘{’ },若不存在潛在的匹配則返回false
3)遍歷pattDirs,如果包含”**“,直接跳出遍歷;記錄當前位置,與pathDirs對應位置的字符串進行比較,不同則返回false
4)第3步全部匹配結束後,
在我們的路徑中
pattern = /Users/chonglou/IdeaProject/spring-framework/spring-context/out/test/resources/org/springframework/context/support/test/context*.xml path = /Users/chonglou/IdeaProject/spring-framework/spring-context/out/test/resources/org/springframework/context/support/test/contextC.xml
最後只是去看看我們的模式匹配路徑和全路徑是否都是以”/“結束,顯然上述兩個均沒有以”/“結束,返回false
這裏我們只是粗略介紹,之後,會詳細介紹。
注: 在AntPathMatcherTests測試用例中,還暫時未發現通過 - {spring:[a-z]+} matches the regexp [a-z]+ as a path variable named "spring" 也就是通過花括弧,這種形式去匹配xml路徑的。 不過卻發現通過 result = pathMatcher.extractUriTemplateVariables( "{symbolicName:[\\w\\.]+}-sources-{version:[\\d\\.]+}-{year:\\d{4}}{month:\\d{2}}{day:\\d{2}}.jar","com.example-sources-1.0.0-20100220.jar"); 這樣去解析jar包或則url路徑的。之後我們會有單獨的博客進行講解。
總結
- refresh 的一些方法但看確實能懂一些,但是要串起來,感覺有點難啊。加油吧。。。
- prepareBeanFactory()看了一部分了,後面其他方法也會陸續跟上。