Spring IOC-WebApplicationContext继承结构
2015-07-03 15:22
639 查看
WebApplicationContext继承自ApplicationContext,所以ApplicationContext有的功能这个类都会有,那么本文主要介绍一下这个体系类独有的功能。
还是看接口中声明的方法,发现主要多了一个方法
那么下面我们就看子(接口)类的对这个方法的实现,首先看直接子接口ConfigurableWebApplicationContext(可配置的),看声明的方法:
声明的方法很好理解,直接看子类的实现,首先看第一级抽象子类AbstractRefreshableWebApplicationContext:
那么我们就直接看具体类AnnotationConfigWebApplicationContext和XmlWebApplicationContext吧,核心的方法就是设置bean配置信息的路径方法loadBeanDefinitions()。。其中XmlWebApplicationContext的和AbstractXmlApplicationContext的代码比较像,是这样的:
这里不再介绍,直接看AnnotationConfigWebApplicationContext的loadBeanDefinitions方法:
OK,bean工厂注册完了,注意上面的ClassPathBeanDefinitionScanner 类,这个类的作用就是读取bean上面的注解信息,从而将bean注册到bean工厂中,看一下通常在web.xml中的配置:
有了包,就可以将这个包下的所有合法注解的bean注册到bean工厂中。
还是看接口中声明的方法,发现主要多了一个方法
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工厂中。
相关文章推荐
- 关于webappbuilder for ArcGIS下拉菜单
- Spring IOC-WebApplicationContext刷新(was.refresh())
- 如此繁荣的移动webapp开发市场:总结当下的一些移动web开发套件
- 移动端WebApp隐藏地址栏的方法
- webapp构建工具库
- WebApplicationInitializer in Spring MVC
- SpringMVC 上下文webApplicationContext
- 【转】移动webApp开发的前端工程师必要的代码,手机webApp触摸左右滑动切换轮播图片广告
- 使用phonegap建立安卓webapp项目及其问题总结
- 为webapp应用制定IOS,Android桌面快捷图标
- Maven war包间相互依赖
- 图文详解MyEclipse中新建Maven webapp项目的步骤(很详细)
- 使用touch事件实现手机网页右侧菜单栏拖动显示隐藏
- WebApp开发之--"rem"单位
- 开发框架WebApp.net
- WebApp登陆页面设计
- 获取webapp目录
- SpringMVC理解之一:应用上下文webApplicationContext
- webapp前端性能优化规范
- webapp meta头设置