Spring propery placeholder not working as expected if defined twice - spring

I have defined 2 property placeholder conf with same order .Behaviour what i was found is that if i define a property in the 2nd file with default value it is not picking from the file.if i didnot specify the default value it is taking from the file.can anybody help.``
1)
config.properties
userName=Jithin
2)
configtext.properties
charlength=256
Program:
#Value("${charlength:128}")
private String charLenght;
If i specify the default values as 128, charLenth field is assigned with 128
If i am not specifying the default value. ie:
#Value("${charlength}")
private String charLenght; In this case, it is reading from the property file and assigned with 256

Related

Is there a way to configure 2 property system in same #Value ins spring boot?

I trying to use 2 different property files with the same parameters which every parameter is describing the same property for example:
NewsPaperConsumer.properties, MarketConsumer.properties when every file have the same parameters.
My aim is to use the separated way to make the configuration files more readable but at the progromatic side union them to one hash map for example:
NewPaperConsumer and MarketConsumer have the parameter serverAddress so I'll get it by:
#Value("${serverAddress}")
private HashMap<String,String> serverAdresses;
how I change the way the system property save the parameter (instead of assign the value to string that It will assign it to hash map - {"key" : "value }

How to access field value in a JSR message with spring boot 2?

I would like to customize my error message in the following way:
Assume following declaration of a class Person:
#Size(min=10, max=250, message="{size.name}")
private String name;
Within the declared error message in ValidationMessages.properties I would like to output the field value as well, i.e. I would like to do something like this:
size.name = The name '${validatedvalue}' is invalid, its size must be between {min} and {max}
Assume the content of the field 'name' is “xyz”. Then the error message should look like this:
The name 'xyz' is invalid, its size must be between 10 and 250
The substitution for min and max works, but filed value i am getting as '' ,how can I do this for the field value?

How to load value with dynamic key place holder in spring batch?

Spring Batch: How to get value from property file if key is generated dynamically from input parameter
propertyfile content:
my.table.book.bic.code=11111
my.table.news.bic.code=22222
Spring batch configuration
< property name="bicCodeValue" value="#{jobParameters['inputTable'] + '.bic.code'}" />
Where inputTable is input parameter for batch
inputTable = my.table.book
inputTable = my.table.news
I am not getting value from property file instead of value from property file I am getting only key in code "my.table.book.bic.code".
I need to update only in xml file like
< property name="bicCodeValue" value="#{jobParameters['inputTable'] + '.bic.code'}" / >
But this is not working.
You could inject an instance of
#Autowired
private Environment env;
and then do something like:
env.getProperty("my.table."+inputTable+".book.bic.code")
According to your properties, the SpEL expression jobParameters['inputTable'] should return a String value, so you can try to use the concat method in your expression:
<property name="bicCodeValue" value="#{jobParameters['inputTable'].concat('.bic.code')}" />
For more details about SpEL, please see here: https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#expressions

FreeMarker Java 8 Optional value support

Does FreeMarker support Optional value in Java 8?
e.g. I have String id, and its getter method is like:
public Optional<String> getId() {
return Optional.ofNullable(Id);
}
How am I going to reference it in the .ftl file. It seems that {data.id} can not find the correct Optional value. But gives me Optional[1334586]
Well, Freemarker is not supposed to be aware of Optional or it is better to say that its dynamically typed so it works for any object.
Since you calling ${data.id} it's just calls toString on Optional which is totally expected behavior.
If you want to handle null values in your template and for that you want to use Optional, you may choose to set a default value if null, so Optional usage won't be needed:
Synopsis: unsafe_expr!default_expr or unsafe_expr! or (unsafe_expr)!default_expr or (unsafe_expr)!
Example: ${data.id!"No id."}
Or check if it's exists:
<#if data?? && data.id??>
Id found
<#else>
Id not found
</#if>
For more info check out the Freemarker docs. Specifically parts: Handling missing values and Missing value test operator.
If you just want to get the value from Optional in your template:
${data.id.orElse('')}
or
${data.id.get()!''}

How to set default context-param values for ServletContextPropertyPlaceholderConfigurer?

How to set default boolean values with ServletContextPropertyPlaceholderConfigurer? For example, I want property to have false value if it is not set as <context-param>
you can always define a default value for every place where the property configurere "inject" some value.
The default synax is ${propertyName:default}
So for example:
${myContextParam:false}

Resources