Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate strings within a Spring XML configuration file?

I have a String value in a Spring configuration file that comes to be as the result of a JNDI lookup -- it happens to be a path name:

<jee:jndi-lookup id="myAppHomeDir" jndi-name="myAppHomeDir" />

Now I need to concatenate on to the end of this path another string and hand it off to another Spring bean as follows (which of course doesn't work):

<bean id="LogPath" class="org.mystuff.initBean">
    <property name="logDirectory">
       <jee:jndi-lookup id="myAppHomeDir"
                 jndi-name="myAppHomeDir" /> + "/logs"
    </property>
</bean>

Is there a simple way to do this without me having to write a utility class in Java?

like image 796
HDave Avatar asked Nov 23 '10 21:11

HDave


People also ask

How do I combine multiple strings into one?

You can concatenate a list of strings into a single string with the string method, join() . Call the join() method from 'String to insert' and pass [List of strings] . If you use an empty string '' , [List of strings] is simply concatenated, and if you use a comma , , it makes a comma-delimited string.

What are the 2 methods used for string concatenation?

There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.

Can I import one XML into another XML in spring?

Just import the xml defining the bean with <import resource="otherXml. xml"> and you will be able to use the bean definition.


1 Answers

Try using Spring EL (expression language). I would try the following (not tested):

<jee:jndi-lookup id="myAppHomeDir" jndi-name="myAppHomeDir" />

<bean id="LogPath" class="org.mystuff.initBean">
    <property name="logDirectory" value="#{myAppHomeDir+'/logs'}"/>
</bean>

Not quite sure if it would work. The thing that troubles me is the cast from File (I guess) to String when concatenating. So if the previous one didn't work, I would try:

#{myAppHomeDir.canonicalPath+'/logs'}

Let us know if it works.

like image 165
Grzegorz Oledzki Avatar answered Oct 12 '22 11:10

Grzegorz Oledzki