50

Using a construct such as

@Component
public class SomeClass {

    @Inject
    private Environment env;

    private String key;


    @PostConstruct
    private void init() {

        key = env.getProperty("SOME_KEY_PROPERTY");

    }

    ....
}

it is possible to assign some field with some property.

Is there a shorter, more concise form to do this?

Abdull
  • 26,371
  • 26
  • 130
  • 172
  • look on http://stackoverflow.com/questions/3965446/how-to-read-system-environment-variable-in-spring-applicationcontext – iMysak Jan 31 '13 at 03:01

5 Answers5

74
@Component
public class SomeClass {

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

    ....
}
Abdull
  • 26,371
  • 26
  • 130
  • 172
52

You should be able to do this(assuming that you have a PropertySourcesPlaceHolderConfigurer registered)

    @Value("${SOME_KEY_PROPERTY}")
    private String key;

Note you can set a default value with:

    @Value("${SOME_KEY_PROPERTY:default-value}")
Zymotik
  • 6,412
  • 3
  • 39
  • 48
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • 12
    Note, this is for Spring 3.1+ only. – ach Jan 31 '13 at 15:37
  • 1
    This worked for me in my Spring Boot app (running spring-core 4.2.5.RELEASE), and it appears much cleaner than the other accepted answer. – Marty Chang May 02 '16 at 20:52
  • Ok but how do I configure said PropertySourcesPlaceHolderConfigurer to grab the environment variables values? – ninrod Dec 29 '16 at 15:52
  • you can use this approach on a setter method, too – jediz Jan 10 '17 at 10:11
  • @ach if I understand correctly, the difference between 3.1+ and lower is the `$` vs `#`. is that right? – asgs Feb 20 '18 at 11:10
  • 4
    @asgs The `#` tells spring that inside it is a SpEL expression and one expression you have is `environment` to get values from the environment. On the other hand, `$` express a property that spring will try to load its value, from a property file, environment, command-line arguments, etc. – PhoneixS Apr 29 '20 at 11:43
  • @PhoneixS if `$` can handle environment variables, then I guess, there's no need to use `#` (at least from a consistency perspective) – asgs May 04 '20 at 13:24
  • 1
    @asgs you're correct for environment variables. But `#` can handle more things, so maybe some projects have more `#` than `$`. – PhoneixS May 04 '20 at 16:03
  • what other property can you lookup other than `environment` using `#`? – Abdulbasith Feb 18 '22 at 15:41
32

You might also find it useful to provide 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.

default_value can also be blank, in that case it will behave as if some_property was optional:

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

(Notice the colon)

If the default value contains special characters (dot, colon, etc.), then wrap it in SpEL like this:

@Value("${some_property:#{'default_value'}}")
private String key;
rustyx
  • 80,671
  • 25
  • 200
  • 267
9

If you need to add an environment variable as default value.

@Value("${awsId:#{environment.AWS_ACCESS_KEY_ID}}")
@Value("${awsSecret:#{environment.AWS_SECRET_ACCESS_KEY}}")

This is a combination of two previous answers.

Jose Martinez
  • 11,452
  • 7
  • 53
  • 68
2

There are 17 ways to override a property value in spring boot, one of them is environment variables (Item 10. in the official documentation

The only trick is that you have to convert property names to to uppercase and underscore. For example if you want to overwrite the property

myApp.myProperty

then you have to have an environment variable called

MYAPP_MYPROPERTY

this means that you can just have

@Value("${myApp.myProperty}")

without any customization and you can still overwrite it with an environment variable

Peter Szanto
  • 7,568
  • 2
  • 51
  • 53