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工厂中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值