Reputation: 1826
Problem:
I have 3 parts in the software:
I want to connect to C from A and B
I wrote a library with following setup:
My clients A and B both have there own sources and properties:
The properties of the Connector contains some login info for the service e.g.
target.url=https://....
target.usr=blablabla
target.key=mySHAkey
which is used in the TargetConfig to preconfigure the Connector e.g.
@Value("target.url")
String url;
@Value("target.usr")
String usr;
@Value("target.key")
String key;
@Bean
public TargetConnector connector() {
return new TargetConnector(url, usr, key);
}
Now when I use the connector jar in the client I can find the configuration via packagescan. The connector class is loaded but the problem is that it does not load the properties files.
Research
I found that multiple property files cannot have the same name (e.g. clients application-{profile}.properties clashes with the one from the connector), so I tried to rename application-{profile}.properties of the targetConnector to application-connector-{profile}.properties.
The properties whoever still do not get loaded, (which makes sense since I do not have a e.g connector-dev profile but my profile is simply named dev).
Furthermore, even if I try to explicitly load one of the property files from the connector with:
@PropertySource({"classpath*:application-connector-dev.properties"})
it cannot be found
Question
My question is actually 3 tiered:
If further explanation is needed, please ask.
Answer
I went for an approach as given in the accepted answer. I Just created 3 configs for the dev, tst, prd profiles containing the values needed and annotated the config files with the correct profiles.
Upvotes: 2
Views: 3104
Reputation: 3170
I think there is a typo in this line @PropertySource({"classpath*:application-connector-dev.properties"}) Please check by removing the asterik.
Upvotes: 1
Reputation: 254
You are using @Configuration annotated class. Maybe you can have one per profile. Here you are an example:
@Configuration
@Profile("profileA")
@PropertySource({"classpath:application-profileA.properties"})
public class ConfigurationProfileA{
@Value("${target.url}")
String url;
@Value("${target.usr}")
String usr;
@Value("${target.key}")
String key;
@Bean
public TargetConnector connector() {
return new TargetConnector(url, usr, key);
}
}
Do the same for profile B (maybe you can structure this better but the key points here are the annotation @Profile("") and @PropertySource(""))
Once you have your config class, Spring will use the Configuration class you want by just filling -spring.profiles.active=profileA (or the name of the profile you have written in the @Profile("") annotation)
Upvotes: 1
Reputation: 2018
In order to run with a specific profile, you can run with option -spring.profiles.active=dev
for example
If you don’t run with a profile, it will load the default profile in application.properties
that you don’t seem to have.
Furthermore, an advice would be to always have an application.properties and put in it the common properties and the default values that you would override in other properties files.
Other mistake is how you assign properties with @Value
annotation, you need to use @Value("${PROPERTY_FROM_PROPERTIES_FILE}")
Upvotes: 0