一、利用ClassPathXmlApplicationContext从classpath中读取XML文件

例1:
/* 加载应用上下文(单个文件)
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("resource/applicationContext.xml");"src/resource/"
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("file:D:/usermanage/src/applicationContext.xml");
*//* 加载应用上下文(多个文件) */
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","applicationContext_pro.xml"});/* 获取bean
BeanFactory factory = applicationContext;
IUserService userService = (IUserService) factory.getBean("userService");
*/
IUserService userService = (IUserService) applicationContext.getBean("userService");testAddUser(userService);//业务
例2:
/* 加载应用上下文(单个文件)
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
*//* 加载应用上下文(多个文件) */
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });// 获取bean
IUserService userService = (IUserService) applicationContext.getBean("userService");testAddUser(userService);// 关闭上下文
applicationContext.close();

二、利用ClassPathResource,从classpath中读取XML文件

/* 加载应用上下文(单个文件) */
Resource resource = new ClassPathResource("applicationContext.xml"); // 获取bean
BeanFactory factory=new XmlBeanFactory(resource);
IUserService userService = (IUserService)factory.getBean("userService");testAddUser(userService);

三、利用XmlWebApplicationContext读取( 专为Web工程定制)

例4:
/* 加载应用上下文 */
XmlWebApplicationContext xmlWebApplicationContext = new XmlWebApplicationContext();
xmlWebApplicationContext.setConfigLocations(new String[] {"applicationContext.xml"});
xmlWebApplicationContext.setServletContext(pageContext.getServletContext());
xmlWebApplicationContext.refresh();// 获取bean
IUserService userService = (IUserService ) xmlWebApplicationContext.getBean("userService ");testAddUser(userService);// 关闭上下文
xmlWebApplicationContext.close();

四、利用FileSystemResource读取

例5:
/* 加载应用上下文 */
Resource resource = new FileSystemResource("D:/tomcat/webapps/test/WEB-INF/classes/applicationContext.xml");
// 获取bean
BeanFactory factory = new XmlBeanFactory(resource);
IUserService userService = (IUserService) factory.getBean("userService");testAddUser(userService);

注意:利用FileSystemResource读取,则配置文件必须放在project根目录下,或者写明绝对路径,否则就会抛出找不到文件的异常。

五、利用FileSystemXmlApplicationContext读取,可以指定XML定义文件的相对路径或者绝对路径来读取定义文件。

例6:
/* 1 */
String[] path={"WebContent/WEB-INF/applicationContext.xml","WebContent/WEB-INF/applicationContext_task.xml"};
ApplicationContext context = new FileSystemXmlApplicationContext(path);/* 2
String path="WebContent/WEB-INF/applicationContext*.xml";
ApplicationContext context = new FileSystemXmlApplicationContext(path); *//* 3
ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:地址"); */

Spring加载applicationContext.xml应用上下文的方式相关推荐

  1. spring加载ApplicationContext.xml的四种方式

    spring加载ApplicationContext.xml的四种方式 spring 中加载xml配置文件的方式,好像有4种, xml是最常见的spring 应用系统配置源.Spring中的几种容器都 ...

  2. spring容器扩展功能之一:spring加载ApplicationContext.xml的四种方式

    容器加载Bean的常见两个类ApplicationContext和BeanFactory, 一.首先,看看spring中加载配置在xml中的Bean对象到容器 spring 中加载xml配置文件的方式 ...

  3. web.xml的contextConfigLocation作用及自动加载applicationContext.xml

    web.xml的contextConfigLocation作用及自动加载applicationContext.xml 转自:http://blog.csdn.net/sapphire_aling/ar ...

  4. java配置文件实现方式_java相关:详解Spring加载Properties配置文件的四种方式

    java相关:详解Spring加载Properties配置文件的四种方式 发布于 2020-4-29| 复制链接 摘记: 一.通过 context:property-placeholder 标签实现配 ...

  5. Spring加载properties文件的两种方式

    2019独角兽企业重金招聘Python工程师标准>>> 在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取pro ...

  6. Tomcat原理系列之四:Tomat如何启动spring(加载web.xml)

    Tomcat原理系列之四:Tomat如何启动spring 熟悉的web.xml ContextLoaderListener Tomcat的初始化StandardContext.startInterna ...

  7. spring 如何加载applicationContext.xml

    在web.xml中配置spring NO1.推荐使用 <listener> <listener-class> org.springframework.web.context.C ...

  8. spring加载application.xml异常

    2019独角兽企业重金招聘Python工程师标准>>> Caused by: org.springframework.beans.factory.xml.XmlBeanDefinit ...

  9. Spring 加载、解析applicationContext.xml 流程

    概要 Spring 框架使用了BeanFactory 进行加载 xml 和生成 bean 实例.下面我们分析下Spring加载xml文件的过程. spring 版本是最新的 4.3.9 release ...

  10. Spring加载存放位置不同的beans.xml

    1 beans.xml存放在类目录下 ClassPathXmlApplicationContext atc=new ClassPathXmlApplicationContext("beans ...

最新文章

  1. python基础学习笔记(八)
  2. linux wifi 报错 siocsifflags: operation not possible due to rf-kill
  3. java综合面试题_综合性18道面试官必问经典Java面试题!
  4. MySQL中函数CONCAT及GROUP_CONCAT 对应oracle中的wm_concat
  5. vmware虚拟机安装win7_图文分享虚拟机怎么安装win7系统
  6. ARX中实现实体的偏移
  7. 去除椒盐噪声(简单的matlab代码实践,PSNR评价)
  8. sql数据库自动备份
  9. PLC通过智能网关,实现HTTP协议的POST请求,与服务端JSON格式双向通讯
  10. python做软件二次开发好吗_利用Python二次开发Creo
  11. 8类必须学会的运营工具
  12. linux鼠标选中的内容不能复制,解决vim不能使用鼠标右键复制的问题
  13. UML核心元素之参与者
  14. 把照片转换成pdf的格式
  15. PHP接口限定ip访问curl,PHP---CURL 访问接口问题
  16. pandas将csv文件保存文feather格式,大大加快读取速度
  17. Wireshark 网卡出错 The capture session could not be initiated on interface ‘en0‘
  18. Twitter 永久在家办公 VS 特斯拉对抗加州政府成功复工
  19. 【第六篇】Qt学习与使用---在qt中打印PDF文件(不是生成PDF)
  20. 联通服务器维护破解限速,网速1mbps(联通限速1mbps解除方法)

热门文章

  1. m126a linux驱动下载,【惠普m126a驱动】惠普m126a驱动下载 v15.0 官方版-开心电玩
  2. f分布表完整图a=0.01_R语言具有Student-t分布改进的GARCH(1,1)模型的贝叶斯估计...
  3. 如何成为一个合格的数据分析师
  4. 程序员人生之路(强烈推荐,分析的透彻!)
  5. 易语言版{大智慧/分析家/飞狐交易师}DLL插件接口开发模块(beta),自定义股票软件公式扩展函数...
  6. java excel 数组公式_Excel数组公式怎么使用? Excel数组公式计算的实例教程
  7. 【计算机网络】物理层 : 奈氏准则 ( 失真 | “失真“ 影响因素 | 码间串扰 | 奈奎斯特定理 | 码元极限传输速率 | 信息极限传输速率 | 奈氏准则计算示例 )★
  8. 斑马Revit:“BIM等级考试证书”究竟要不要考?
  9. 如何在Excel批量查询电话号码归属地?
  10. 朴素贝叶斯模型、推导、拉普拉斯平滑