0

I have used spring-boot profiles to change property values for different environments, now I want to use the same approach to load different resource files, ie example I have dev_queries and prod_queries.xml with sql queries.

How can I make spring-boot load dev_queries.xml if active profile is dev and prod_queries.xml otherwise. I know that I can check the active profile but my idea is to do not add specific logic for handle this situation.

Juan Rada
  • 3,513
  • 1
  • 26
  • 26

1 Answers1

2

Would it help to externalize the filename as a custom property (docs, especially 24.4)? So that in your properties you would use:

# application-dev.properties
myapp.query-source=dev_queries.xml

# application-production.properties
myapp.query-source=prod_queries.xml

In your application beans this setting can be accessed by using the @Value annotation:

@Value("${myapp.query-source}")
private String querySource;  // will be dev_queries.xml or prod_queries.xml

That way in the code where you are loading the xml file you don't have to conditionally check for the currently active profiles but can externalize that setting to the properties.

sthzg
  • 5,514
  • 2
  • 29
  • 50
  • I ended doing something really similar thanks :), @ConfigurationProperties(locations = "${myapp.query_file}") – Juan Rada Jan 24 '16 at 21:11