I am trying to set variiable throw SpEL in spring boot application:
#Value("${data.jndi-name:#{null}}")
private String jndiDataSource;
data.jndi-name should comes from application-{profile}.properties. But field jndiDataSource always null even if data.jndi-name exists.
Code with #Value("${data.jndi-name}") work fine but jndiDataSource contains empty string.
My question is how to set null to variable if property does not exists using SpEL.
upd: values comes from profile specific property file
try this:
#Value("${data.jndi-name:#null}")
private String jndiDataSource;
Related
I am trying to pass properties values in #HystrixProperty annotation like below
#HystrixProperty(name="circuitBreaker.requestVolumeThreshold", value="${requestVolumeThreshold}")
Also Tried this
#HystrixProperty(name="circuitBreaker.requestVolumeThreshold", value="#{${requestVolumeThreshold}}")
However its giving me exception "IllegalArgumentException"; bad property value
Where Am I going wrong ?
I'm trying to get a value from the application properties file with condtional. If property a doesn't exits take property b.
But I can't get it to work. I have read about "SPEL" but cannot really understand it. I have tried this
#Value("'${api.path}' ?: '${api.backuppath}'")
private String path;
This doesnt work because it needs value on both property to work. In my case the first property can be null or the second can be null.
In my class, I have 2 private fields that are pre-populated via #Value annotation. Although the value is getting read correctly from .properties file, its default value is never applied if in .properties file it's not set.
I have tried with creating PropertySourcesPlaceholderConfigurer bean and specifying location to the file: "classpath:application.properties". These are my properties:
#Value("${year:2019}")
private Integer year;
resources/application.properties:
year=
When the year is set, Integer year receives the right value. If it remains empty, I expect default value (2019) to be set, but it remains null.
Spring version: 5.1.8.RELEASE
It is null because your properties file sets it to blank. If you remove the year= from the properties file you should get the default value.
I'm migrating from file based .properties file to consul based configuration in my spring application. I'm using spring-cloud-consul. Earlier in my property file I had a property like following
test.key=
In spring application class corresponding field is like this
#Value("${test.key:defaultVal}")
private String testConsul;
In the runtime, the value of testConsul string is an empty string. But when using the consul, whenever I put key test.key without a value, in the runtime it gets resolved to a null.
Is there anyway I can pass an empty string value through consul ?
This is the work around we're using. use a default value which is not exist
${no.such.key:}
Hope this helps.
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?
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.
Try systemProperties['java.io.tmpdir'].
It's a map, so if the key has a dot in the name, you should use [..]
For me it only works with different property-names (being property.name.a a key with a value in my application.properties and property.name.b a Environment-Variable) like:
#Value("${property.name.a:${property.name.b}}")
The same names didn´t work for me like expected (loading the default, when the first property isn´t present), e.g.:
#Value("${property.name.a:${property.name.a}}")