如何从某个包开始加载应用程序中的所有.properties文件



我想从某个包级别开始加载所有所有.properties文件。应该加载该包中的所有内容和任何子包。例如,如果我指定我的。foo作为启动包,my.foo.MyProperties.properties和my.foo.bar.MyOtherProperties.properties应该被选中。我更喜欢(并将接受)使用类路径并进入所有可用的.jar的解决方案,但我也会投票支持基于文件的解决方案。

使用反射。代码应该是这样的,

    Predicate<String> filter = new FilterBuilder().include(".*\.properties");
    Reflections reflections = new Reflections(new ConfigurationBuilder()
            .filterInputsBy(filter)
            .setScanners(new ResourcesScanner())
            .setUrls(asList(ClasspathHelper.forJavaClassPath())));
    System.out.println(reflections.getStore().get(ResourcesScanner.class).keySet());

查看测试代码以获得更多示例。

最新更新