How to concatenate two beans lines in a configuration file in Spring 2.5

I know that Spring 3.0 and above has an EL value, but in this case the project uses Spring 2.5, an example:

<bean id="dir" class="java.lang.String"> <constructor-arg value="c:/work/" </bean> <bean id="file" class="java.lang.String"> <constructor-arg value="file.properties" /> </bean> <bean id="path" class="java.lang.String"> <constructor-arg value=**dir + file**/> </bean> 
+4
source share
4 answers

It works?

 <bean id="dir" class="java.lang.String"> <constructor-arg value="c:/work/" </bean> <bean id="file" class="java.lang.String"> <constructor-arg value="file.properties" /> </bean> <bean id="path" factory-bean="dir" factory-method="concat"> <constructor-arg ref="file"/> </bean> 

Note the use of the String.concat(java.lang.String) method String.concat(java.lang.String) as a factory-method .

But XML is actually not the best place for such things, how about Java @Configuration ?

 @Configuration public class Cfg { @Bean public String dir() { return "c:/work/"; } @Bean public String file() { return "file.properties"; } @Bean public String path() { return dir() + file(); } } 
+6
source

Spring is not a place to store application logic like this.

If you need it, add a getPath() method that will do something like:

 public String getPath() { if(path == null) { path = getDir() + getPath(); } return path; } 
+2
source

Have you tried using Spring -EL? Take a look at LINK

With Spring-EL, you can do something like the following:

 <bean id="dir" class="java.lang.String"> <property name="path" value="c:/work/" /> </bean> <bean id="file" class="java.lang.String"> <property name="fileName" value="file.properties" /> </bean> <bean id="path" class="java.lang.String"> <property name="initialShapeSeed" value="#{dir.path}#{file.fileName}"/> </bean> 

It is available with Spring 3.0.

0
source

You can use ref instead of value. I.e.

 <bean id="path" class="java.lang.String"> <constructor-arg ref="file"/> </bean> 
0
source

All Articles