Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate string in spring xml configuration

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?

like image 807
rouble Avatar asked Nov 22 '11 22:11

rouble


4 Answers

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.

like image 133
beny23 Avatar answered Nov 18 '22 18:11

beny23


You need to look at PropertyPlaceholderConfigurer. This allows you define global properties, which can either come from a properties file, or in your case, you can define a default value, in which case it's just a global property. The following will work:

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName">
        <value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
    </property>
    <property name="properties">
        <props>
            <prop key="driver">jdbc.oracle.Driver</prop>
            <prop key="dbname">fred</prop>
        </props>
    </property>
    <property name="locations">
        <list>
            <value>file:properties/application.properties</value>
        </list>
    </property>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName"><value>${driver}</value></property>
  <property name="url"><value>jdbc:${dbname}</value></property>
</bean>

This means that you have default values for ${driver} and ${dbname}, which are used to define the data source. These values can be overridden in the application.properties file, or even as a -D option on the command line.

like image 21
Matthew Farwell Avatar answered Nov 18 '22 17:11

Matthew Farwell


As jaxws:* namespace does not like Spring EL, an alternative could be to declare an EndpointImpl bean, instead of the jaxws:endpoint object.

It is some more work, but as pointed out in http://cxf.apache.org/docs/jax-ws-configuration.html, it is the actual implementation used by the namespace declaration.

like image 1
edrabc Avatar answered Nov 18 '22 17:11

edrabc


You can mix propertyplaceholder vars and Spring EL:

<bean id="dataSource" class="xx.xxx.xxxxx.datasource.DataSourceWrapper" destroy-method="close">
<property name="dataSourceClassName" value="${db.dataSourceClassName}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="maximumPoolSize" value="${db.maxConnections}" />
<property name="connectionTimeout" value="${db.connectionTimeout}" />
<property name="dataSourceProperties">
    <props>
        <prop key="databaseName">${db.databaseName}</prop>
        <prop key="serverName">${db.serverName}#{':'}${db.port}</prop>
    </props>
</property>

Look at ${db.serverName}#{':'}${db.port} concat.

like image 1
Rod Lima Avatar answered Nov 18 '22 17:11

Rod Lima