简体   繁体   中英

spring boot property with profile from dependency

Problem:

I have 3 parts in the software:

  • Client A service
  • Client B service
  • Target C service

I want to connect to C from A and B
I wrote a library with following setup:

  • /src/main/java/pkg.../TargetConnector.java
  • /src/main/java/pkg.../TargetConfig.java
  • /src/main/resources/application-dev.properties
  • /src/main/resources/application-tst.properties
  • /src/main/resources/application-prd.properties

My clients A and B both have there own sources and properties:

  • /src/main/java/pkg.../Client{A/B}Service.java
  • /src/main/java/pkg.../Client{A/B}Config.java
  • /src/main/resources/application-dev.properties
  • /src/main/resources/application-tst.properties
  • /src/main/resources/application-prd.properties

The properties of the Connector contains some login info for the service eg

target.url=https://....
target.usr=blablabla
target.key=mySHAkey

which is used in the TargetConfig to preconfigure the Connector eg

@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 (eg 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 eg 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:

  1. How can I load a property file in a dependency jar at all?
  2. How can I load the profiled version of the property file if the the properties file has a different name than application.properties? eg application-connector.properties
  3. How can i combine the answers from question 1 and 2 to load the profiled version of the property in the jar?

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.

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)

我认为此行中有一个错字@PropertySource({“ classpath *:application-connector-dev.properties”})请通过删除星号进行检查。

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}")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM