DALDOUL
DALDOUL

Reputation: 164

How to refactor the way of getting multiple @Value from a properties file

I need to get some values from aplication.properties file and these values depend on the country, for instance these are the below values that I need to fetch:

@Value("${fr.Name}")
private String frName;

@Value("${fr.address}")
private String frAddress;

@Value("${de.Name}")
private String deName;

@Value("${de.address}")
private String deAddress;

@Value("${bz.Name}")
private String bzName;

@Value("${bz.address}")
private String bzAddress;

Then in my service method, I will be using multiple if else statement and my list of values keep increasing each time I add new country

  public String apply(string country){
    if ("fr".equals(country))
       return frName + frAddress 
    else if ("de".equals(country))
       return deName + deAddress 
    else if ("bz".equals(country))
       return bzName + bzAddress 
} 

So how can I refactor it without using multiple if-else statements?

Upvotes: 3

Views: 968

Answers (2)

Ryuzaki L
Ryuzaki L

Reputation: 39998

2.7.1. Loading YAML

You can load the configuration file directly into Map using @ConfigurationProperties and then get the corresponding value with respective key. So you can avoid using if block

application.yml If you are using application.yml

countries:
  fr: fnamefaddress
  de: dnamedaddress
  bz: bnamebaddress

application.properties : If you are using application.properties

countries.fr=fnamefaddress
countries.de=dnamedaddress
countries.be=bnamebaddress

Configuration Class

@Configuration
@ConfigurationProperties
public class CountriesConfig {

   @Getter
   private Map<String, String> countries = new HashMap<String, String>();

}

output :

{bz=bnamebaddress, de=dnamedaddress, fr=fnamefaddress}

So in code you can simply use get or getOrDefault on Map

countries.get("fr");

Upvotes: 1

Nonika
Nonika

Reputation: 2560

Look at spring configuration properties. this gives an opportunity to handle configuration properties more declarative way https://www.baeldung.com/configuration-properties-in-spring-boot

EDIT

@Component
@ConfigurationProperties(prefix = "my", ignoreUnknownFields = false)

public class CountryBasedProps {

    Map<String,CountryProps> config = new HashMap<>();


    public Map<String, CountryProps> getConfig() {
        return config;
    }

    public void setConfig(Map<String, CountryProps> config) {
        this.config = config;
    }

    public static class CountryProps{
        String name;
        String address;

        /*
           other properties, with getters/setters
         */

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        @Override
        public String toString() {
            return "CountryProps{" +
                    "name='" + name + '\'' +
                    ", address='" + address + '\'' +
                    '}';
        }
    }

    @Override
    public String toString() {
        return "CountryBasedProps{" +
                "config=" + config +
                '}';
    }
}

properties:

my.config.fr.name=French
my.config.fr.address=Paris
my.config.de.name=German
my.config.de.address=Berlin

output

CountryBasedProps{config={fr=CountryProps{name='French', address='Paris'}, de=CountryProps{name='German', address='Berlin'}}}

Note:

add @EnableConfigurationProperties() into your project

Upvotes: 4

Related Questions