B
Brooks
Guest
Brooks Asks: SpEL (Spring) syntax for default value when property non-existant or equal to 0
I'm trying to set an Integer property to a default value of 1 if it either does not exist or if its current value is 0 and I can't for the life of me get the 0 comparison to work properly.
The original value (if set) will come in via the command line.
Here are the relevant line in my class:
I have tried numerous variations of the @Value annotation and at one point, I swear it recognized the 0 value, and I can't get back to that state. I was first trying to simply get it to recognize the 0 value and default to 1 and I was then going to try and insert a null-check.
Is there a way to do both all in the same annotation? One idea I had was to split the null-check and 0-check into two different properties, basically have
My command is:
The property ends up just being equal to whatever I set it to on the command line.
Any ideas on how to check for both non-existent and 0 and default to 1 if either is true, otherwise set to whatever comes in via command line?
I'm trying to set an Integer property to a default value of 1 if it either does not exist or if its current value is 0 and I can't for the life of me get the 0 comparison to work properly.
The original value (if set) will come in via the command line.
Here are the relevant line in my class:
Code:
@Configuration
public class AppConfig {
@Value("${${instance.procmultiplier} == 0 ? Integer.parseInt('1') : ${instance.procmultiplier}}")
public Integer procMultiplier;
}
I have tried numerous variations of the @Value annotation and at one point, I swear it recognized the 0 value, and I can't get back to that state. I was first trying to simply get it to recognize the 0 value and default to 1 and I was then going to try and insert a null-check.
Is there a way to do both all in the same annotation? One idea I had was to split the null-check and 0-check into two different properties, basically have
procmultiplier=${instance.procmultiplier:1}
in a properties file and then change the annotation to @Value("${${procmultiplier} == 0 ? Integer.parseInt('1') : ${procmultiplier}}")
but nothing I try works.My command is:
mvn clean package && java -Dspring.profiles.active=json -Dinstance.procmultiplier=0 -jar target/MyApp-0.0.1-SNAPSHOT.jar
.The property ends up just being equal to whatever I set it to on the command line.
Any ideas on how to check for both non-existent and 0 and default to 1 if either is true, otherwise set to whatever comes in via command line?
SolveForum.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, solveforum.