java - Castor:为具有抽象属性的类创建映射文件

标签 java spring castor

我有这门课:

public class Source extends Node {
  protected DistributionSampler delay ;
  protected DistributionSampler batchsize ;


/**
 * @param name The name of the source node
 * @param d The {@link DistributionSampler} used to generate the
 *          inter-arrival times
*/
  public Source( String name, DistributionSampler d ) {
    super( name ) ;
    delay = d ;
    batchsize = new Deterministic( 1 ) ;
    Sim.schedule( new Arrival( Sim.now() + delay.next() ) ) ;
  }

/**
 * @param name The name of the source node
 * @param d The {@link DistributionSampler} used to generate the
 *          inter-arrival times
 * @param b The {@link DistributionSampler} used to generate the
            batch sizes
*/
  public Source( String name, DistributionSampler d, DistributionSampler b ) {
    super( name ) ;
    delay = d ;
    batchsize = b ;
    Sim.schedule( new Arrival( Sim.now() + delay.next() ) ) ;
  }

   ....
}

DistributionSampler 是一个抽象类。

在从 XML 转换为 Java 对象时,我会知道要使用抽象类的哪个具体实现(通过 bean 名称)。

但是,我不完全确定如何编写映射文件来告诉 Castor 如何进行翻译。

任何帮助将不胜感激。

最佳答案

<class name="network.Source">
        <description xmlns="">
            Default mapping for class network.Source
        </description>

        <map-to xml="Source"/>

        <field name="name" type="java.lang.String" required="true">
            <bind-xml  node="element" />
        </field>

        <field name="delay" type="tools.DistributionSampler" required="true" set-method="initialiseDelay" get-method="getDelay">
            <bind-xml  auto-naming="deriveByClass" node="element" location="delay"/>
        </field>

        <field name="batchSize" type="tools.DistributionSampler">
            <bind-xml  auto-naming="deriveByClass" node="element" location="batchSize"/>
        </field>
    </class>

auto-naming="deriveByClass"部分意味着,如果我们发送它将嵌入延迟内的元素的节点名称绑定(bind)到它希望扩展 distributionSampler 的等效类。

因此很乐意处理以下 xml:

<Source name="asd">
    <delay>
        <Deterministic time="234" />
    </delay>

    <batchSize>
        <Erlang K="234" Theta="234" />
    </batchSize>
</Source>

将使用确定性和 Erlang 的映射文件将其映射到扩展 DistributionSampler 的类实例。

关于java - Castor:为具有抽象属性的类创建映射文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2370149/

相关文章:

java - Flink 流式处理程序在处理时间上正确运行,但在事件时间上不会产生结果

java - 我可以在 GXT 中的鼠标单击事件上将 ContentPanel 添加到 ContentPanel 中吗

java - java中如何使用try/catch避免抛出FileNotFoundException

java - Castor:如何映射 Java 层次结构

java - Castor XML 绑定(bind)和 JAXB 绑定(bind)有什么区别

用于 int 范围的 Java 8 IntStream?

java - 多线程应用程序中的spring + SQLite

Java 邮件发送器删除第二个内容 ID

java - 设置 Spring bean 的子属性

java - 使用 Castor XML 映射解码时忽略字段