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

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?

-------------Problems Reply------------

I tried the following and it worked for me:

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.

Try systemProperties['java.io.tmpdir'].

It's a map, so if the key has a dot in the name, you should use [..]

Category:java Views:87 Time:2019-03-22
Tags: java spring

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.099 (s). 11 q(s)