• Spring加载XML机制


    转载自跳刀的兔子   http://www.cnblogs.com/shipengzhi/articles/3029872.html

    加载文件顺序

    情形一:使用classpath加载且不含通配符

    这是最简单的情形,Spring默认会使用当前线程的ClassLoader的getResource方法获取资源的URL,如果无法获得当前线程的ClassLoaderSpring将使用加载类org.springframework.util.ClassUtils的ClassLoader。

    1.当工程目录结构如图所示:

         

    ApplicationContext context = new ClassPathXmlApplicationContext("conf/application-context.xml");

    加载[conf/application-context.xml]

    2.当工程目录结构如图所示:

    即bin目录下只有.class文件,没有配置文件,依赖的conf.jar里有配置文件:

    ApplicationContext context = new ClassPathXmlApplicationContext("conf/application-context.xml");

    加载[conf/application-context.xml]

    3. 当工程目录结构如图所示:

    ApplicationContext context = new ClassPathXmlApplicationContext("conf/application-context.xml");

    只会会加载bin/conf目录下的application-context.xml文件,不会加载jar包中的conf/application-context.xml。 

    情形二:使用classpath加载,包含通配符

    Spring会通过使用路径中的非通配符部分先确定资源的大致位置,然后根据这个位置在确定具体的资源位置

    1. 当工程目录结构如图所示:

    ApplicationContext context = new ClassPathXmlApplicationContext("conf/**/*application-context.xml");

    加载[admin-application-context.xml]

    加载[application-context.xml]

    2.当工程目录结构如图所示:

    ApplicationContext context = new ClassPathXmlApplicationContext("conf/**/*application-context.xml");

    加载conf/application-context.xml

    加载conf/admin/admin-application-context.xml

    3.当工程目录结构如图所示:

    ApplicationContext context = new ClassPathXmlApplicationContext("conf/**/*application-context.xml");

    bin/conf/application-context.xml文件和bin/conf/admin/admin-application-context.xml都会被加载,

    但conf.jar文件中的配置文件并不会被加载。

    情形三:使用classpath*前缀且不包含通配符

    使用classpath*前缀可以获取所有与给定路径匹配的classpath资源,从而避免出现两个不同位置有相同名字的文件,Spring只加载其中一个的情况。

    当工程目录结构如图所示:

    这时使用

    ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:conf/application-context.xml");

    Spring将会加载bin目录下的application-context.xml文件和jar包里的application-context.xml文件。

    情形四:使用classpath*前缀,包含通配符

     当工程目录结构如图所示:

     

    ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:conf/**/*application-context.xml");

    conf目录下包括各级子目录中的所有配置文件,因此bin/conf/application-context.xml和 bin/conf/admin/admin-application-context.xml

    以及jar包中的 conf/application-context.xml和 conf/admin/admin-application-context.xml都会被加载

    Classpath*加载和Classpath加载区别

    classpath*:的出现是为了从classpath和多个jar文件中加载相同的文件,classpath:只能加载找到的第一个文件。

    classpath*载使用了classloader的getResources() 方法;

    PathMatchingResourcePatternResolver类中,我们可以更清楚的了解其对的处理:如果是以classpath*开头,它会遍历classpath。

    最终从文件加载的时候仍然是JAVA中常见的读取文件的方法:
    如ClassPathResource得到inputstream的方法是利用class loader。

    如ClassPathResource得到inputstream的方法是利用class loader。

    public InputStream getInputStream() throws IOException { 
        InputStream is; 
        if (this.clazz != null) { 
            is = this.clazz.getResourceAsStream(this.path); 
        }

    如FileSystemResource得到inputstream的方法是利用FileInputStream。

    public InputStream getInputStream() throws IOException {
         return new FileInputStream(this.file);
     }
    Never mind!准备好才能迎接机会!
  • 相关阅读:
    我孤独吗?我软弱吗?
    DataGrid 中的 HyperLinkColumn 如何传递多个参数?(未整理)
    C# 获取 MAC地址!
    (文本)文件操作
    ioninfinitescroll实现上拉分页加载更多
    前端面试题:防抖的实现
    vue 传送门功能,实现模态窗口
    前端面试题:节流的实现( 减少一段时间的触发频率)
    vue中的render函数
    ionic5实现tab栏切换效果
  • 原文地址:https://www.cnblogs.com/sjjsh/p/5251626.html
Copyright © 2020-2023  润新知