在 spring xml 配置中连接字符串

发布于 2024-12-17 07:45:46 字数 974 浏览 5 评论 0原文

我需要将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

此生挚爱伱 2024-12-24 07:45:46

您可以使用 Spring-ELfactory-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"
   ...

编辑:

jaxws:endpoint 标记似乎能够使用 #beanId 表示法引用 bean 值,但不喜欢弹簧-EL。因此,通过构造一个 String bean,我们可以解决这个问题,而且它看起来仍然相当整洁。

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.

淑女气质 2024-12-24 07:45:46

您需要查看 PropertyPlaceholderConfigurer。这允许您定义全局属性,该属性可以来自属性文件,或者在您的情况下,您可以定义默认值,在这种情况下它只是一个全局属性。以下内容将起作用:

<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>

这意味着您有 ${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:

<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.

两人的回忆 2024-12-24 07:45:46

由于 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.

怎樣才叫好 2024-12-24 07:45:46

您可以混合使用 propertyplaceholder var 和 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.serverName}#{':'}${db.port} 连接。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文