Spring配置文件的读取
1、配置文件的命名
Spring框架中的默认配置文件,建议命名为applicationContext.xml
* 编写配置文件,默认位置有两个 ①src目录、②WEB-INF目录
2、Spring 配置文件分离
第一种 new ClassPathXmlApplicationContext("bean1.xml","bean2.xml");
同时加载多个配置文件,这些配置文件 并列关系
第二种 new ClassPathXmlApplicationContext("applicationContext.xml");
在applicationContext.xml
<import resource="classpath:bean1.xml"/>
<import resource="classpath:bean2.xml"/>
主配置文件是 applicationContext.xml ,在主配置文件中 引入 子配置文件 bean1.xml bean2.xml
在开发中 主要用 第二种
3、读取配置文件
读取配置文件 在开发中
// spring内部提供工厂,只需要将实现类进行配置,交由Spring工厂创建对象
// 读取 Spring配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml"); // applicationContext就是工厂
// 通过配置bean的id获得class类实例
IHelloService helloService2 = (IHelloService) applicationContext
.getBean("helloService");
ApplicationContext应用上下文,加载Spring框架
加载classpath:
new ClassPathXmlApplicationContext("applicationContext.xml");
加载磁盘路径:
new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
4、BeanFactory 接口 和 ApplicationContext 接口关系
AplicationContext 是 BeanFactory 的 一个子接口
BeanFactory 是Spring 核心工厂接口,加载Bean 采用延迟加载,使用getBean时,才会创建 Bean的实例
ApplicationContext 接口 对BeanFactory 的一个扩展 ,默认在加载配置文件时,就会创建Bean实例 ,同时提供额外功能
* 国际化处理
* 自动装配
* 事件传递
* 各种不同应用层的Context实现
* 在实际开发中,通常使用 ApplicationContext 接口
Spring配置文件的读取的更多相关文章
- java spring 配置文件的读取
java读取本地配置文件主要分为两类,一类为class相关文件或子文件夹下,一类文件 为jar包外配置文件. class相关文件夹或子文件夹下读取配置文件可以使用Object.class.getRes ...
- 读取spring配置文件的方法(spring读取资源文件)
1.spring配置文件 <bean id="configproperties" class="org.springframework.beans.factory. ...
- Spring Boot 2 读取配置文件
开发环境:IntelliJ IDEA 2019.2.2Spring Boot版本:2.1.8 新建一个名称为demo的Spring Boot项目. 一.默认配置文件 Spring Boot会读取名称a ...
- spring boot: 从配置文件中读取数据的常用方法(spring boot 2.3.4)
一,从配置文件中读取数据有哪些方法? 通常有3种用法: 1,直接使用value注解引用得到配置项的值 2, 封装到Component类中再调用 3, 用Environment类从代码中直接访问 生 ...
- Spring 配置文件中将common.properties文件外置
将配置文件的路径从项目中移出来 1. 在springApplicationContext中 <context:property-placeholder location="file:$ ...
- Spring配置文件外部化配置及.properties的通用方法
摘要:本文深入探讨了配置化文件(即.properties)的普遍应用方式.包括了Spring.一般的.远程的三种使用方案. 关键词:.properties, Spring, Disconf, Java ...
- XML配置文件的命名空间与Spring配置文件中的头
一直以来,写Spring配置文件,都是把其他配置文件的头拷贝过来,最多改改版本号,也不清楚哪些是需要的,到底是干嘛的.今天整理一下,拒绝再无脑copy. 一.Spring配置文件常见的配置头 < ...
- 分离你的spring配置文件,让结构更清晰
前言 接着上一篇的,这次框架的改变也成功分离了spring的配置文件. 以前,spring的配置文件从一开始的一点,到后面的逐渐变多,慢慢的,在一个spring的配置文件中就包含了好几块不同的bean ...
- Spring Web项目spring配置文件随服务器启动时自动加载
前言:其实配置文件不随服务器启动时加载也是可以的,但是这样操作的话,每次获取相应对象,就会去读取一次配置文件,从而降低程序的效率,而Spring中已经为我们提供了监听器,可监听服务器是否启动,然后在启 ...
随机推荐
- struts2文件下载及 <param name="inputName">inputStream</param>的理解
转自:http://blog.csdn.net/wnczwl369/article/details/7483290 转自:http://hi.baidu.com/c2_sun/item/934a542 ...
- C语言标量类型(转)
在C语言中,枚举类型.字符型和各种整数的表示形式统一叫做标量类型. 当在C表达式中使用标量类型的值时,编译器就会自动将这些标识符转换为整数保存. 这种机制的作用是,在这些标量类型上执行的操作与整型上执 ...
- android国外网站
转载来自 http://www.23apk.com/?p=305 http://www.androidboards.com/ http://www.androidev.com/ http://andr ...
- 电赛菜鸟营培训(零)——Keil环境搭建
一.Keil开发软件安装 1.安装keil软件 2.使用注册机进行破解 将方框内的ID号复制到注册机,然后得到License,放到最底下就可以完成了. 二.Keil工程搭建 表示参考数据手册,在这里建 ...
- zookeeper中client命令实践
Welcome to ZooKeeper! 2016-09-14 16:06:04,528 [myid:] - INFO [main-SendThread(master:2181):ClientCnx ...
- android 按钮宽度按比例
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_paren ...
- Xamarin.iOS项目编译提示Could not AOT the assembly
Xamarin.iOS项目编译提示Could not AOT the assembly 错误信息:Could not AOT the assembly **************.dll 这个错误是 ...
- Ubuntu 安装Redis体验
背景:由于之前一直没有试过Linux的环境,今天加了内存之后,虚拟机开了3G,速度大大提高,对照博客试一下安装Redis的过程. 体验: 下载源码,解压,编译 $ wget http://do ...
- ural 1156. Two Rounds
1156. Two Rounds Time limit: 2.0 secondMemory limit: 64 MB There are two rounds in the Urals Champio ...
- 如何修改 SplendidCRM 页脚版权信息
打开 SplendidCRM 网站中的Web Site\_controls\Copyright.ascx 文件找到这段代码<div id="divFooterCopyright&quo ...