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//方法都比较简单,主要就是维护ServletContext和ServletConfig的引用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工厂中。
- Spring IOC-WebApplicationContext继承结构
- Spring IOC-WebApplicationContext刷新(was.refresh())
- Spring Ioc(3)——WebApplicationContext的构建
- 先码后看 spring IOC原理(WebApplicationContext、ContextLoaderListener) 转侵删
- 先码后看 spring IOC源码(beanfactory、applicationcontext、webapplicationcontext) 侵立删
- Spring IoC容器结构
- Spring WebApplicationContext
- spring webapplicationcontext
- Spring WebApplicationContext
- 扯谈spring mvc之WebApplicationContext的继承关系
- 扯谈spring mvc之WebApplicationContext的继承关系
- 扯谈spring mvc之WebApplicationContext的继承关系
- Spring IOC-BeanFactory的继承体系结构
- Spring IOC-ApplicationContext的继承关系
- Spring IOC注入(四)继承
- Spring中WebApplicationContext
- Spring中的WebApplicationContext
- strut2 call spring WebApplicationContext
- 【CodeVS】3303 翻转区间
- 指针函数与函数指针
- c#读excel 不用office组件
- plsql中的集合
- Xcode用Archive生成ipa失败,只生成Generic Xcode Archive的解决方案
- Spring IOC-WebApplicationContext继承结构
- 关于mysql中的SQL_CALC_FOUND_ROWS
- Android onInterceptTouchEvent()和onTouchEvent()
- Running the Cloudera Training VM in VirtualBox(转)
- 冒泡排序
- 【斯坦福---机器学习】复习笔记之欠拟合和过拟合的概念
- java 多线程
- 2012年5月SAT香港真题解析
- oracle 创建表sql语句,主键,自增长