Java 在SpringXML配置中连接字符串
我需要将Springbean的字符串值连接到现有字符串,然后将其设置为另一个bean的属性:Java 在SpringXML配置中连接字符串,java,spring,jax-ws,cxf,spring-ws,Java,Spring,Jax Ws,Cxf,Spring Ws,我需要将Springbean的字符串值连接到现有字符串,然后将其设置为另一个bean的属性: <bean id="inet" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetClass"><value>java.net.InetAddress</value></property>
<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>
java.net.InetAddress
getLocalHost
获取主机名
此时,在“主机”bean中有了主机名。我现在需要连接它并将其传递给publishedEndpointUrl属性。大概是这样的:
<jaxws:endpoint
id="foo"
publishedEndpointUrl= "http://" + host + "/Foo"
implementor="com.example.v1.foo"
address="/v1/Foo"/>
使用SpringXML配置是如何做到这一点的?您需要了解。这允许您定义全局属性,它可以来自属性文件,或者在您的情况下,您可以定义默认值,在这种情况下,它只是一个全局属性。以下工作将起作用:
<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>
系统属性模式覆盖
jdbc.oracle.Driver
弗雷德
文件:properties/application.properties
${driver}
jdbc:${dbname}
这意味着您有${driver}和${dbname}的默认值,用于定义数据源。这些值可以在application.properties文件中重写,甚至可以作为命令行上的-D选项重写。您可以使用和工厂方法
:
<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"
...
由于jaxws:*namespace不喜欢springel,另一种方法是声明bean,而不是jaxws:endpoint
对象
这是更多的工作,但正如中所指出的,这是名称空间声明使用的实际实现。您可以混合使用propertyplaceholder变量和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>
${db.databaseName}
${db.serverName}{':'}${db.port}
看看${db.serverName}{:'}${db.port}concat.这是一个智能解决方案,但我得到了以下异常:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为“{http://”+localhost.hostName+'/Foo”的bean