spring读取配置文件初始化容器操作总结
Spring初始化容器.三种经常用到的实现:
一、ClassPathXmlApplicationContext:从类路径中加载。
二、FileSystemXmlApplicationContext:从文件系统加载。
三、XmlWebApplicationContext:从web系统中加载。
使用1、bean工厂:最简单的容器,提供了基础的依赖注入支持。创建各种类型的Bean.
BeanFactory factory = null; //声明
ClassPathResource resource = new ClassPathResource("spring.xml");//类路径
FileSystemResource fileSystemResource =
new FileSystemResource("D:\\Ncode\\mcode\\sday02\\src\\spring.xml");
//系统路径
//InputStreamResource res
//= new InputStreamResource(
//new FileInputStream("D:\\Ncode\\mcode\\sday02\\src\\spring.xml"));
factory= new XmlBeanFactory(resource);
//XmlBeanFactory(参数可以是resource或者fileSystemResource等
//但是不能是 res 原因可以查看:文档Part III. Core Technologies 6. Resources
//中6.2 The Resource interface 有关isOpen方法的说明);
factory = new ClassPathXmlApplicationContext("spring.xml");
//从类路径中加载
factory = new
FileSystemXmlApplicationContext("D:\\Ncode\\mcode\\sday02\\src\\spring.xml"); //从文件系统加载
HelloService helloService =
factory.getBean("helloServiceImpl", HelloServiceImpl.class);
helloService.sayHello();
使用2、
应用上下文:建立在bean工厂基础之上,提供系统架构服务。ApplicationCotext,spring更加高级的容器。功能强大:
1.提供文本信息解析工具,包括对国际化支持。
2.提供载入文件资源的通用方法,如图片。
3.可以向注册为监听器的bean发送事件。
在很少的情况下,使用BeanFactory,如在移动设备。
ApplicationContext context = new
FileSystemXmlApplicationContext("file:D:\\Ncode\\mcode\\sday02\\src\\spring.xml");
context = new
ClassPathXmlApplicationContext("classpath:spring.xml");
HelloService helloService =
context.getBean("helloServiceImpl", HelloServiceImpl.class);
helloService.sayHello();
使用3、在web应用程序中
1、采用XMLWebApplication对象 初始化容器
XmlWebApplicationContext context = new XmlWebApplicationContext();
//默认的路径/WEB-INF/applicationContext.xml
//applicationContext.xml文件名称 可以任意起
//重新设置路径
//context.setConfigLocations(
// new String[] {"/WEB-INF/classes/applicationContext.xml"});
//设置ServletContext上下下文为web应用程序的上下文
context.setServletContext(getServletContext());
//刷新
context.refresh();
//根据id名称获取
HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class);
//执行helloDao对象的方法
helloDao.sayHello();
因为XmlWebApplicationContext默认加载的文件和文件名称为:
/WEB-INF/applicationContext.xml
因此需要在WEB-INF下添加此配置文件。
2、利用 WebApplicationContextUtils工具类
//直接采用getWebApplicationContext(getServletContext()) 获取context对象
WebApplicationContext context=
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
//context =
//WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
System.out.println(context);
HelloDao helloDao = context.getBean("helloDaoImpl", HelloDaoImpl.class);
helloDao.sayHello();
说明:
1、当采用getWebApplicationContext(getServletContext())获取context对象的时候,输出的context对象为null 所以在使用
context.getBean("helloDaoImpl", HelloDaoImpl.class);会出现空指针的异常
2、当采用getRequiredWebApplicationContext(getServletContext());获取context对象的时候 会出现如下bug
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
Bug解决:不关是1和2都是因为没有在web.xml文件中配置ContextLoaderListener,所以只需要在web.xml文件中加入以下代码中即可
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
转载于:https://blog.51cto.com/seajelly/1185166
spring读取配置文件初始化容器操作总结相关推荐
- Spring读取配置文件,获取bean的几种方式
Spring读取配置文件,获取bean的几种方式 方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemX ...
- Spring 读取配置文件(二)
Spring 读取配置文件并调用 bean package cn.com.test.receive;import org.springframework.beans.factory.annotatio ...
- spring 读取配置文件的优先级
为什么80%的码农都做不了架构师?>>> spring 读取配置文件的优先级 1 结论 在spring加载properties配置文件的过程中,会根据key出现覆盖现象,后加载 ...
- Spring读取配置文件的几种方法(从0开始)
新建一个Maven空项目 新建配置文件 在src -> main下新建一个资源目录resources,然后在该资源目录下新建一个application.properties文件. #自定义内容 ...
- 关于spring读取配置文件的两种方式
很多时候我们把需要随时调整的参数需要放在配置文件中单独进行读取,这就是软编码,相对于硬编码,软编码可以避免频繁修改类文件,频繁编译,必要时只需要用文本编辑器打开配置文件更改参数就行.但没有使用框架之前 ...
- spring读取配置文件的几种方式
场景 假如有以下属性文件dev.properties, 需要注入下面的tag tag=123 通过PropertyPlaceholderConfigurer <bean class=" ...
- Spring框架配置文件 application.xml 示例
注解版 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http: ...
- java加载xml配置文件_java读取配置文件的几种方法
原标题:java读取配置文件的几种方法 在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置文件来完成,本文根据笔者工作中用到的读取配置文件的方法小小总结一下,主要叙述的是spring ...
- java读取配置文件的几种方法
在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配置文件来完成,本文根据笔者工作中用到的读取配置文件的方法小小总结一下,主要叙述的是spring读取配置文件的方法. 一.读取xml配置 ...
最新文章
- AI来编程,码农何去何从?
- 一天搞定CSS:表格(table)--19
- 5. 最长回文子串——暴力法---动态规划解法---扩展中心法
- 【theano-windows】学习笔记二——theano中的函数和共享参数
- 4. 用MVC实现URL路由
- 爬虫scrapy模块
- [LintCode] Reverse Pairs 翻转对
- shell中$( )与${ }及$(( ))的区别
- gin框架的学习--golang
- 德芙网络营销策略ppt_看德芙网络营销策划经典案例,戳这里!
- 飞桨 DNN波士顿房价预测
- Java集成PayPal支付
- 基于HTML+CSS+JS+JQuery的京东商城
- python 地图偏移_python 地图经纬度转换、纠偏的实例代码 百度地图经纬度web纠偏怎么用...
- golang gorm增删改查db.Model db.Where db.Table
- Cmder安装和配置
- Chrome安装JsonView插件指南
- Excel表格快速生成LaTeX
- 老龄人口公交出行_在设计技术时要考虑到老年人口
- 主流浏览器对HTML5的支持情况
热门文章
- 用YOLOv5模型识别出表情!
- 官宣!这些北京高校,正式进军雄安!
- 投了3遍都被毙的论文,终于中了
- 声称代码已开源却迟迟没更新,网友等了好几个月,最终一怒之下把作者挂网上...
- SOTA排行榜大变天!MIT研究十个CV测试集,平均错误率超3.4%
- 像“打游戏”一样用Numpy,试试?
- 900 多道 LeetCode 题解,这个 GitHub 项目值得 Star!
- 改进张益唐证明,陶哲轩「他的证明比我还强」,这个天才青年还解决了困扰数学界近80年的「简单问题」...
- 数据结构和算法,到底有多重要?
- ICCV 2019:谷歌获最佳论文!中国入选论文最多,中科院、清华领跑