Spring IOC-WebApplicationContext繼承結構

WebApplicationContext繼承自ApplicationContext,所以ApplicationContext有的功能這個類都會有,那麼本文主要介紹一下這個體系類獨有的功能。
還是看接口中聲明的方法,發現主要多了一個方法ServletContext getServletContext();就是說可以得到ServletContext,這是連接Spring和web的橋樑
那麼下面我們就看子(接口)類的對這個方法的實現,首先看直接子接口ConfigurableWebApplicationContext(可配置的),看聲明的方法:

//和ServletContext相關
void setServletContext(ServletContext servletContext);
void setServletConfig(ServletConfig servletConfig);
void setNamespace(String namespace);
void setConfigLocations(String[] configLocations);
String[] getConfigLocations();

聲明的方法很好理解,直接看子類的實現,首先看第一級抽象子類AbstractRefreshableWebApplicationContext:

//這個類的繼承關係可以看出,這個類既是WebApplicationContext的子類也是ApplicationContext的子類,而且繼承了AbstractRefreshableConfigApplicationContext的refresh()功能,所以這個類的主要任務就是設置bean配置的信息和設置與ServletContext的關係。
public abstract class AbstractRefreshableWebApplicationContext extends AbstractRefreshableConfigApplicationContext
//方法都比較簡單,主要就是維護ServletContextServletConfig的引用
private ServletContext servletContext;
private ServletConfig servletConfig;
public void setServletContext(ServletContext servletContext) {
    this.servletContext = servletContext;
}
public void setServletConfig(ServletConfig servletConfig) {
    this.servletConfig = servletConfig;
    if (servletConfig != null && this.servletContext == null) {       this.setServletContext(servletConfig.getServletContext());
    }
}

那麼我們就直接看具體類AnnotationConfigWebApplicationContext和XmlWebApplicationContext吧,核心的方法就是設置bean配置信息的路徑方法loadBeanDefinitions()。。其中XmlWebApplicationContext的和AbstractXmlApplicationContext的代碼比較像,是這樣的:
這裏寫圖片描述
這裏不再介紹,直接看AnnotationConfigWebApplicationContext的loadBeanDefinitions方法:

protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) {                  
    AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(beanFactory);    
    reader.setEnvironment(this.getEnvironment());                                             
這個類去scan                                                                                      
    ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(beanFactory); 
    scanner.setEnvironment(this.getEnvironment());  
    ………………
    //先當成一個一個的類去加載,如果報錯ClassNotFoundException,那麼就當成package去加載                                      
String[] configLocations = getConfigLocations();                                               
if (configLocations != null) {                                                                 
    for (String configLocation : configLocations) {                                            
        try {                                                                                  
            Class<?> clazz = getClassLoader().loadClass(configLocation);                       
            //註冊到bean工廠                                                                             
            reader.register(clazz);                                                            
        }                                                                                      
        catch (ClassNotFoundException ex) {                                                    
            if (logger.isDebugEnabled()) {                                                     
                logger.debug("" + ex);                                     
            }        
            //註冊到bean工廠                                                                          
            int count = scanner.scan(configLocation);                                          
            ………………                                                                                
        }                                                                                      
    }                                                                                          
}                                                                                                                                        

OK,bean工廠註冊完了,注意上面的ClassPathBeanDefinitionScanner 類,這個類的作用就是讀取bean上面的註解信息,從而將bean註冊到bean工廠中,看一下通常在web.xml中的配置:
這裏寫圖片描述
有了包,就可以將這個包下的所有合法註解的bean註冊到bean工廠中。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章