代码之家  ›  专栏  ›  技术社区  ›  SingleShot

如何将自定义Spring模式类型与传统Spring模式类型混合和匹配?

  •  4
  • SingleShot  · 技术社区  · 16 年前

    假设我有一节课 Person 具有属性 name age ,并且可以像这样配置弹簧:

    <beans:bean id="person" class="com.mycompany.Person">
      <beans:property name="name" value="Mike"/>
      <beans:property name="age" value="38"/>
    </beans:bean>
    

    我想为它提供一个定制的Spring模式元素,这很容易做到,允许我在Spring配置文件中包含以下内容:

    <Person name="Mike" age="38"/>
    

    架构定义如下所示:

    <xsd:complexType name="Person">
      <xsd:complexContent>
        <xsd:extension base="beans:identifiedType">
          <xsd:attribute name="name" type="xsd:string"/>
          <xsd:attribute name="age" type="xsd:int"/>
        </xsd:extension>
      </xsd:complexContent>
    </xsd:complexType>
    

    基于此,现在让我们假设我想选择将我的自定义模式元素与SpringBean的传统元素和属性混合并匹配,因此我可以选择这样做:

    <Person name="Mike">
      <beans:property name="age" value="38"/>
    </Person>
    

    1 回复  |  直到 16 年前
        1
  •  1
  •   ChssPly76    16 年前

    首先,如果你的例子是 所有你想做的,考虑使用 p-namespace 取而代之的是给自己留点大麻烦:

    <beans:bean id="person" class="com.mycompany.Person" p:name="Mike" p:age="38"/>
    

    是的,它看起来不像你想象的那么漂亮 <Person name= age= />

    如果这不能满足您的需求,您将考虑实现自己的名称空间/bean定义解析器。Spring文档有一个 chapter dedicated to that