因此,对于我们的测试结构,当前有一个基本模块,其中包含一些常见的配置文件等(例如:ds.properties)。现在,我目前正在其他模块中运行测试,并且尝试加载所有.properties文件(以获取所有配置),而我正在使用
(new PathMatchingResourcePatternResolver(getClass().getClassLoader())).getResources("classpath:*.properties")
现在,仅查找alpha.properties(模块中的属性文件)。有没有办法在所有模块中获取属性文件?
我已经尝试过的一些东西:
(new PathMatchingResourcePatternResolver(getClass().getClassLoader())).getResources("classpath:ds.properties")
返回我想要的ds.properties,但显然不返回auth.properties。
(new PathMatchingResourcePatternResolver(getClass().getClassLoader())).getResources("classpath*:*.properties")
再次只有alpha.properties
(new PathMatchingResourcePatternResolver(getClass().getClassLoader())).getResources("classpath*:**/*.properties")
从我不想要的jre返回alpha.properties和一堆.properties文件。
我太懒了,无法在文档中找到参考,但是本质上是这样的:
顶级类路径扫描未找到与模式匹配的所有资源。原因写在文档中。
将您的属性文件放入一个包(用于maven的src / main / resources / somefolder)中,并使其适应扫描路径,它应该可以正常工作。(类路径*:somefolder / *。properties)
请注意,classpath *:与Ant样式的模式结合使用时,除非模式文件实际存在于目标文件中,否则在模式启动之前,它至少必须与至少一个根目录可靠地配合使用。这意味着类似于“ classpath *:*。xml " will not retrieve files from the root of jar files but rather only from the root of expanded directories. This originates from a limitation in the JDK’s
ClassLoader.getResources()方法的模式,该模式仅返回传入的空字符串的文件系统位置(指示可能要搜索的根)。
classpath:
如果要搜索的根包在多个类路径位置中可用,则不能保证具有“ ”资源的蚂蚁样式模式会找到匹配的资源。这是因为诸如
com / mycompany / package1 / service-context.xml可能仅位于一个位置,但是当诸如
使用classpath:com / mycompany / ** / service-context.xml尝试解析它,解析器将处理getResource(“ com / mycompany”);返回的(第一个)URL。如果此基本包节点存在于多个类加载器位置,则实际的最终资源可能不在其下方。因此,classpath*:
在这种情况下,最好在相同的Ant样式模式下使用“ ”,它将搜索包含根包的所有类路径位置。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句