Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 4 @Value where property default is a java system property

Tags:

java

spring

In Spring 4, using the @Value annotation, what is the right way to specify a system property as a default if a specified property does not exists?

Whereas this works for the no-default case:

@Value("${myapp.temp}")
private String tempDirectory;

This doesn't work when I need a default:

@Value("#{myapp.temp ?: systemProperties.java.io.tmpdir}")
private String tempDirectory;

Nor does this:

@Value("#{myapp.temp ?: systemProperties(java.io.tmpdir)}")
private String tempDirectory;

Both of these give me an exception at the time Spring is trying to create the bean:

org.springframework.beans.factory.BeanCreationException: Error creating bean  
     with name 'configurationService': Invocation of init method failed;
     nested exception is java.lang.NullPointerException

Can this be done?

like image 723
David H Avatar asked Jun 29 '15 16:06

David H


1 Answers

I tried the following and it worked for me:

@Value("${myapp.temp:#{systemProperties['java.io.tmpdir']}}")
private String tempDirectory;

The missing parts for you I believe was not using ?: and needing the #{}. According to this answer:

${...} is the property placeholder syntax. It can only be used to dereference properties.

#{...} is SpEL syntax, which is far more capable and complex. It can also handle property placeholders, and a lot more besides.

So basically what is happening is we are telling Spring to first interpret myapp.temp as property placeholder syntax by using the ${} syntax. We then use : instead of ?: (which is called the Elvis operator) since the elvis operator only applies to Spring Expression Language expressions, not property placeholder syntax. The third part of our statement is #{systemProperties['java.io.tmpdir']} which is telling Spring to interpret the next expression as a Spring Expression and allows us to get system properties.

like image 94
Ian Dallas Avatar answered Oct 24 '22 07:10

Ian Dallas