I would like to dynamically change the application.property for each environment in development using Spring Boot.
Example)
local environment:application-local.property
development environment:application-dev.property
production environment:application-prod.property
macOS
IDE:STS
languages:Java
Setting the environment variable in application-local.property does not read this file and does not start successfully.
→ Because renaming application.property will start normally and the API will work properly, we believe that the reason is that the property file is not misconfigured and the property file is not read.
The details are as follows.
Please tell me the settings to load the environment variables and dynamically switch application.property.
In the ~./.bash_profile, click
export SPRING_PROFILES_ACTIVE=local
and
source to /.bash_profile
reflects the environment variables in .With the printenv command
SPRING_PROFILES_ACTIVE=local
We have verified that the appears.
From the Java program, click
Map<String, String>env=System.getenv();
Iterator<String>ite1=env.keySet().iterator();
Iterator<String>ite2=env.values().iterator();
while(ite1.hasNext()){
System.out.println("variable name:"+ite1.next()+"["+ite2.next()+"]");
}
System.out.println("---");
String profiles = System.getenv("SPRING_PROFILES_ACTIVE");
System.out.println(profiles);
says but
Variable name: ****
.
.
.
Variable name: ****
---
Null
It appears that the environment variable could not be read.
java macos spring-boot
I guess it happened when I tried to run Spring Boot on STS.
~/.bash_profile
settings are only valid for applications that started from bash
(that is, started from a terminal).
Probably starting STS from Dock etc.
I think there are three options available.
The first method is to start STS from a terminal.
This method takes over the environment variable that you set in .bash_profile
.
The second method is to configure environment variables that are enabled even when booted from the GUI such as Finder, Dock, Spotlight (Example).
The configuration method should vary depending on the MacOS version, so I think it would be better to specify the OS version in the question.
(As for this method, I do not currently own a Mac, so I cannot answer it.Please look forward to someone else's answer.)
The third option is to specify a profile in the boot option on the STS.
When you click the pencil icon from the Boot Dashboard to open the project configuration dialog, the Spring Boot tab has a profile setting, which is enabled by setting it to local
.
(By the way, this method does not set the environment variable, but sets the startup argument --spring.profiles.active
, so System.getenv("SPRING_PROFILES_ACTIVE");
does not pick it up.If you really want to set it as an environment variable, you can set it from the Environment tab of this dialog.)
On another note, the property file extension (at least by default) is .properties
instead of .property
(Reference:ProfileSpecific
).
After the above problem has been resolved, this may also be a problem.
© 2024 OneMinuteCode. All rights reserved.