Spring Expression Language condtional application properties - java

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.

Related

Not able to pass config values in #HystrixProperty annotation

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 ?

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;

Passing a variable to #Value annotation for reading specific property from properties file

In my Spring boot application, a specific value needs to be read from the properties file depending on a string value returned from another function. My properties file is as follows:
A=value_a
B=value_b
A function returns either A or B, and stores it in a String variable called stringValue . I am looking for doing something along the lines of the following:
#Value(stringValue)
String propertyValue
However, I get the following message on my IDE:
Attribute value must be constant
I have tried to convert stringValue to a static final variable, but to no avail.
I do know that I can pass a specific key to be read from the properties file, such as the following:
#Value("${A}")
String valueOfA
My question is whether I can pass a variable to the #Value annotation?
#Value annotations are resolved during startup, when Spring context is built. Since stringValue would not be available at this time, you can't use it for injection purposes.
An exception to this scenario would be if the bean with #Value annotation is prototype-scoped, in which case a new instance of it would be created any time it's requested. Still, stringValue would need to be available to the Spring context in order to be used at injection point.
Without seeing more code, it's not possible to give you any more detailed answer.
You can autowire Environment in your application and use it to read from the properties file as it is supposed to be at runtime.( This is assuming all files where your properties are have been added to environment).
I will be posting my answer with assumptions as you have not updated your question, with all information I requested.
So your code would be-
lets call your class where your making a call to a function called getValue to get value of stringValue- Example. Now lets assume you are making a call to the function getValue in a method in the class Example called doSomething().
class Example{
#Autowire
private Enviornment environment.
private String propertyValue
public void doSomething(){
String value=getValue()// this is where u know whether
its A or B.
propertyValue=environment.getProperty(value);
// do whatever u want know
}
Thanks for the help guys! I read the values from the properties file into a org.springframework.core.io.Resource object, and then used that to retrieve the specific value that I required. The following was how I structured my solution code:
#Value("classpath:config.properties")
private Resource propertiesfile;
I then declared a java.util.Properties object and read the values from the Resource object into it.
Properties properties = new Properties();
try{
properties.load(propertiesfile.getInputStream());
}catch(IOException e){
logger.error("Parsing error while reading properties file",e.toString());
}
Finally, based on the value in my stringValue variable, I read the corresponding value from the properties file
String propertyValue = properties.getProperty(stringValue);
if your variable is another environment variable you can try in this format.
#Value("${${stringvalue}}")
where stringvalue is the environment variable.

Reading empty string from consul using spring-cloud-consul

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.

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