Merge string in spring xml configuration

I need to bind the string value of the 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 host name in the 'host' bean. Now I need to combine it and pass it to the publishEndpointUrl attribute. Something like that:

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

How to do this using spring xml configuration?

+8
java spring spring-ws jax-ws cxf
source share
4 answers

You can 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 is apparently able to reference bean values ​​using the #beanId notation, but Spring -EL is not like it. Therefore, having built a String bean, we will get around this and still look pretty neat.

+16
source share

You need to look at PropertyPlaceholderConfigurer . This allows you to define global properties that can either come from a property file, or in your case you can define a default value, in which case it is 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 determine the data source. These values ​​can be overridden in the application.properties file or even as the -D option on the command line.

+3
source share

As jaxws: * namespace dislikes Spring EL , an alternative would be to declare an EndpointImpl bean instead of a jaxws:endpoint object.

This is another work, but as stated in http://cxf.apache.org/docs/jax-ws-configuration.html , this is the actual implementation used in the namespace declaration.

+1
source share

You can mix v2 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> 

Take a look at the $ {db.serverName} # {':'} $ {db.port} concat.

+1
source share

All Articles