zoukankan      html  css  js  c++  java
  • 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、读取配置文件

    读取配置文件 在开发中

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

  • 相关阅读:
    scp命令报错-bash: scp: command not found
    shell比较两个字符串是否相等
    bat脚本:自动压缩n天前的文件【转载】
    shell bash判断文件或文件夹是否存在
    linux文件分割(将大的日志文件分割成小的)【转载】
    TCP/IP模型各个层次的功能和协议
    nginx初级安装配置
    Heartbeat+DRBD+MySQL高可用方案【转】
    【转载】CentOS 6.4下PXE+Kickstart无人值守安装操作系统
    oracle的exp和imp命令的使用【转载】
  • 原文地址:https://www.cnblogs.com/kingxiaozi/p/3387148.html
Copyright © 2011-2022 走看看