Concatenate strings within a Spring XML configurat

2019-02-05 21:22发布

问题:

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?

回答1:

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.