How to load all .properties files in an application, starting at a certain package?
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.
精彩评论