Concatenate string in spring xml configuration

rouble picture rouble · Nov 22, 2011 · Viewed 18.3k times · Source

I need to concatenate the string value of a spring bean, to an existing string, and then set it as an attribute of another bean:

<bean id="inet" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass"><value>java.net.InetAddress</value></property>
    <property name="targetMethod"><value>getLocalHost</value></property>
</bean>
<bean id="host" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject"><ref local="inet"/></property>
    <property name="targetMethod"><value>getHostName</value></property>
</bean>

At this point, I have the hostname, in the 'host' bean. I now need to concatenate it and pass it to the publishedEndpointUrl attribute. Something like this:

<jaxws:endpoint 
    id="foo"
    publishedEndpointUrl= "http://" + host + "/Foo" 
    implementor="com.example.v1.foo"
    address="/v1/Foo"/>

How is this done using spring xml configuration?

Answer

beny23 picture beny23 · Nov 23, 2011

You could use Spring-EL and factory-method:

<bean id="localhost" class="java.net.InetAddress" factory-method="getLocalHost" />

<bean id="publishedUrl" class="java.lang.String">
    <constructor-arg value="#{'http://' + localhost.hostName + '/Foo'}" />
</bean>

<jaxws:endpoint
   ...
   publishedEndpointUrl="#publishedUrl"
   ...

EDIT:

The jaxws:endpoint tag appears to be able to reference bean values by using the #beanId notation but does not like Spring-EL. So by constructing a String bean, we get around this and it still looks fairly neat.