java 在 Spring XML 配置文件中连接字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4261372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concatenate strings within a Spring XML configuration file?
提问by HDave
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:
我在 Spring 配置文件中有一个 String 值,它是 JNDI 查找的结果——它恰好是一个路径名:
<jee:jndi-lookup id="myAppHomeDir" jndi-name="myAppHomeDir" />
<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):
现在我需要将另一个字符串连接到此路径的末尾,并将其传递给另一个 Spring bean,如下所示(这当然不起作用):
<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?
有没有一种简单的方法可以做到这一点,而我不必用 Java 编写实用程序类?
回答by Grzegorz Oledzki
Try using Spring EL (expression language). I would try the following (not tested):
尝试使用Spring EL(表达式语言)。我会尝试以下(未测试):
<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:
不太确定它是否会起作用。困扰我的是在连接时从 File(我猜)到 String 的转换。所以如果前一个不起作用,我会尝试:
#{myAppHomeDir.canonicalPath+'/logs'}
Let us know if it works.
让我们知道它是否有效。