Mike
Mike

Reputation: 103

How to pass a parameter as an environment variable

How do you pass a parameter as an environment variable?

Step 1: Open up user bash file 'vim ~/.bash_profile', write the environment variable and save the file

export TWLIO_NUMBER=+303....

Step 2: In the application porperties file, store the variable

twlio_number=${TWLIO_NUMBER}

Step 3: Import Value in order to use it

  import org.springframework.beans.factory.annotation.Value;

  @Value("${twlio_number}")
  private String TWILIO_NUMBER;

Also, if I hardcode the value in the application properties file, the code works.

Upvotes: 1

Views: 2703

Answers (2)

BabyishTank
BabyishTank

Reputation: 1482

So I have a system variable in my spring project.

String dataSourceUrl = System.getenv(DEFAULT_CONNECTION);

I am looking for something quick to set the variable in command line.

  1. I found this command work in mac export DEFAULT_CONNECTION=value (value wrap in single quote to prevent the values being replaced)
  2. You can check by env | grep DEFAULT_CONNECTION
  3. run as usual mvn spring-boot:run

Upvotes: 0

Related Questions