Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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:

@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?

It appears my problem was using ${...} instead of #{...}. Here is my solution using two different properties:

bootstrap.properties

procmultiplier=${instance.procmultiplier:1}

AppConfig.java

@Configuration
public class AppConfig {
    @Value("#{${procmultiplier} == 0 ? 1 : ${procmultiplier}}")
    public Integer procMultiplier;

Command Line: mvn clean package && java -Dspring.profiles.active=json -Dinstance.procmultiplier=0 -jar target/MyApp-0.0.1-SNAPSHOT.jar

Also allows for a missing instance.procmultiplier parameter.

I still don't know how to do it all in one shot with 1 property though...

According to this post you should try

@Value("#{someBean.someProperty != null ? someBean.someProperty : 'default'}")
private String ternary;
                Additionally, checking for 'null' is not the same as checking for existence.  If I leave the property off the command line, I get a Could not resolve placeholder error.
– Brooks
                Dec 17, 2018 at 18:08
                You also can add the && someBean.someProperty != 0 condition. And if you want you can give a default value in the application.properties for that value
– aBnormaLz
                Dec 17, 2018 at 18:26
                It's still not a bean.  In order to resolve a property (as opposed to a bean field), you need ${...} inside.  Your answer is not correct, but did give me some much needed inspiration!
– Brooks
                Dec 17, 2018 at 18:28
                Actually that is also written in the post at '2. Operators' section. But my answer was half correct because of the #{...} thingy :D the other half is yours, so I gave you an upvote :)
– aBnormaLz
                Dec 17, 2018 at 18:29

This works for me in xml:

<bean id="myBean" class="myClass" >
    <property name="myProp" value="#{systemProperties['a.b'] ?: '${a.b}'}"/>
</bean>

This will take from system properties if exist, otherwise from property file

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.