D.Bronder D.Bronder - 29 days ago 8
Java Question

Can not read properties file based on active profile

Hello java programmers,

I have a problem with reading the properties file based on active profile. I have created a spring boot application and I would like to deploy my application for different environments such as development and production. I have searched quit a lot, but I could not find a solution for my problem.

I have set a profile in the edit configuration menu. active profile in edit configuraions

I have a properties file called application-dev.properties in my src/main/resources and I have a Settings.java where I would like to read the properties file.

This is my Settings.java

@Configuration
@Component
public class Settings {

private static Logger log = Logger.getLogger(Settings.class);

@Value("${TradingBot.production}")
public boolean isProduction;

@Value("${api.KEY}")
public static String API_KEY;

@Value("${api.ORDERS_URL}")
public static String ORDERS_URL;

@Value("${api.TRADES_URL}")
public static String TRADES_URL;

@Value("${api.PARTICIPANTS_URL}")
public static String PARTICIPANTS_URL;

@Value("${api.INDIVIDUAL_URL}")
public static String INDIVIDUAL_URL;

@Value("${api.BALANCE_URL}")
public static String BALANCE_URL;

@Value("${api.TRANSACTIONS_URL}")
public static String TRANSACTIONS_URL;

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
return new PropertySourcesPlaceholderConfigurer();
}
}


According to the spring documentation this should be enough to read the correct properties file, unfortunately I get an empty String when I print the value.

When I run the project it will give me the following output.

2016-10-31 13:44:04.011 INFO 32992 --- [ main] TradingBot : The following profiles are active: dev


I hope someone can help me.

Answer

Thanks to M.Denium I knew what the problem was. I could solve it by adding the @Value property at the setter. Example:

 public static String API_KEY;

    public static String ORDERS_URL;

    public static String TRADES_URL;

    public static String PARTICIPANTS_URL;

    public static String INDIVIDUAL_URL;

    public static String BALANCE_URL;

    public static String TRANSACTIONS_URL;

    @Value("${api.KEY}")
    public void setAPI_KEY(String API_KEY) {
        this.API_KEY = API_KEY;
    }

    @Value("${api.ORDERS_URL}")
    public void setORDERS_URL(String ORDERS_URL) {
        this.ORDERS_URL = ORDERS_URL;
    }

    @Value("${api.TRADES_URL}")
    public void setTRADES_URL(String TRADES_URL) {
        this.TRADES_URL = TRADES_URL;
    }

    @Value("${api.PARTICIPANTS_URL}")
    public void setPARTICIPANTS_URL(String PARTICIPANTS_URL) {
        this.PARTICIPANTS_URL = PARTICIPANTS_URL;
    }

    @Value("${api.INDIVIDUAL_URL}")
    public void setINDIVIDUAL_URL(String INDIVIDUAL_URL) {
        this.INDIVIDUAL_URL = INDIVIDUAL_URL;
    }

    @Value("${api.BALANCE_URL}")
    public void setBALANCE_URL(String BALANCE_URL) {
        this.BALANCE_URL = BALANCE_URL;
    }

    @Value("${api.TRANSACTIONS_URL}")
    public void setTRANSACTIONS_URL(String TRANSACTIONS_URL) {
        this.TRANSACTIONS_URL = TRANSACTIONS_URL;
    }

In this case the variable will still be static and it can get the value from the property file.

Comments