1

I set a key inside my environment (in bash_profile file) called 'FLAG'(the value is 'true'). I'm trying to get his value by using annotation Value. so far i tried to do this:

@Value("\${FLAG}")
 private lateinit var process_flag: String

but no success, I'm getting an error:

java.lang.IllegalArgumentException: Could not resolve placeholder 'FLAG' in string value "${FLAG}"

*should I add any import\annotation to the class?

3
  • How are you running the code? Is it running as you, in a process where your .bash_profile file was applied? Commented Nov 10, 2017 at 19:43
  • I'm using intellij so i guess its running on my local. i guess it doesn't know where is the bash_profile is..im using mac Commented Nov 10, 2017 at 19:45
  • See this question for how to verify the environment variables of another process: Environment variables of a running process on Unix? Commented Nov 10, 2017 at 19:47

4 Answers 4

2

Try providing a default value in case the variable is not defined:

@Value("${some_property:default_value}")
private String key;

Otherwise you'll get an exception whenever some_property is not defined.

If that doesn't work you can also try:

@Component
public class SomeClass {

    @Value("#{environment.SOME_KEY_PROPERTY}")
    private String key;

    ....
}
Sign up to request clarification or add additional context in comments.

Comments

2

Solution: maybe it will sound funny but the solution for me was to turn off and on the intellij, of course after declare the environment variable.

Comments

0

Spring cannot see what is inside bash_profile file it rather gives access to Jvm system properties. Try below:

java .... -DFLAG=${FLAG}

"DFLAG" is jvm system property and "flag" is the value in bash_profile file.

Comments

0

For the custom source locations try to add this annotation into your config:

@PropertySource("classpath:{whatever_you_want_path}")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.