Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring - read environment variables from inside the application.properties file

I specified Spring properties inside the application.properties file. How can i populate those properties from the environment variables?

Here is what I tried, but it doesn't seem to work:

application.properties

spring.datasource.url=jdbc:postgresql://#{ systemProperties['DATABASE_HOST']}:5432/dbname
spring.datasource.username = postgres
spring.datasource.password = postgres
like image 209
Oleg Avatar asked Sep 26 '16 12:09

Oleg


People also ask

How read data from application properties file in Spring?

Another way to read application properties in the Spring Boot application is to use the @ConfigurationProperties annotation. To do that, we will need to create a Plain Old Java Object where each class field matches the name of the key in a property file.

Can we use environment variables in properties file?

You can put environment variables in your properties file, but Java will not automatically recognise them as environment variables and therefore will not resolve them. In order to do this you will have to parse the values and resolve any environment variables you find.

How do you access a value defined in the application properties file?

You can use @Value("${property-name}") from the application. properties if your class is annotated with @Configuration or @Component . You can make use of static method to get the value of the key passed as the parameter.


2 Answers

You can refer to environment properties in the same way you refer to Spring properties using ${...} syntax.

In your case:

spring.datasource.url=jdbc:postgresql://${DATABASE_HOST}:5432/dbname
like image 109
Maciej Walkowiak Avatar answered Nov 15 '22 19:11

Maciej Walkowiak


Out of the box, as you know, spring-boot expects its Datasource details to be using a specific set of variable names. Being spring of course you can rework this if you need by a few methods:

1/ If the need to use variables from the environment comes from deployment to a cloud service such as Cloud Foundry or Horuku, there is spring-boot-starter-cloud-connector which handles allot of the plumbing out of the box. A good read is the (Binding to Data Services with Spring Boot in Cloud Foundry article and the Deploying to the cloud docs which walks you thru this

2/ Instead of relying on Spring-Boot's own auto-magical wiring mechanism, you can create a custom configuration bean to override how the DataSource information is populated. A good read explaining the annotations involved can be found here: Spring Java Config Documentation - @Bean Configuration JavaDOC. Based on your example above, here is what I spat out:

@Configuration
public class MyDataSourceConfig {

    @Bean
    @Primary
    public DataSource getDataSource() {

        String url = "jdbc:postgresql://" + System.getenv("DATABASE_HOST") + ":5432/dbname";
        String username = "postgres"; 
        String password = "postgres";
        String driverClassName = "org.postgresql.Driver";

        /*
         * Create the datasource and return it
         * 
         * You could create the specific DS 
         * implementation (ie: org.postgresql.ds.PGPoolingDataSource) 
         * or ask Spring's DataSourceBuilder to autoconfigure it for you, 
         * whichever works best in your eyes
        */

        return DataSourceBuilder
                .create()
                .url( url )
                .username( username )
                .password( password )
                .driverClassName( driverClassName )
                .build();
    }
}

Just remember that in spring, you can always override allot of the default behaviours with a little bit of digging!

Hope this helps!

like image 42
Eric G Avatar answered Nov 15 '22 20:11

Eric G