RuntimeAnnoatationReader阅读定制注释 [英] RuntimeAnnoatationReader to read custom annotations

查看:177
本文介绍了RuntimeAnnoatationReader阅读定制注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何RuntimeAnnotationReader可用于JAXB注释阅读定制标注,并在runtime.While在谷歌搜索控件类领域,我发现这个链接的http://glassfish.10926.n7.nabble.com/injecting-JAXB-annotations-at-runtime-td59852.html但我无法收集有关RuntimeAnnotationReader多的信息。
我有以下reqirement:
假设我有一个类名Person.java

How RuntimeAnnotationReader can be used for JAXB annotations to read Custom Annotations and control class fields at runtime.While searching on google i found this link http://glassfish.10926.n7.nabble.com/injecting-JAXB-annotations-at-runtime-td59852.html but i couldn't gather much information about RuntimeAnnotationReader. I have the following reqirement: Suppose I have a class name Person.java

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSeeAlso;

@XmlRootElement(name="Person")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlSeeAlso({Student.class})
public class Person <T>{

private T payload;
@XmlElement(name = "Person-Name")
private String name;

public void setPayload(T payload){
    this.payload = payload;
}

public T getPayload(){
    return payload;
}

public void setName(String name){
    this.name = name;
}

public String getName(){
    return name;
}
}

和student.java:

and student.java:

  package RuntimeAnnotation;

  import javax.xml.bind.annotation.XmlAccessType;
  import javax.xml.bind.annotation.XmlAccessorType;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlRootElement;

  @XmlRootElement
  @XmlAccessorType(XmlAccessType.FIELD)
  public class Student {

@XmlElement
private String name;

@XmlElement
private String id;

public void setName(String name) {
    this.name = name;
}

public String getName(){
    return name;
}

public void setId(String id){
    this.id = id;
}

public String getId(){
    return id;
}
  }

虽然初始化Person类对象,我设置的有效载荷字段作为学生对象。现在,在编组XML输出​​显示有效载荷标记名,但我想学生应该在标记名被显示。

While initializing person class object, i am setting payload field as student object. Now while marshalling XML output shows payload as tagname, but I want student should be appear at tagname.

元帅输出:

 <Person>
     <payload xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="student">
      <name>ABC</name>
      <id>239423</id>
  </payload>
<Person-Name>XYZ</Person-Name>
</Person>

期望的输出:

<Person>
<T>
    <name>ABC</name>
    <id>239423</id>
<T>
<Person-Name>XYZ</Person-Name>
</Person>

P.S:应该是参数化的类名。在上述情况下T应当是学生。

P.S.: should be the parametrized class name. In the above case T should be Student.

推荐答案

要回答你的问题,请参见Annox自定义注释器实现的例子:

To answer your question, see Annox for an example of a custom Annotation Reader implementation:

<一个href=\"https://svn.java.net/svn/annox~svn/trunk/core/src/main/java/org/jvnet/annox/xml/bind/AnnoxAnnotationReader.java\" rel=\"nofollow\">https://svn.java.net/svn/annox~svn/trunk/core/src/main/java/org/jvnet/annox/xml/bind/AnnoxAnnotationReader.java

免责声明:我Annox和许多其他JAXB公用事业的作者

Disclaimer: I'm the author of Annox and many other JAXB utilities.

但除此之外,我不认为你真的需要这种运行时技巧来实现自己的目标。了解更多关于@XmlElementRef和JAXBElement的。你可以通过让像一个有效载荷得到您的XML:

But apart from that I don't think you really need this runtime tricks to achieve your goal. Read more on @XmlElementRef and JAXBElement. You can get your XML by having a payload like:

@XmlElementRef
JAXBElement<T> payload;

这篇关于RuntimeAnnoatationReader阅读定制注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