【问题标题】:Simple Spring Property Substitution简单的 Spring 属性替换
【发布时间】:2017-01-08 05:08:01
【问题描述】:

所以我有一个spring配置文件:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="DisputeId" class="com.mycompany.validation.Attributes">
        <property name="validator" value="com.mycompany.validation.StringValidator" />
        <property name="maxLen" value="21" />
        <property name="nullable" value="false" />
    </bean>

我只是想将“com.mycompany.validation.Attributes”替换为类似于“${attributes.class}”的内容。

我该怎么做?我不需要在不同的文件中定义 ${attributes.class},而是希望它在同一个 xml 文件中。我知道我只是没有在寻找正确的术语...

谢谢!

【问题讨论】:

    标签: xml spring properties configuration substitution


    【解决方案1】:

    所以我最终做的是:

        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="properties">
             <value>attrib.class=com.mycompany.validation.Attributes</value>
          </property>
        </bean>
    
        <bean id="DisputeId" class="${attrib.class}">
          <property name="maxLen" value="21" />
          <property name="nullable" value="false" />
        </bean>
    

    这很好用,我真的只是想缩短要写入/读取的 xml 数量。有没有其他方法可以做到这一点 - 只是好奇。

    【讨论】:

      【解决方案2】:

      在spring上下文中声明一个map,并按键使用

      【讨论】:

      • 试过 ,但无法找到在 xml 配置中引用已定义变量的方法,例如 ${attrib.class}。我可能做得不对?
      猜你喜欢
      • 2018-04-28
      • 1970-01-01
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      • 2015-09-25
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      相关资源
      最近更新 更多