Spring boot switch API calls

is there any spring way to configure switching between different API calls using the properties set. Any suggestions are welcomed. Thanks

1 answer

  • answered 2020-02-17 04:58 Mark Bramnik

    IMO, Your question is somewhat vague, an example of what you're trying to achieve could be helpful.

    Anyway, in spring boot "properties set" can be implemented in configurations and spring profiles:

    1. create file src/main/resources/application-p1.propreties (p1 stands for profile, give it a meaningful name instead):
    ... // and so forth
    1. create file src/main/resources/application-p2.properties that will denote a different profile of execution:
    ... // and so forth
    1. Note, the keys are the same, but the values are different between (1) and (2) Now, create beans "responsible for API calls". Inject the values with the given names (api1.host, api1.port, ...) to the corresponding beans

    2. Run the spring boot application with a profile:

    java -jar myapp.jar --spring.profiles.active=p1 // or alternatively p2

    Now different sets of properties values will be loaded depending on profiles.

    Side notes:

    1. Its also possible to use yaml files for configuration, in yaml file you can also define all profiles in one file if you wish or create different yamls

    2. If you want some "default" set of properties that would be resolved the application is running without --spring.profiles.active flag - you can put these key/value pairs into application.properties which is resolved automatically.

    3. If you specify the same key/value pairs in both application.properties and application-p1.properties and run with p1 profiles the values in application-p1.properties (more specific) will take precedence over the default values.

    Update 1 based on OP's comment:

    You still can use profiles in this case:

    For example: in application-p1.properties create a key/value like calls.shouldCallB=true application-p2.properties will contain key/value like calls.shouldCallB=false

    Then you can inject this value into your service of API where you decide where to call A, B, C, D beans:

    public class MyService {
       private final boolean shouldCallB;
       public MyService(boolean shouldCallB) {
          this.shouldCallB = shouldCallB;
       public void execute() {
          if(shouldCallB) {

    In Java Configuration you can map the configuration property with @Value or @ConfigurationProperties (spring boot supports both, configuration properties is better IMO):

    public class CallsConfigProps {
       private boolean shouldCallB;
       // getters, setters
    public class MyAppConfig {
        public MyService myService(CallsConfigProps config) {
          return new MyService(config.getShouldCallB());

    Depending on the loaded profile the switch will be turned on or off