Spring IOC-WebApplicationContext继承结构

来源:互联网 发布:那个软件看书好 编辑:程序博客网 时间:2024/06/03 04:05

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

0 0
原创粉丝点击