博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring配置文件的读取
阅读量:7085 次
发布时间:2019-06-28

本文共 1413 字,大约阅读时间需要 4 分钟。

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、读取配置文件

读取配置文件 在开发中

1      // spring内部提供工厂,只需要将实现类进行配置,交由Spring工厂创建对象2         // 读取 Spring配置文件3         ApplicationContext applicationContext = new ClassPathXmlApplicationContext(4                 "applicationContext.xml");5 6         // applicationContext就是工厂7         // 通过配置bean的id获得class类实例8         IHelloService helloService2 = (IHelloService) applicationContext9                 .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 接口

 

转载于:https://www.cnblogs.com/kingxiaozi/p/3387148.html

你可能感兴趣的文章
ABP框架 - 动态Web Api层
查看>>
JUC回顾之-ScheduledThreadPoolExecutor底层实现原理和应用
查看>>
js中移除空白节点
查看>>
BZOJ 3343 教主的魔法 分块
查看>>
SQL NOW() 函数
查看>>
IntelliJ IDEA激活
查看>>
使用JavaScript实现弹出层效果的简单实例
查看>>
FreePascal - Typhon在Windows10 X64下的使用问题!
查看>>
来个原创: mongodb 集群实例
查看>>
MVC程序实现Autocomplete功能
查看>>
[设计模式]静态代理模式
查看>>
[裴礼文数学分析中的典型问题与方法习题参考解答]5.1.24
查看>>
电脑稳定性检测软件
查看>>
rabbitmq management advance lesson
查看>>
window server 2012 更改密钥 更改系统序列号
查看>>
2.6 《硬啃设计模式》第8章 复制不是很难 - 原型模式(Prototype Pattern)
查看>>
Intel处理器 天梯图
查看>>
attachEvent传递给其handler的一个默认参数
查看>>
简单介绍join,outer-join,semi-join,anti-join的区别
查看>>
最能够体现一个好的程序员的地方
查看>>