Using env variable in Spring Boot’s application.properties

We Are Going To Discuss About Using env variable in Spring Boot’s application.properties. So lets Start this Java Article.

Using env variable in Spring Boot’s application.properties

Advertisements
  1. Using env variable in Spring Boot's application.properties

    You don't need to use java variables. To include system env variables add the following to your application.properties file:

  2. Using env variable in Spring Boot's application.properties

    You don't need to use java variables. To include system env variables add the following to your application.properties file:

Solution 1

Advertisements

You don’t need to use java variables. To include system env variables add the following to your application.properties file:

spring.datasource.url = ${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/"nameofDB"
spring.datasource.username = ${OPENSHIFT_MYSQL_DB_USERNAME}
spring.datasource.password = ${OPENSHIFT_MYSQL_DB_PASSWORD}

But the way suggested by @Stefan Isele is more preferable, because in this case you have to declare just one env variable: spring.profiles.active. Spring will read the appropriate property file automatically by application-{profile-name}.properties template.

Original Author Ken Bekov Of This Content

Solution 2

Advertisements

The easiest way to have different configurations
for different environments is to use spring profiles.
See externalised configuration.

This gives you a lot of flexibility.
I am using it in my projects and it is extremely helpful.
In your case you would have 3 profiles:
‘local’, ‘jenkins’, and ‘openshift’

You then have 3 profile specific property files:
application-local.properties,
application-jenkins.properties,
and application-openshift.properties

There you can set the properties for the regarding environment.
When you run the app you have to specify the profile to activate like this:
-Dspring.profiles.active=jenkins

Edit

According to the spring doc you can set the system environment variable
SPRING_PROFILES_ACTIVE to activate profiles and don’t need
to pass it as a parameter.

is there any way to pass active profile option for web app at run time ?

No.
Spring determines the active profiles as one of the first steps,
when building the application context.
The active profiles are then used to decide which property files are read and which beans are instantiated.
Once the application is started this cannot be changed.

Original Author Stefan Isele – prefabware.com Of This Content

Solution 3

Advertisements

Flyway doesn’t recognize the direct environment variables into the application.properties (Spring-Boot V2.1).
e.g

spring.datasource.url=jdbc:mysql://${DB_HOSTNAME}:${DB_PORT}/${DB_DATABASE}
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASS}

To solve this issue I did this environment variables, usually I create the file .env:

SPRING_DATASOURCE_URL=jdbc:mysql://127.0.0.1:3306/place
SPRING_DATASOURCE_USERNAME=root
SPRING_DATASOURCE_PASSWORD=root

And export the variables to my environment:

export $(cat .env | xargs)

And finally just run the command

mvn spring-boot:run

Or run your jar file

java -jar target/your-file.jar

There another approach here: https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/maven-plugin/examples/run-env-variables.html

Original Author Felipe Girotti Of This Content

Solution 4

Advertisements

Here is a snippet code through a chain of environments properties files are being loaded for different environments.

Properties file under your application resources ( src/main/resources ):-

 1. application.properties
 2. application-dev.properties
 3. application-uat.properties
 4. application-prod.properties

Ideally, application.properties contains all common properties which are accessible for all environments and environment related properties only works on specifies environment. therefore the order of loading these properties files will be in such way –

 application.properties -> application.{spring.profiles.active}.properties.

Code snippet here :-

    import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.Resource;

    public class PropertiesUtils {

        public static final String SPRING_PROFILES_ACTIVE = "spring.profiles.active";

        public static void initProperties() {
            String activeProfile = System.getProperty(SPRING_PROFILES_ACTIVE);
            if (activeProfile == null) {
                activeProfile = "dev";
            }
            PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer
                    = new PropertySourcesPlaceholderConfigurer();
            Resource[] resources = new ClassPathResource[]
                    {new ClassPathResource("application.properties"),
                            new ClassPathResource("application-" + activeProfile + ".properties")};
            propertySourcesPlaceholderConfigurer.setLocations(resources);

        }
    }

Original Author Ajay Kumar Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment