Spring Boot - autoconfigure if any property with prefix exists - spring-boot

I'm trying to register an autoconfiguration with #ConditionalOnProperty. This configuration should be created when there is any property with a predefined prefix.
For example, this configuration should be created when there is one of the properties listed below
test.property.any-text-goes-here.some-nested-property=test
test.property.nested.nested-two=another
I've tried to use #ConditionalOnProperty with name=test.property, but it doesn't seem to be working. Maybe this can be sorted out using #ConditionalOnExpression, but I have no idea how SpEL expression should look like.
I'm wondering if there a way to achieve this without the need of implementing custom Condition.
Thanks for your help!

Unfortunately, it is not possible. #ConditionalOnProperty evaluates and compares only final property values in flat structure. It does not work on objects :(

Related

Reading a configuration Value from YAML in Micronaut

How to read a value from application.yml in my Micronaut project? I can clearly see annotation is resolved to proper value (true in this case), but it is not applied to the variable (stays as default false). I've tried using #Value and #ConfigurationProperties
In a comment the OP has indicated that they are doing new FeatureToggleService(). Creating your own instance of the object is the problem. Instead of using new, let the DI container create and manage the instance. If you do, then #Value will be relevant.
See https://github.com/jeffbrown/filiard/blob/f6f704fb95d7821919748bb41968f87d11cee07b/src/main/java/filiard/DemoController.java and https://github.com/jeffbrown/filiard/blob/f6f704fb95d7821919748bb41968f87d11cee07b/src/main/java/filiard/FlagHelper.java for a working example.
UPDATE:
Based on additional information this is not the correct answer!!!
As pointed out, #Value can be private, but Micronaut advices against it.
Short answer, it is because it is private. Wrong
From the documentation:
The #Value annotation accepts a string that can have embedded placeholder values (the default value can be provided by specifying a
value after the colon : character). Also try to avoid setting the
member visibility to private, since this requires Micronaut Framework
to use reflection. Prefer to use protected.
Also, consider using #Property instead of #Value. Still valid
https://docs.micronaut.io/latest/guide/#valueAnnotation
NOTE:
The Micronaut framework does not inspect a manually created instance, even if it is instantiated in a #Factory, unlike other frameworks.

Difference between #Value and #ConfigurationProperties?

Difference between #Value and #ConfigurationProperties?
In which scenario should I use #Value or #ConfigurationProperties?
#RefreshScope will refresh properties value for both?
I was questioning the same, to myself, and I found a concrete reason and why is better to use ConfigurationProperties over Value.
The main reason is that you could end with some text in the code forcing you to do full-text search to understand where a Value property is used.
You can find the whole explanation in in this lightweith reading: https://tuhrig.de/why-using-springs-value-annotation-is-bad/
Insteead of using #Value annotation multiple time to read value from properties file .we can use #configurationProperties one time

How to load a bean only if the property value is other than a fixed value

I am facing a situation in which a bean should be created by the application context only if the property contains any other value than a specific value.
i.e... one property file.path= /specific/path, If the value is other than this then bean should be loaded.
I can see that there is #ConditionalOnProperty (name="file.path", havingValue="....") out of the box but in my case, I am looking a property like havingValueOtherThan="..." or similar kind of property or annotation if it is there out of the box in the spring.
There are many possible options, besides profiles that were stated in comments, here are 2 options:
Option 1
Use #ConditionalOnExpression with any SPeL expression you wish
Option 2
You can always create your own conditional annotation with any logic:
Create an annotation that will depict your own business case with a #Conditional on it.
Then Implement the conditional logic of your choice. Here is an example of achieving this.
BTW, the profiles that mentioned in comments are using Conditionals engine under the hood (The #Profile annotation has a #Conditional({ProfileCondition.class}) in its definition since Spring 4.x)

Reusing Spring RequestMapping parsing functionality

I have some properties like
/my/{custom}/url
I would need to replace {custom} with some value at runtime
I know that Spring is using "#RequestMapping" with a similar syntax for #PathAttribute matching.
I'm wondering if there is some Class I can reuse from Spring to achieve what I need.
A good option for this is to use a UriComponentsBuilder - see reference here: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/util/UriComponentsBuilder.html
UriComponentsBuilder.fromPath("/test/{one}/{two}").buildAndExpand(map).toUriString()

Spring 3 Field Formatting

I am looking at using the Spring's Field formatting in particular the existing DateFormatter. I do understand that I need to specify a pattern on an annotation in my POJO.
Instead of hard coding the pattern I need to be able to provide it dynamically, I know this is not feasible with annotations. To properly support internationalization I would need to look up a pattern from a properties file before passing it to a Formatter.
Can anyone suggest an approach I can take?
Not sure however you may try implementing InitializingBean or init-method and set the values dynamically.
like suggested in spring forum for cron expression.

Resources