在 spring xml 配置中连接字符串
我需要将 spring bean 的字符串值连接到现有字符串,然后将其设置为另一个 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>
此时,我在“host”bean 中拥有主机名。我现在需要连接它并将其传递给publishedEndpointUrl 属性。像这样的事情:
<jaxws:endpoint
id="foo"
publishedEndpointUrl= "http://" + host + "/Foo"
implementor="com.example.v1.foo"
address="/v1/Foo"/>
如何使用 spring xml 配置完成此操作?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 Spring-EL 和
factory-method
:编辑:
jaxws:endpoint
标记似乎能够使用#beanId
表示法引用 bean 值,但不喜欢弹簧-EL。因此,通过构造一个 String bean,我们可以解决这个问题,而且它看起来仍然相当整洁。You could use Spring-EL and
factory-method
: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 aString
bean, we get around this and it still looks fairly neat.您需要查看 PropertyPlaceholderConfigurer。这允许您定义全局属性,该属性可以来自属性文件,或者在您的情况下,您可以定义默认值,在这种情况下它只是一个全局属性。以下内容将起作用:
这意味着您有 ${driver} 和 ${dbname} 的默认值,它们用于定义数据源。这些值可以在 application.properties 文件中覆盖,甚至可以作为命令行上的 -D 选项覆盖。
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:
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.
由于 jaxws:* 命名空间 与 Spring EL 不同,另一种选择是声明一个 EndpointImpl bean,而不是
jaxws:endpoint
对象。这还需要一些工作,但正如 http://cxf.apache 中指出的那样。 org/docs/jax-ws-configuration.html,它是命名空间声明使用的实际实现。
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.
您可以混合使用 propertyplaceholder var 和 Spring EL:
查看 ${db.serverName}#{':'}${db.port} 连接。
You can mix propertyplaceholder vars and Spring EL:
Look at ${db.serverName}#{':'}${db.port} concat.