开发者

How to load all .properties files in an application, starting at a certain package?

开发者 https://www.devze.com 2023-03-24 22:42 出处:网络
I would like to load all all .properties files starting at a certain package level.All in that package and any child packages should be loaded.So for example if I specified my.foo as the starting pack

I would like to load all all .properties files starting at a certain package level. All in that package and any child packages should be loaded. So for example if I specified my.foo as the starting package, my.foo.MyProperties.properties and my.foo.bar.MyOtherProperties.properties should be picked up. I would prefer (and will accept) a solution that us开发者_高级运维es the classpath and went into all available .jars, but I will upvote a file based solution as well.


Use reflections. The code should be something like,

    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());

Look at the test code for more examples.

0

精彩评论

暂无评论...
验证码 换一张
取 消