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


in spring 4, using @value annotation, right way specify system property default if specified property not exists?

whereas works no-default case:

@value("${myapp.temp}") private string tempdirectory; 

this doesn't work when need default:

@value("#{myapp.temp ?: systemproperties.java.io.tmpdir}") private string tempdirectory; 

nor this:

@value("#{myapp.temp ?: systemproperties(java.io.tmpdir)}") private string tempdirectory; 

both of these give me exception @ time spring trying create bean:

org.springframework.beans.factory.beancreationexception: error creating bean        name 'configurationservice': invocation of init method failed;      nested exception java.lang.nullpointerexception 

can done?

i tried following , worked me:

@value("${myapp.temp:#{systemproperties['java.io.tmpdir']}}") private string tempdirectory; 

the missing parts believe not using ?: , needing #{}. according answer:

${...} property placeholder syntax. can used dereference properties.

#{...} spel syntax, far more capable , complex. can handle property placeholders, , lot more besides.

so happening telling spring first interpret myapp.temp property placeholder syntax using ${} syntax. use : instead of ?: (which called elvis operator) since elvis operator applies spring expression language expressions, not property placeholder syntax. third part of our statement #{systemproperties['java.io.tmpdir']} telling spring interpret next expression spring expression , allows system properties.


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -