@ConditionalOnExpression if property is presentSort ArrayList of custom Objects by propertyCreate the perfect JPA entityDealing with “Xerces hell” in Java/Maven?Spring Boot @ConditionalOnProperty behaviorZuul router with Spring application.yml routes won't work as expectedHow can I get the keys from application.properties within other properties files served by Spring Boot Config Server?Prefix for nested configuration properties in springSpring boot read array from YAML (properties) fileConfigurationProperties binding a property name with a number in from a yaml file@GetMapping with array parameters by application.yml

Boss making me feel guilty for leaving the company at the end of my internship

How many times to repeat an event with known probability before it has occurred a number of times

Background for black and white chart

My players want to use called-shots on Strahd

How many possible starting positions are uniquely solvable for a nonogram puzzle?

For Saintsbury, which English novelists constituted the "great quartet of the mid-eighteenth century"?

What is the difference between state-based effects and effects on the stack?

Interview was just a one hour panel. Got an offer the next day; do I accept or is this a red flag?

Are athletes' college degrees discounted by employers and graduate school admissions?

Will users know a CardView is clickable

How to make a villain when your PCs are villains?

Does WiFi affect the quality of images downloaded from the internet?

Why is gun control associated with the socially liberal Democratic party?

How would Japanese people react to someone refusing to say “itadakimasu” for religious reasons?

Bullying by school - Submitted PhD thesis but not allowed to proceed to viva until change to new supervisor

SQL Server has encountered occurences of I/O requests taking longer than 15 seconds

Digital signature that is only verifiable by one specific person

How can Caller ID be faked?

Does an African-American baby born in Youngstown, Ohio have a higher infant mortality rate than a baby born in Iran?

Someone who is granted access to information but not expected to read it

Does anyone recognize these rockets, and their location?

Sakkāya-Ditthi and Self-View

How to test soql with For Update statement

Does PC weight have a mechanical effect?



@ConditionalOnExpression if property is present


Sort ArrayList of custom Objects by propertyCreate the perfect JPA entityDealing with “Xerces hell” in Java/Maven?Spring Boot @ConditionalOnProperty behaviorZuul router with Spring application.yml routes won't work as expectedHow can I get the keys from application.properties within other properties files served by Spring Boot Config Server?Prefix for nested configuration properties in springSpring boot read array from YAML (properties) fileConfigurationProperties binding a property name with a number in from a yaml file@GetMapping with array parameters by application.yml






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I have application.yml that has a property and it takes an array/list like this



tokens:
token:
- name: one
site: chicago
token:
- name: two
site: 'st. louis'


How do I use @ConditionalOnExpression such that just the presence of tokens in application.yml suffice?



I have tried @ConditionalOnProperty("tokens") and @CondtionalOnProperty(prefix="tokens", value= "token". It does not work. Even the official doc suggests that with lists/arrays it won't work great.










share|improve this question
























  • Why would you want to do that? It will be hard to maintain a property which is either present or absent. Programmer will have to go through the code to figure out what property to add for enabling. It would be better to have a second boolean property tokenPresent.

    – Aditya Gupta
    Mar 25 at 3:28












  • This is a very good advise. The trouble is, I am developing an application that will be added as a dependency in other projects. I don't want them to add unnecessary properties. You know what I mean?

    – Faraz Durrani
    Mar 25 at 3:32











  • Yeah. I did something like this few months back. Will add an answer now.

    – Aditya Gupta
    Mar 25 at 3:36

















1















I have application.yml that has a property and it takes an array/list like this



tokens:
token:
- name: one
site: chicago
token:
- name: two
site: 'st. louis'


How do I use @ConditionalOnExpression such that just the presence of tokens in application.yml suffice?



I have tried @ConditionalOnProperty("tokens") and @CondtionalOnProperty(prefix="tokens", value= "token". It does not work. Even the official doc suggests that with lists/arrays it won't work great.










share|improve this question
























  • Why would you want to do that? It will be hard to maintain a property which is either present or absent. Programmer will have to go through the code to figure out what property to add for enabling. It would be better to have a second boolean property tokenPresent.

    – Aditya Gupta
    Mar 25 at 3:28












  • This is a very good advise. The trouble is, I am developing an application that will be added as a dependency in other projects. I don't want them to add unnecessary properties. You know what I mean?

    – Faraz Durrani
    Mar 25 at 3:32











  • Yeah. I did something like this few months back. Will add an answer now.

    – Aditya Gupta
    Mar 25 at 3:36













1












1








1








I have application.yml that has a property and it takes an array/list like this



tokens:
token:
- name: one
site: chicago
token:
- name: two
site: 'st. louis'


How do I use @ConditionalOnExpression such that just the presence of tokens in application.yml suffice?



I have tried @ConditionalOnProperty("tokens") and @CondtionalOnProperty(prefix="tokens", value= "token". It does not work. Even the official doc suggests that with lists/arrays it won't work great.










share|improve this question
















I have application.yml that has a property and it takes an array/list like this



tokens:
token:
- name: one
site: chicago
token:
- name: two
site: 'st. louis'


How do I use @ConditionalOnExpression such that just the presence of tokens in application.yml suffice?



I have tried @ConditionalOnProperty("tokens") and @CondtionalOnProperty(prefix="tokens", value= "token". It does not work. Even the official doc suggests that with lists/arrays it won't work great.







java spring spring-boot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 7 at 3:54









Aditya Gupta

1,02721322




1,02721322










asked Mar 25 at 2:46









Faraz DurraniFaraz Durrani

2,93421137




2,93421137












  • Why would you want to do that? It will be hard to maintain a property which is either present or absent. Programmer will have to go through the code to figure out what property to add for enabling. It would be better to have a second boolean property tokenPresent.

    – Aditya Gupta
    Mar 25 at 3:28












  • This is a very good advise. The trouble is, I am developing an application that will be added as a dependency in other projects. I don't want them to add unnecessary properties. You know what I mean?

    – Faraz Durrani
    Mar 25 at 3:32











  • Yeah. I did something like this few months back. Will add an answer now.

    – Aditya Gupta
    Mar 25 at 3:36

















  • Why would you want to do that? It will be hard to maintain a property which is either present or absent. Programmer will have to go through the code to figure out what property to add for enabling. It would be better to have a second boolean property tokenPresent.

    – Aditya Gupta
    Mar 25 at 3:28












  • This is a very good advise. The trouble is, I am developing an application that will be added as a dependency in other projects. I don't want them to add unnecessary properties. You know what I mean?

    – Faraz Durrani
    Mar 25 at 3:32











  • Yeah. I did something like this few months back. Will add an answer now.

    – Aditya Gupta
    Mar 25 at 3:36
















Why would you want to do that? It will be hard to maintain a property which is either present or absent. Programmer will have to go through the code to figure out what property to add for enabling. It would be better to have a second boolean property tokenPresent.

– Aditya Gupta
Mar 25 at 3:28






Why would you want to do that? It will be hard to maintain a property which is either present or absent. Programmer will have to go through the code to figure out what property to add for enabling. It would be better to have a second boolean property tokenPresent.

– Aditya Gupta
Mar 25 at 3:28














This is a very good advise. The trouble is, I am developing an application that will be added as a dependency in other projects. I don't want them to add unnecessary properties. You know what I mean?

– Faraz Durrani
Mar 25 at 3:32





This is a very good advise. The trouble is, I am developing an application that will be added as a dependency in other projects. I don't want them to add unnecessary properties. You know what I mean?

– Faraz Durrani
Mar 25 at 3:32













Yeah. I did something like this few months back. Will add an answer now.

– Aditya Gupta
Mar 25 at 3:36





Yeah. I did something like this few months back. Will add an answer now.

– Aditya Gupta
Mar 25 at 3:36












2 Answers
2






active

oldest

votes


















0














You can use @Conditional(YourCondition.class)



Quoating from docs:




A single condition that must be matched in order for a component to be
registered. Conditions are checked immediately before the
bean-definition is due to be registered and are free to veto
registration based on any criteria that can be determined at that
point.




Illustration:



@Conditional(MyCondition.class)
@Controller
public class Box
...



public class MyCondition implements Condition

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata)
Environment env = context.getEnvironment();
return null == env.getProperty("tokens.token[0].name");




Here, en.getProperty will return null in case of missing property.






share|improve this answer

























  • In theory, ConditionalOnProperty would accomplish this.

    – Makoto
    Mar 25 at 4:18











  • Yeah, but here you can write your logic inside MyCondition class.

    – Aditya Gupta
    Mar 25 at 5:02












  • @AdityaGupta ty. i edited you answer a bit. If I ask for env.getProperty("tokens"), it always returns null. I have to give full property only then it actually returns a value instead of null.

    – Faraz Durrani
    Mar 25 at 5:57












  • Looks good. Thanks for updating.

    – Aditya Gupta
    Mar 25 at 17:42


















-1















@Faraz Durrani try this concept you will get...




@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "tokens")
public class ServiceClass

private String token;
...






share|improve this answer


















  • 1





    No Kumar this is totally off. Sorry.

    – Faraz Durrani
    Mar 25 at 6:00











  • i done a sample on this and it's working for me ...better to try this....

    – kumar
    Mar 25 at 9:27











  • yes I understand kumar. But my question is different. I think you misunderstood the question.

    – Faraz Durrani
    Mar 26 at 2:43











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55330646%2fconditionalonexpression-if-property-is-present%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can use @Conditional(YourCondition.class)



Quoating from docs:




A single condition that must be matched in order for a component to be
registered. Conditions are checked immediately before the
bean-definition is due to be registered and are free to veto
registration based on any criteria that can be determined at that
point.




Illustration:



@Conditional(MyCondition.class)
@Controller
public class Box
...



public class MyCondition implements Condition

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata)
Environment env = context.getEnvironment();
return null == env.getProperty("tokens.token[0].name");




Here, en.getProperty will return null in case of missing property.






share|improve this answer

























  • In theory, ConditionalOnProperty would accomplish this.

    – Makoto
    Mar 25 at 4:18











  • Yeah, but here you can write your logic inside MyCondition class.

    – Aditya Gupta
    Mar 25 at 5:02












  • @AdityaGupta ty. i edited you answer a bit. If I ask for env.getProperty("tokens"), it always returns null. I have to give full property only then it actually returns a value instead of null.

    – Faraz Durrani
    Mar 25 at 5:57












  • Looks good. Thanks for updating.

    – Aditya Gupta
    Mar 25 at 17:42















0














You can use @Conditional(YourCondition.class)



Quoating from docs:




A single condition that must be matched in order for a component to be
registered. Conditions are checked immediately before the
bean-definition is due to be registered and are free to veto
registration based on any criteria that can be determined at that
point.




Illustration:



@Conditional(MyCondition.class)
@Controller
public class Box
...



public class MyCondition implements Condition

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata)
Environment env = context.getEnvironment();
return null == env.getProperty("tokens.token[0].name");




Here, en.getProperty will return null in case of missing property.






share|improve this answer

























  • In theory, ConditionalOnProperty would accomplish this.

    – Makoto
    Mar 25 at 4:18











  • Yeah, but here you can write your logic inside MyCondition class.

    – Aditya Gupta
    Mar 25 at 5:02












  • @AdityaGupta ty. i edited you answer a bit. If I ask for env.getProperty("tokens"), it always returns null. I have to give full property only then it actually returns a value instead of null.

    – Faraz Durrani
    Mar 25 at 5:57












  • Looks good. Thanks for updating.

    – Aditya Gupta
    Mar 25 at 17:42













0












0








0







You can use @Conditional(YourCondition.class)



Quoating from docs:




A single condition that must be matched in order for a component to be
registered. Conditions are checked immediately before the
bean-definition is due to be registered and are free to veto
registration based on any criteria that can be determined at that
point.




Illustration:



@Conditional(MyCondition.class)
@Controller
public class Box
...



public class MyCondition implements Condition

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata)
Environment env = context.getEnvironment();
return null == env.getProperty("tokens.token[0].name");




Here, en.getProperty will return null in case of missing property.






share|improve this answer















You can use @Conditional(YourCondition.class)



Quoating from docs:




A single condition that must be matched in order for a component to be
registered. Conditions are checked immediately before the
bean-definition is due to be registered and are free to veto
registration based on any criteria that can be determined at that
point.




Illustration:



@Conditional(MyCondition.class)
@Controller
public class Box
...



public class MyCondition implements Condition

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata)
Environment env = context.getEnvironment();
return null == env.getProperty("tokens.token[0].name");




Here, en.getProperty will return null in case of missing property.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 25 at 5:58









Faraz Durrani

2,93421137




2,93421137










answered Mar 25 at 4:12









Aditya GuptaAditya Gupta

1,02721322




1,02721322












  • In theory, ConditionalOnProperty would accomplish this.

    – Makoto
    Mar 25 at 4:18











  • Yeah, but here you can write your logic inside MyCondition class.

    – Aditya Gupta
    Mar 25 at 5:02












  • @AdityaGupta ty. i edited you answer a bit. If I ask for env.getProperty("tokens"), it always returns null. I have to give full property only then it actually returns a value instead of null.

    – Faraz Durrani
    Mar 25 at 5:57












  • Looks good. Thanks for updating.

    – Aditya Gupta
    Mar 25 at 17:42

















  • In theory, ConditionalOnProperty would accomplish this.

    – Makoto
    Mar 25 at 4:18











  • Yeah, but here you can write your logic inside MyCondition class.

    – Aditya Gupta
    Mar 25 at 5:02












  • @AdityaGupta ty. i edited you answer a bit. If I ask for env.getProperty("tokens"), it always returns null. I have to give full property only then it actually returns a value instead of null.

    – Faraz Durrani
    Mar 25 at 5:57












  • Looks good. Thanks for updating.

    – Aditya Gupta
    Mar 25 at 17:42
















In theory, ConditionalOnProperty would accomplish this.

– Makoto
Mar 25 at 4:18





In theory, ConditionalOnProperty would accomplish this.

– Makoto
Mar 25 at 4:18













Yeah, but here you can write your logic inside MyCondition class.

– Aditya Gupta
Mar 25 at 5:02






Yeah, but here you can write your logic inside MyCondition class.

– Aditya Gupta
Mar 25 at 5:02














@AdityaGupta ty. i edited you answer a bit. If I ask for env.getProperty("tokens"), it always returns null. I have to give full property only then it actually returns a value instead of null.

– Faraz Durrani
Mar 25 at 5:57






@AdityaGupta ty. i edited you answer a bit. If I ask for env.getProperty("tokens"), it always returns null. I have to give full property only then it actually returns a value instead of null.

– Faraz Durrani
Mar 25 at 5:57














Looks good. Thanks for updating.

– Aditya Gupta
Mar 25 at 17:42





Looks good. Thanks for updating.

– Aditya Gupta
Mar 25 at 17:42













-1















@Faraz Durrani try this concept you will get...




@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "tokens")
public class ServiceClass

private String token;
...






share|improve this answer


















  • 1





    No Kumar this is totally off. Sorry.

    – Faraz Durrani
    Mar 25 at 6:00











  • i done a sample on this and it's working for me ...better to try this....

    – kumar
    Mar 25 at 9:27











  • yes I understand kumar. But my question is different. I think you misunderstood the question.

    – Faraz Durrani
    Mar 26 at 2:43















-1















@Faraz Durrani try this concept you will get...




@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "tokens")
public class ServiceClass

private String token;
...






share|improve this answer


















  • 1





    No Kumar this is totally off. Sorry.

    – Faraz Durrani
    Mar 25 at 6:00











  • i done a sample on this and it's working for me ...better to try this....

    – kumar
    Mar 25 at 9:27











  • yes I understand kumar. But my question is different. I think you misunderstood the question.

    – Faraz Durrani
    Mar 26 at 2:43













-1












-1








-1








@Faraz Durrani try this concept you will get...




@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "tokens")
public class ServiceClass

private String token;
...






share|improve this answer














@Faraz Durrani try this concept you will get...




@Component
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "tokens")
public class ServiceClass

private String token;
...







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 25 at 4:26









kumarkumar

274110




274110







  • 1





    No Kumar this is totally off. Sorry.

    – Faraz Durrani
    Mar 25 at 6:00











  • i done a sample on this and it's working for me ...better to try this....

    – kumar
    Mar 25 at 9:27











  • yes I understand kumar. But my question is different. I think you misunderstood the question.

    – Faraz Durrani
    Mar 26 at 2:43












  • 1





    No Kumar this is totally off. Sorry.

    – Faraz Durrani
    Mar 25 at 6:00











  • i done a sample on this and it's working for me ...better to try this....

    – kumar
    Mar 25 at 9:27











  • yes I understand kumar. But my question is different. I think you misunderstood the question.

    – Faraz Durrani
    Mar 26 at 2:43







1




1





No Kumar this is totally off. Sorry.

– Faraz Durrani
Mar 25 at 6:00





No Kumar this is totally off. Sorry.

– Faraz Durrani
Mar 25 at 6:00













i done a sample on this and it's working for me ...better to try this....

– kumar
Mar 25 at 9:27





i done a sample on this and it's working for me ...better to try this....

– kumar
Mar 25 at 9:27













yes I understand kumar. But my question is different. I think you misunderstood the question.

– Faraz Durrani
Mar 26 at 2:43





yes I understand kumar. But my question is different. I think you misunderstood the question.

– Faraz Durrani
Mar 26 at 2:43

















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55330646%2fconditionalonexpression-if-property-is-present%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript