Spring整理系列(03)——spring容器ApplicationContext初始化(spring应用上下文初始化)
来源:互联网 发布:股票量化软件 编辑:程序博客网 时间:2024/05/18 13:27
可以通过以下三种方式加载spring容器,实现bean的扫描与管理:
1、 ClassPathXmlApplicationContext:从类路径中加载
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");context.start();
2、 FileSystemXmlApplicationContext: 从文件系统加载
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("E:\\spring-context.xml");context.start();
3、 XmlWebApplicationContext:从web系统中加载
即把spring容器加载到servlet容器(web容器)中,所以需要在web.xml文件中配置servlet或者listener的方式,实现spring容器的加载
web.xml配置listener方式实现加载:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-context.xml</param-value> </context-param>
web.xml配置servlet方式实现加载:
<servlet> <servlet-name>springServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/spring-mvc*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>
注意:
在spring MVC中,一般需要同时使用上面两种方式,servlet方式负责配置controller等view层,listener方式负责配置service、dao等model层。
关于ContextLoaderListener和DispatcherServlet的关系与区别,可以详细看看下面的延伸文章(似懂非懂,暂时不做整理):
源码层面
原理层面(总结版)
应用层面
另外:关于web容器(servlet容器)加载spring容器及关系介绍:在servlet中注入spring的bean,servlet容器和spring容器
4、补充:spring容器初始化使用到的基础包、类以及作用
-org.springframework.beans
-BeanFactory提供配置结构和基本功能,加载并初始化Bean
-org.springframework.context
-ApplicationContext保存了Bean对象并在Spring中被广泛使用
0 0
- Spring整理系列(03)——spring容器ApplicationContext初始化(spring应用上下文初始化)
- Spring上下文ApplicationContext初始化Bean和事件
- Spring配置初始化ApplicationContext
- spring配置初始化applicationcontext
- java spring ApplicationContext-属性初始化
- Spring源码研究——容器初始化
- spring初始化容器
- Spring初始化容器
- Spring初始化容器
- Spring获得初始化容器
- Spring容器初始化
- spring容器重复初始化
- Spring IoC容器初始化
- Spring Ioc容器初始化
- spring初始化容器
- Spring容器初始化
- Spring容器初始化过程
- Spring-IoC容器初始化
- LINUX下的硬盘管理
- 设计模式GOF23——外观模式
- 接口数据的缓存及Linux定时缓存数据
- 【一天一道Leetcode】#203.Remove Linked List Elements
- 【算法】(扩展)KMP+manacher
- Spring整理系列(03)——spring容器ApplicationContext初始化(spring应用上下文初始化)
- HDU 1171 Big Event in HDU(DP)
- HDU 5794 A Simple Chess(多校,dp,容斥)
- 不同系统如何安装 Git
- 2016-8-6夏令营总结(kmp,回文串,扩展kmp)
- iOS块动画使用
- characters
- okhttp3使用
- “除非Microsoft FTP 服务(FTPSVC)正在运行,否则无法启动FTP站点。服务目前已停止”问题解决