8

I want to be able to specify Environment Variables in my Spring Boot Application. I have an external library that I'm using that requires environment variables, and the values to those variables will change according to the environment where we are deploying.

For example, in application.properties of my packaged spring boot jar "myapp-boot-packaged.jar", I add the following property:

foo=bar

The equivalent for java -Dfoo=bar -jar myapp-boot-packaged.jar

4
  • 1
    what was the problem exactly? are you asking how to set env variables and values through spring boot application? Commented Mar 13, 2019 at 0:16
  • 2
    Yes. I have an external library that expects the environment variables to be set. However, the external library does not recognize it when I define the variables in application.properties. I am able to run the application by specifying the environment variables through the IDE's Run configuration. I am looking for a way to package it into my application. Commented Mar 13, 2019 at 18:50
  • Turns out that the issue was within the jar that was provided. Commented Apr 17, 2019 at 17:44
  • 1
    Please, did you find any solution for this? Commented Sep 29, 2021 at 4:45

3 Answers 3

6

You can simply run it informing in the command line like this:

$ java -jar myapp-boot-packaged.jar --foo='bar'

Spring Boot Docs

Sign up to request clarification or add additional context in comments.

Comments

5

You have to declare in application.properties

foo=${bar}

The when you run it:

Java -jar myApp.jar -Dbar=someValue

Comments

1

Use properties from environment or property source

You don't have to configure anything it's supported natively.

Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration. Property values can be injected directly into your beans by using the @Value annotation, accessed through Spring’s Environment abstraction, or be bound to structured objects through @ConfigurationProperties.

It's done for you, so what you still have to do is to declare your variable in :

  • Command Line arguments (--foo=bar)
  • JNDI attributes from java:comp/env.
  • Java System properties (System.getProperties()).
  • OS environment variables.

Properties by environment (Profiles)

If you want to use property per environment (local, dev, production) you can use profiles, a dummy example is to have application-[profile].properties (where [profile] could be dev, prod ...) along with application.properties in the your classpath or externalized (refer to using spring.config.location ).

Note that application profile specific properties override those in application.properties

2 Comments

Thanks. The external library reading the environment variable is not able to read it from my application.properties file. I am looking for a way to package it within my application using profile specific properties file.
Turns out that the issue was within the jar that was provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.