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

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

Related

Spring boot how to append multiple properties to single value?

I have multiple properties in my application.properties file
prop.a=A
prop.b=B
prop.c=C
// and more
Now i have to add the property A to the rest of them. I am doing this like following
#Value("${prop.a}")
private String a;
#Value("${prop.b}")
private String b;
b = new StringBuffer(b).append(a).toString();
I have to individually append each string. Can i do this in the annotation? like #Value("${prop.b}" + "${prop.a}") ?
If you want to do this programmatically, you have to do this:
#Value( "${prop.a}${prop.b}" )
private String b;
You can, however, achieve this in application.properties itself this way:
prop.a=A
prop.b=${prop.a}B
prop.c=${prop.a}C
(Please note that wherever your example says prob.*,I have changed to prop.*.)

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 }

Spring SpEL: how to write ternary operation in XML config?

In my Spring XML config, I need to set a value to a specific property value depending on the value of another property.
I need something like this:
<bean id="myid" class="myclass">
<property name="myprop"
value="#{${property_a} == 'test-a' ? ${property_b} : 'anothervalue'}"
/>
I want myprop to be set the value of property_b if property_a is equal to "test-a", otherwise myprop must be set to "anothervalue".
property_a and property_b are both defined in my config.properties file.
Is it possible to write such a statement in XML SpEL?
<property name="myprop"
value="#{'${property_a}' == 'test-a' ? '${property_b}' : 'anothervalue' }" />
You have to ensure that the result of properties placeholder resolution is still literal. So, that's why we must wrap ${...} to ''.

Spring propery placeholder not working as expected if defined twice

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

Spring config string format

A stupid question on formatting but not able to figure out the correct negate parameter. I've the following string value which needs to be passed as a constructor arg through spring.
private final static String STRING_PATTERN = "\\<.*?>";
In spring config,
<bean class="com.test.TestBean">
<constructor-arg value="\\<.*?>" />
</bean>
As you can see, its not in the correct format. Can anyone please provide a clue ?
Thanks
Since Spring config is an XML file
there is no need to replace \ with \\ as in Java string literals
you need to replace < and > with < and >
So, you get
<constructor-arg value="\<.*?>" />

Resources