I have a large Spring web application, dating back several years. I need to update it to Spring Boot (corporate requirement). I'm well on my way - it starts (!), although there are some issues with properties being injected, which causes the app to fail.
Specifically, there are three huge config files per profile, eg qa_config.properties
, qa_ehcache.xml
, qa_monitoring.properties
. At present I only care about qa_config.properties
, which I have renamed to Spring Boot's preferred name, application-qa.properties
, and application-qa_monitoring.properties
The application has a number of classes annotated with @Named
(from the javax.ws.rs-api ) which are loaded early - so early that I need to inject properties in the constuctor:
package com.domain.app;
import org.springframework.beans.factory.annotation.Value;
import javax.inject.Named;
@Named
public class Foo {
// Cant use @Value here, it is not yet in the context
protected String bar; // lives in application-qa.properties
protected String qux; // lives in application-qa_monitoring.properties
public Foo(@Value("${application.property.named.bar}") String bar,
@Value("${monitoring.property.named.qux}") String qux) {
this.bar = bar;
this.qux = qux;
doSomeWork();
}
}
Properties files:
#application-qa.properties
application.property.named.bar=something
and
#application-qa_monitoring.properties
monitoring.property.named.qux=another_thing
My problem: I want to have both application-qa.properties
and application-qa_monitoring.properties
in context as soon as possible, and before the @Named
classes are loaded.
To achieve this, I am running the application with an active profile of qa
, which successfully adds that set of properties into the context.
I added this line to the application.properties
file, to ensure that the other properties are loaded:
spring.profiles.include=${spring.profiles.active}_monitoring.properties
When I run the Spring Boot app, the output tells me
The following profiles are active: qa_monitoring.properties,qa
When debugging the Foo
class, the value of bar
is correct
But, the value of qux
is null.
Am I missing something about the order in which properties files are loaded? I would have thought that the include
line in application.properties
would be sufficient to "flatten" the two files very early on, if one is in context, so both should be available?
What I could do instead is just throw all the vars in the two properties files into one, the application-qa.properties
but I'd like, if possible, to keep them seperate and as close to the original structure as possible.
Thanks to pvpkiran and Andy Brown.
My application.properties file should have read
spring.profiles.include=${spring.profiles.active}_monitoring
ie, just adding another profile
, in this case qa_monitoring
- Spring automagically adds the application-
prefix and the .properties suffix