Concatenate string in spring xml configuration
Asked Answered
B

4

9

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?

Ballottement answered 22/11, 2011 at 22:50 Comment(0)
K
19

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.

Klatt answered 22/11, 2011 at 23:41 Comment(1)
thats a smart solution, but I get the following exception: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '{'http://' + localhost.hostName + '/Foo'}'Ballottement
S
3

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.

Staircase answered 22/11, 2011 at 23:7 Comment(0)
D
1

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.

Donation answered 1/10, 2012 at 13:58 Comment(0)
E
1

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.

Elvaelvah answered 20/5, 2015 at 18:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.