1

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)

midix
  • 111
  • 8

2 Answers2

0

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 .

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.

pkgajulapalli
  • 1,066
  • 3
  • 20
  • 44
  • 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 Jun 29 '18 at 08:33
  • 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 Jun 29 '18 at 08:36
0

You can do it with the help of this Environment class.

@Autowired
Environment environment;

and then

String profile = ((StandardServletEnvironment) environment).getSystemEnvironment().get("PROFILE").toString();
pvpkiran
  • 25,582
  • 8
  • 87
  • 134