Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: How to override properties with dash "-" on Linux ENV .profile?

I want to override ANY property in application.properties with an ENV setting. In my application, I define properties using "prefix" with DOTs "." and suffix with "dash" (e.g., "-").

For example:

application.server.jgroups-port= some port #

Now, I want to override this property from the OS ENV settings.

On Windows, when I set this ENV property, this is my results:

First (FAILS),

Windows ENV >> APPLICATION_SERVER_JGROUPS_PORT = 5445

environment.getProperty("application.server.jgroups-port") returns NULL

Second (FAILS),

Windows ENV >> APPLICATION_SERVER_JGROUPSPORT = 5445

environment.getProperty("application.server.jgroups-port") returns NULL

Third (THIS WORKS!),

Windows ENV >> APPLICATION_SERVER_JGROUPS-PORT = 5445

environment.getProperty("application.server.jgroups-port") returns 5445

Notice, the "dash" (e.g., "-") on the last one.

YAY! I have effectively set the property from the Windows ENV using a "dash". Spring Boot maps this ENV perfectly to the application property.

On Linux, however, it does not accept a "dash" (e.g., "-") in its ENV, so my .profile blows up when I use the same approach that I used on Windows >> APPLICATION_SERVER_JGROUPS-PORT = 5445. What do I need to do to make Linux ENV settings set my "application.server.jgroups-port" property?

EDIT: It looks like the org.springframework.core.env.SystemEnvironmentPropertySource is where I'd need to do some work to support a dashed property name in Java as a Linux ENV. For instance, a call to getProperty("somePrefix.foo-suffix") = APPLICATION_SERVER_JGROUPS_PORT in the SystemEnvironmentPropertySource just like it had a period - getProperty("somePrefix.foo.suffix")

like image 592
Jason Avatar asked Jan 26 '15 21:01

Jason


2 Answers

You can also provide an environment variable named SPRING_APPLICATION_JSON with valid JSON in it. This will allow you to override keys with special characters. For example :

export SPRING_APPLICATION_JSON='{"application.server.jgroups-port": 8080}'

This way, you can also override arrays defined in yaml like this :

foo.bar:
  - 1
  - 2
  - 3

with:

export SPRING_APPLICATION_JSON='{"foo.bar": ["4","5","6"]}'

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config

like image 125
jebeaudet Avatar answered Nov 15 '22 09:11

jebeaudet


Your second attempt should work. According to the Spring Boot Reference Documentation:

To convert a property name in the canonical-form to an environment variable name you can follow these rules:

  • Replace dots (.) with underscores (_).
  • Remove any dashes (-).
  • Convert to uppercase.

I just tested a very similar usecase and it worked well for me

like image 25
Neidhart Orlich Avatar answered Nov 15 '22 09:11

Neidhart Orlich