8.3.4 在ApplicationContext中使用资源

        不管以怎样的方式创建ApplicationContext实例,都需要为ApplicationContext指定配置文件,Spring允许使用一份或多分XML配置文件。

        当程序创建ApplicationContext实例时,通常也是以Resource的方式来访问配置文件的,所以ApplicationContext完全支持ClassPathResource、FileSystemResource、ServletContextResource等资源访问方式。

        ApplicationContext确定资源访问策略通常有两种方法:

          ⊙ 使用ApplicationContext实现类指定访问策略。

          ⊙ 使用前缀指定访问策略。

        1.使用ApplicationContext实现类指定访问策略

          创建ApplicationContext对象时,通常可以使用如下三个实现类:

            ⊙ ClassPathXMLApplicationContext : 对应使用ClassPathResource进行资源访问。

            ⊙ FileSystemXmlApplicationContext : 对应使用FileSystemResource进行资源访问。

            ⊙ XmlWebApplicationContext : 对应使用ServletContextResource进行资源访问。

          当使用ApplicationContext的不同实现类时,就意味着Spring使用响应的资源访问策略。

        2.使用前缀指定访问策略

          Spring允许使用前缀来指定资源访问策略。

package edu.pri.lime._8_3_4.prefix.bean.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.Resource; public class SpringTest { public static void main(String[] args) {
/*
* 通过搜索文件系统路径下的xml文件创建ApplicationContext,
* 但通过指定classpath:前缀强制搜索类加载路径*/
ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:app_8_3_4.xml");
System.out.println(ctx);
/*使用ApplicationContext的资源访问策略来访问资源,没有指定前缀*/
Resource resource = ctx.getResource("book.xml");
System.out.println(resource.getFilename());
System.out.println(resource.getDescription());
}
}

          Console :

org.springframework.context.support.FileSystemXmlApplicationContext@2752f6e2: startup date [Sun Feb 12 19:50:38 CST 2017]; root of context hierarchy
book.xml
file [E:\Users\Administrator\workspace\lime\book.xml]

          系统从类加载路径下搜索xml;但使用ApplicationContext来访问资源时,依然采用的是FileSystemResource实现类,这与FileSystemXmlApplicationContext的访问策略是一致的。这表明:通过classpath:前缀指定资源访问策略仅仅对档次访问有效,程序后面进行资源访问时,还会根据ApplicationContext的实现类来选择对应的资源访问策略。

        3.classpath*:前缀的用法

          classpath*:前缀提供了加载多个XML配置文件的能力,当使用classpath*:前缀来指定XML配置文件时,系统将搜索类加载路径,找到所有与文件名匹配的文件,分别加载文件中的配置定义,最后合并成一个ApplicationContext。

package edu.pri.lime._8_3_4.prefixStart.bean.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext; public class SpringTest { public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath*:app_8_3_4.xml");
System.out.println(ctx);
}
}

          将配置文件app_8_3_4.xml分别放在应用的classes路径(该路径被设为类加载路径之一)下,并将配置文件放在classes/8.3.4.3路径下(该路径也被设为类加载路径之一),程序实例化ApplicationContext时显示:

为什么不是想象中的两个Loading XML bean definitions?

信息: Loading XML bean definitions from URL [file:/E:/Users/Administrator/workspace/lime/target/classes/app_8_3_4.xml]
org.springframework.context.support.FileSystemXmlApplicationContext@2752f6e2: startup date [Mon Feb 13 21:30:37 CST 2017]; root of context hierarchy

          当使用classpath*:前缀时,Spring将会搜索类加载路径下所有满足该规则的配置文件。

          如果不是采用classpath*:前缀,而是改为使用classpath:前缀,Spring则只加载第一个符合条件的XML文件。

          当使用classpath:前缀时,系统通过类加载路径搜索xml文件,如果找到文件名匹配的文件,系统立即停止搜索,加载该文件,即使有多个文件名匹配的文件,系统也只加载第一个文件。资源文件的搜索顺序取决于类加载路径的顺序,排在前面的配置文件将优先被加载。

          注意 :

            classpath*: 前缀仅对ApplicationContext有效。实际情况是,创建ApplicationContext时,分别访问多个配置文件(通过ClassLoader的getResource方法实现)。因此,classpath*:前缀不可用于Resource,使用classpath*:前缀一次性访问多个资源是行不通的。

          一次性加载多个配置文件的方式:指定配置文件时使用通配符。

ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:app_8_3*.xml");

          Console :

