Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

spring

I know that Spring 3.0 and up has EL, but in this case the project is using Spring 2.5 , 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>
like image 985
Leo Avatar asked Feb 20 '23 19:02

Leo


2 Answers

Does this work?

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

Notice the usage of String.concat(java.lang.String) method as factory-method.

But XML isn't really the best place for stuff like this, what 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();
    }
}
like image 101
Tomasz Nurkiewicz Avatar answered Mar 20 '23 16:03

Tomasz Nurkiewicz


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

If you need that, add a getPath() method that does something like:

public String getPath() {
    if(path == null) {
        path = getDir() + getPath();
    }
    return path;
}
like image 24
corsiKa Avatar answered Mar 20 '23 17:03

corsiKa