Reputation: 31
Spring boot searches for application.properties
in different locations:
in classpath, in current folder, in /config folder and so on.
Is there a simple way to load any resource the same way?
For example, I want to load files:
and parse them by myself, not like property files.
Is there a simple way to ask spring boot to find them like it searches for application.properties in different locations?
Upvotes: 3
Views: 2090
Reputation: 348
PropertySourcesPlaceholderConfigurer
used to load properties on startup and you can access its properties just like application.properties
@Bean
public PropertySourcesPlaceholderConfigurer loadResources() {
var resourceConfigurer = new PropertySourcesPlaceholderConfigurer();
resourceConfigurer.setLocations(
new ClassPathResource("file1"),
new ClassPathResource("file2"),
new ClassPathResource("file3"));
resourceConfigurer.setIgnoreUnresolvablePlaceholders(false);
resourceConfigurer.setIgnoreResourceNotFound(false);
return resourceConfigurer;
}
Note: application.properties are loaded after files registered by PropertySourcesPlaceholderConfigurer
Upvotes: 0
Reputation: 3019
The short answer is that no there is not a way to ask Spring Boot to this. However you can review all of their code. If you look in their ConfigFileApplicationListener class you can see how they have DEFAULT_SEARCH_LOCATIONS defined as the list of places to look for config files and they store this in a Set. Then they use a ResourceLoader to loop through and check if a desired resource exists at each location. If you review their code and their resource documentation I believe you could build your own service to get a file from a possible list of locations.
Upvotes: 1