二月 13, 2017 9:56:35 下午 org.springframework.context.support.FileSystemXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@2752f6e2: startup date [Mon Feb 13 21:56:35 CST 2017]; root of context hierarchy
二月 13, 2017 9:56:35 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from file [E:\Users\Administrator\workspace\lime\target\classes\app_8_3_2.xml]
二月 13, 2017 9:56:35 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from file [E:\Users\Administrator\workspace\lime\target\classes\app_8_3_3.xml]
二月 13, 2017 9:56:35 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
信息: Overriding bean definition for bean 'testBean': replacing [Generic bean: class [edu.pri.lime._8_3_2.bean.impl.TestBean]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [E:\Users\Administrator\workspace\lime\target\classes\edu\pri\lime\_8_3_2\bean\impl\TestBean.class]] with [Generic bean: class [edu.pri.lime._8_3_3.bean.impl.TestBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [E:\Users\Administrator\workspace\lime\target\classes\app_8_3_3.xml]]
二月 13, 2017 9:56:35 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from file [E:\Users\Administrator\workspace\lime\target\classes\app_8_3_4.xml]

          Spring允许将classpath*:前缀和通配符结合使用:

ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath*:app_8_3*.xml");

          Console :

然而并没有什么不同。。。

二月 13, 2017 9:57:58 下午 org.springframework.context.support.FileSystemXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@2752f6e2: startup date [Mon Feb 13 21:57:58 CST 2017]; root of context hierarchy
二月 13, 2017 9:57:58 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from file [E:\Users\Administrator\workspace\lime\target\classes\app_8_3_2.xml]
二月 13, 2017 9:57:58 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from file [E:\Users\Administrator\workspace\lime\target\classes\app_8_3_3.xml]
二月 13, 2017 9:57:58 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory registerBeanDefinition
信息: Overriding bean definition for bean 'testBean': replacing [Generic bean: class [edu.pri.lime._8_3_2.bean.impl.TestBean]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [E:\Users\Administrator\workspace\lime\target\classes\edu\pri\lime\_8_3_2\bean\impl\TestBean.class]] with [Generic bean: class [edu.pri.lime._8_3_3.bean.impl.TestBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [E:\Users\Administrator\workspace\lime\target\classes\app_8_3_3.xml]]
二月 13, 2017 9:57:58 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from file [E:\Users\Administrator\workspace\lime\target\classes\app_8_3_4.xml]

        4.file:前缀的用法(然而并没有实现。。。why?)

package edu.pri.lime._8_3_4.prefixFile.bean.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.Resource; public class SpringTest { public static void main(String[] args) {
ApplicationContext ctx1 = new FileSystemXmlApplicationContext("app_8_3_4_4.xml");
System.out.println(ctx1);
ApplicationContext ctx2 = new FileSystemXmlApplicationContext("/app_8_3_4_4.xml");
System.out.println(ctx2);
Resource resource = ctx2.getResource("app_8_3_4.xml");
System.out.println(resource.getDescription());
}
}

          当FileSystemXmlApplicationContext作为ResourceLoader使用时,FileSysteTemApplicationContext会简单地让所有绑定的FileSystemResource实例把绝对路径都当成相对路径处理,而不管是否以斜杠开头。

          如果程序中需要访问绝对路径,则不要直接使用FileSystemResource或FileSystemXmlApplicationContext来指定绝对路径。建议强制使用file:前缀来区分相对路径和绝对路径。

        ApplicationContext ctx1 = new FileSystemXmlApplicationContext("file:app_8_3_4_4.xml");
ApplicationContext ctx2 = new FileSystemXmlApplicationContext("file:/app_8_3_4_4.xml");

啦啦啦

啦啦啦

8 -- 深入使用Spring -- 3...4 在ApplicationContext中使用资源的更多相关文章

  1. Spring配置文件详解 – applicationContext.xml文件路径

    Spring配置文件详解 – applicationContext.xml文件路径 Java编程                 spring的配置文件applicationContext.xml的默 ...

  2. 获取Spring的上下文环境ApplicationContext的方式

    摘自: http://blog.csdn.net/yang123111/article/details/32099329 获取Spring的上下文环境ApplicationContext的方式 Web ...

  3. 【Spring】23、ApplicationContext ,ApplicationContextAware,Listener,Event 的关系解读

    tomcat容器启动流程 启动tomcat容器,加载web.xml,建立整个容器(Servlet容器,这里是tomcat吧)的上下文,ServletContext,这时web.xml有个监听器,就是C ...

  4. spring ApplicationContext中Bean的生命周期

    AbstractApplicationContext Spring的AbstractApplicationContext是ApplicationContext的抽象实现类,该抽象类的refresh方法 ...

  5. spring BeanFactory及ApplicationContext中Bean的生命周期

    spring bean 的生命周期 spring BeanFactory及ApplicationContext在读取配置文件后.实例化bean前后.设置bean的属性前后这些点都可以通过实现接口添加我 ...

  6. 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean

    7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...

  7. Spring之BeanFactory与ApplicationConText区别

    使用BeanFactory从xml配置文件加载bean: import org.springframework.beans.factory.xml.XmlBeanFactory; import org ...

  8. 【转】Spring总结以及在面试中的一些问题

    [转]Spring总结以及在面试中的一些问题. 1.谈谈你对spring IOC和DI的理解,它们有什么区别? IoC Inverse of Control 反转控制的概念,就是将原本在程序中手动创建 ...

  9. ApplicationContext中Bean的生命周期

    引言 Bean应用上下文中的生命周期和在BeanFactory中生命周期类似,不同的是,如果Bean实现了org.springframework.context.ApplicationContextA ...

随机推荐

  1. PCL点云变换与移除NaN

    对点云的操作可以直接应用变换矩阵,即旋转,平移,尺度,3D的变换就是要使用4*4 的矩阵,例如:       等等模型 在这里直接使用程序开实现一个点云的旋转,新建文件matrix.cpp #incl ...

  2. js jQuery 右键菜单 清屏

    主要用到了oncontextmenu事件,在oncontextmenu事件中使用return false 屏蔽掉原生右键菜单,再使用event获取鼠标的坐标位置,设置自定义菜单的位置. http:// ...

  3. javascript显示年月日时间代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. ClassNotFoundException和NoClassDefFoundError的解决办法

    程序在其他手机都没有问题,但在刷到版本稍微较低的平板或手机上时就会直接闪退,并报出以下异常: java.lang.RuntimeException: Unable to instantiate act ...

  5. What is systemvolumeinformation? delete it?

    System Volume Information完全可以删除 许多人为了自己的电脑上的System Volume Information不知道而苦恼..我再此给大家介绍一下希望能给你点帮助.. Sy ...

  6. I2C上拉电阻

    在一些PCB的layout中,大家往往会看到在I2C通信的接口处,往往会接入一个4.7K的电阻,有的datasheet上面明确有要求,需要接入,有的则没有要求.   I2C接口 对于单片机来讲,有些I ...

  7. Axiom3D:Ogre公告板集与合并批次

    在上文中,我们把Ogre里的网格分解成点线面后,我们要完成一个新的功能,在点上突出显示. 得到顶点位置后,这个功能也就是一个很简单的事,最开始是每个顶点添加一个子节点,节点上添加一个圆点. forea ...

  8. android 8 wifi wifi 扫描过程

    查看一下android wifi扫描的过程. packages\apps\Settings\src\com\android\settings\wifi\WifiSettings.java public ...

  9. 【总结】牛客职播第十期:程盟有你,way来可期

    一.介绍 携程旅行2018校园春招流程介绍&面试答疑 讲师:宋涛,李响 二.答疑 1,技术都是先通过笔试进行优胜劣汰.论真英雄还是靠技术! 2,英语四级必须过! 3,国外大公司招人,对他们的文 ...

  10. Java如何创建用户自定义异常?

    在Java编程中,如何创建用户自定义异常? 此示例显示如何通过扩展Exception类来创建用户定义的异常. package com.yiibai; class MyException extends ...