Using env variable in Spring Boot’s

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

Using env variable in Spring Boot’s

  1. Using env variable in Spring Boot's

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

  2. Using env variable in Spring Boot's

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

Solution 1

You don’t need to use java variables. To include system env variables add the following to your 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 will read the appropriate property file automatically by application-{profile-name}.properties template.

Original Author Ken Bekov Of This Content

Solution 2

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:,,

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:


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 ?

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 – Of This Content

Solution 3

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


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


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:

Original Author Felipe Girotti Of This Content

Solution 4

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 ):-


Ideally, 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.

Code snippet here :-


    public class PropertiesUtils {

        public static final String 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(""),
                            new ClassPathResource("application-" + activeProfile + ".properties")};


Original Author Ajay Kumar Of This Content


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

Also Read,


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