Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: How do you specify an environment variable that has dashes in the application.properties?

Tags:

I have an application.properties file that looks like this:

mcl.sso.frontend-url=http://blah.com:9001
mcl.sso.mocking-agent=false

I am trying to override these two variables from the command line. This should be possible by setting environment variables. Here's how I'm running the command:

MCL_SSO_FRONTEND_URL='foobar' MCL_SSO_MOCKING_AGENT='true' ./gradlew run

However, when I print out the values of these variables, mcl.sso.mocking-agent equals "true" (as expected), but mcl.sso.frontend-url still equals "http://blah.com:9001" (unexpected). Why doesn't mcl.sso.frontend-url change the value of the property? I can only assume this has something to do with the way Spring converts environment variables into property keys, but I can't find any specific documentation on this.

like image 869
Daniel Kaplan Avatar asked Jul 24 '15 22:07

Daniel Kaplan


People also ask

How do I set environment specific properties in Spring Boot?

Environment-Specific Properties File. If we need to target different environments, there's a built-in mechanism for that in Boot. We can simply define an application-environment. properties file in the src/main/resources directory, and then set a Spring profile with the same environment name.

How do I reference an application property in Spring Boot?

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.

How do you specify file path in application properties in Spring Boot?

properties in default location. Spring Boot loads the application. properties file automatically from the project classpath. All you have to do is to create a new file under the src/main/resources directory.


2 Answers

This has been fixed as of Spring Boot 1.2.5. Previously, you'd have to use MCL_SSO_FRONTEND-URL (notice the dash that can't be translated to an underscore).

You can play with bindings using this sample project.

like image 196
Stephane Nicoll Avatar answered Sep 21 '22 06:09

Stephane Nicoll


Just get rid of the dash:

mcl.sso.mocking-agent > MCL_SSO_MOCKINGAGENT
like image 45
alban maillere Avatar answered Sep 23 '22 06:09

alban maillere