Use system variable in spring boot [duplicate]
Use system variable in spring boot [duplicate]
This question already has an answer here:
I have defined a variable in my .bash_profile like this :
export PROFILE=local
Now, I want to get this value in my spring boot application to load different configuration files according to this value.
To do this, I used the annotation @Value but spring can't resolve the value of profile :
@Value("${profile}")
private String environment;
What have I missed ?
EDIT :
This code is launched from a JUnit test inside Eclipse (launched as desktop application)
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
${PROFILE}
Yes and it doesn't work !
– midix
2 days ago
This may help you : stackoverflow.com/questions/8168884/…
– Thoomas
2 days ago
2 Answers
2
You should add spring.profiles.active: ENV_NAME
in your properties file. Or you can pass it from command line using -Dspring.profiles.active=ENV_NAME
.
spring.profiles.active: ENV_NAME
-Dspring.profiles.active=ENV_NAME
EDIT:
You can also use Map<String, String> getenv = System.getenv();
to get all the environment variables in your code. Then you'll have access to all env variables.
Map<String, String> getenv = System.getenv();
I don't want to have application-dev.properties, application-prod.properties... because I don't want that api keys or other critical information are on our git repo.
– midix
2 days ago
You can use
jasypt
to encrypt values in properties file. Or you can any way send the same profile
name through command prompt using -Dprofile=ENV_NAME
and get it using @Value
– pkgajulapalli
2 days ago
jasypt
profile
-Dprofile=ENV_NAME
@Value
You can do it with the help of this Environment
class.
Environment
@Autowired
Environment environment;
and then
String profile = ((StandardServletEnvironment) environment).getSystemEnvironment().get("PROFILE").toString();
It returns a null value :/
– midix
2 days ago
System properties and environment variables are two different things!
– Seelenvirtuose
2 days ago
did you try
${PROFILE}
– pvpkiran
2 days ago