Spring config string format - spring

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="\<.*?>" />

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.*.)

Call a jstl variable inside loop [duplicate]

I have a Map in EL as ${map} and I am trying to get the value of it using a key which is by itself also an EL variable ${key} with the value "1000".
Using ${map["1000"]} works, but ${map["$key"]} does not work. What am I doing wrong and how can I get the Map value using a variable as key?
$ is not the start of a variable name, it indicates the start of an expression. You should use ${map[key]} to access the property key in map map.
You can try it on a page with a GET parameter, using the following query string for example ?whatEver=something
<c:set var="myParam" value="whatEver"/>
whatEver: <c:out value="${param[myParam]}"/>
This will output:
whatEver: something
See: https://stackoverflow.com/tags/el/info and scroll to the section "Brace notation".
I have faced this issue before. This typically happens when the key is not a String. The fix is to cast the key to a String before using the key to get a value from the map
Something like this:
<c:set var="keyString">${someKeyThatIsNotString}</c:set>
<c:out value="${map[keyString]}"/>
Hope that helps
You can put the key-value in a map on Java side and access the same using JSTL on JSP page as below:
Prior java 1.7:
Map<String, String> map = new HashMap<String, String>();
map.put("key","value");
Java 1.7 and above:
Map<String, String> map = new HashMap<>();
map.put("key","value");
JSP Snippet:
<c:out value="${map['key']}"/>
My five cents. Now I am working with EL 3.0 (jakarta impl) and I can access map value using three ways:
1. ${map.someKey}
2. ${map['someKey']}
3. ${map[someVar]} //if someVar == 'someKey'
I think that you should access your map something like:
${map.key}
and check some tutorials about jstl like 1 and 2 (a little bit outdated, but still functional)

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

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 ''.

Ternary operator in Spring

My question is similar to this question. Since that question is quite old so thought of posting new question.
I am also writing my expression in following
<property name="to" value="#{ systemProperties['BR']} == '01' ?
${PROPERTY_VALUE_1_FROM_BUNDLE} :
${PROPERTY_VALUE_2_FROM_BUNDLE}" />
When I fetch the value of "to" variable from my bean. Its giving me something like below
01='01'? value1 : value2
Its not parsing my expression in XML itself.
Am I doing anything wrong here?
You are terminating the SpEL too early; it should be...
<property name="to" value="#{ systemProperties['BR'] == '01' ?
'${PROPERTY_VALUE_1_FROM_BUNDLE}' :
'${PROPERTY_VALUE_2_FROM_BUNDLE}' }" />
Note that you also need single quotes around the placeholders so the resolved values are treated as literals.
I have resolved it using below code
ExpressionParser parser = new SpelExpressionParser();
String toMail = parser.parseExpression(to).getValue(String.class);
Had to make little here and there in XML but its responding as I wanted it to. Now I am getting either value in my "to" variable.

Resources