Not able to pass config values in #HystrixProperty annotation - java

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 ?

Related

Spring Expression Language condtional application properties

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.

Injecting map of key->values (variableName->system environment value) from application.properties to map-object with default values

In application.properties I have map like:
variable.map={variableName1: '${VARIABLE_VALUE_1}', variableName2: '${VARIABLE_VALUE_2}'}
VARIABLE_VALUE_1 and VARIABLE_VALUE_2 comes from system environment variables.
I can read it in my configuration class very simple with SpEL:
#Value("#{${variable.map}}")
private Map<String,String> variableMap;
Thus, I am getting two entries of map variableMap. But if one of them doesn't exsist I am getting an exception when application started:
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'VARIABLE_VALUE_1' in value "{variableName1: '${VARIABLE_VALUE_1}', variableName2: '${VARIABLE_VALUE_2}'}"
How can I handle this exception? For example by put variableName1 with any default value or by putting only entry variableName2 -> VARIABLE_VALUE_2 (so if system environment variable doesn't exsist -> ignore it) ?
If you use parameters in your value, it depends on them. So if you declare two parameters, you need both.
You could try to declare the whole map in one parameter, so you are more flexible (but I'm not sure if that works). And even if you don't have that parameter, you could declare to use null instead.
variable.map=${VARIABLE_VALUE:#{null}}
And declare your value as:
VARIABLE_VALUE = "{'variableName1': 'value1', 'variableName2': 'value2'}"

test failed when i use #Value [duplicate]

I am using sprin version 4.3.8.RELEASE. also i am using #Value to inject values from property file, if the properties are string that no problem, but if the property is Integer that is a problem (i know there is many questions about this i tried all the answers but the issue still exist)
The property is
CONNECTION.TIME.OUT=100000
First solution
#Value("${CONNECTION.TIME.OUT}")
protected Integer connectionTimeOut;
Ecxeption
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "${CONNECTION.TIME.OUT}"
Second solution
#Value("#{new Integer('${CONNECTION.TIME.OUT}')}")
protected Integer connectionTimeOut;
Exception
EL1003E: A problem occurred whilst attempting to construct an object of type 'Integer' using arguments '(java.lang.String)'
Third solution
#Value("#{new Integer.parseInteger('${CONNECTION.TIME.OUT}')}")
protected Integer connectionTimeOut;
Exception
EL1003E: A problem occurred whilst attempting to construct an object of type 'Integer' using arguments '(java.lang.String)'
any ideas why is that
To avoid such type of situation where the exception occurs due to un-availibilty of the property, Add default value in the tag. If property is not available then it will populate the default value
#Value("${CONNECTION.TIME.OUT:10}")
Your property file is probably not loaded properly.
When provided with no valid value for a property placeholder, Spring will automatically try to assign this value to the name of the #Value annotation. In your case, this:
#Value("#{new Integer('${CONNECTION.TIME.OUT}')}")
protected Integer connectionTimeOut;
Is interpreted as:
protected Integer connectionTimeOut = new Integer("${CONNECTION.TIME.OUT}");
Which, indeed, brings an error.
Try to either configure a PropertyPlaceholderConfigurer in your beans, or make sure that your property file is loaded properly in your classpath by your configuration. Something among the lines of:
<context:property-placeholder
ignore-unresolvable="true"
location="classpath:yourfile.properties" />
In your configuration file will help, in this case.
For #Value("${CONNECTION.TIME.OUT}") your error is java.lang.NumberFormatException: For input string: "${CONNECTION.TIME.OUT}". This means that expression was not processed resulting in Integer.parseInt("${CONNECTION.TIME.OUT}") which thrown the NumberFormatException.
Either there is no PropertyPlaceholderConfigurer bean registered in the Spring context and #Value annotations are not processed or there is no property CONNECTION.TIME.OUT defined.
Try removing single quotes worked ''. It worked for me.
#Value("#{new Integer(${CONNECTION.TIME.OUT})}")
Don't forget the "${}" around it! I kept looking at what should have been obvious and missing it.

Spring SpEL. How to set null as default value for field

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;

Java bean gets property name instead of value

I have a .properties file with bunch of properties in it. Here's an example:
mes.mail.debug=true
cookie.sso.domain = .stuffStuff.com
blabla.endpoint = blabla.com
test.value.property = myValue
The problem is with the last one (Which I have just added to the project we're working on).
I read the properties using #Value("${PropertyName}") annotation and it was working perfectly until lately, when I use the same thing, the variable gets the propertyName instead of its value:
#Value("${test.value.property}")
private String mProperty;
so, mProperty gets "test.value.property" where what I'm looking for is for it to get "myValue".
What's happening exactly? Is there something wrong with my project? I have tested in my friend's computer and it works perfectly.
By the way, i'm using Spring Tool Suite.
EDIT: It turns out that it doesn't detect the changes I make in the properties file. So if I change an old property's value; it acts as if nothing happened.
Does anyone has any idea why it's doing like this?
When you declare a Property Placeholder Configurer to load the properties files, you can set it to ignore unresolvable placeholders.
This means that if the property you are injecting with #Value is not found, its name (or key) will be assigned to the variable.
In your case, this option is enabled and the file that has been loaded by the application is not the one you are editing.
To see from where the file is been loaded, check the placeholder configurer location property.

Categories