user8906123
user8906123

Reputation:

How to get the environment variable from local using @Value in spring?

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?

Upvotes: 1

Views: 2891

Answers (4)

user8906123
user8906123

Reputation:

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.

Upvotes: 2

Yuriy Tsarkov
Yuriy Tsarkov

Reputation: 2568

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

@PropertySource("classpath:{whatever_you_want_path}")

Upvotes: 0

Rishi
Rishi

Reputation: 51

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.

Upvotes: 0

Ash_s94
Ash_s94

Reputation: 817

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;

    ....
}

Upvotes: 2

Related Questions