Jaxb @XmlElement可以放在哪里?

Jaxb @XmlElement可以放在哪里?,jaxb,annotations,setter,Jaxb,Annotations,Setter,我最近在setter上使用的项目中看到了这个JAXB注释。根据我自己的经验,我知道@xmlement可以用于属性和getter。我不确定这个注释是否可以也应该用于setter,我在谷歌上搜索了一下,没有找到一个明确的答案。请告知。明确表示它对设定器有效: public abstract class AbstractInlineAnnotationReaderImpl<T,C,F,M> implements AnnotationReader<T,C,F,M> { .

我最近在setter上使用的项目中看到了这个JAXB注释。根据我自己的经验,我知道@xmlement可以用于属性和getter。我不确定这个注释是否可以也应该用于setter,我在谷歌上搜索了一下,没有找到一个明确的答案。请告知。

明确表示它对设定器有效:

public abstract class AbstractInlineAnnotationReaderImpl<T,C,F,M>
    implements AnnotationReader<T,C,F,M> {
...

    public final <A extends Annotation> A getMethodAnnotation(Class<A> annotation, M getter, M setter, Locatable srcPos) {
        A a1 = getter==null?null:getMethodAnnotation(annotation,getter,srcPos);
        A a2 = setter==null?null:getMethodAnnotation(annotation,setter,srcPos);

        if(a1==null) {
            if(a2==null)
                return null;
            else
                return a2;
        } else {
            if(a2==null)
                return a1;
            else {
                // both are present
                getErrorHandler().error(new IllegalAnnotationException(
                    Messages.DUPLICATE_ANNOTATIONS.format(
                        annotation.getName(), fullName(getter),fullName(setter)),
                    a1, a2 ));
                // recover by ignoring one of them
                return a1;
            }
        }
    }
...
}
公共抽象类AbstractInlineAnnotationReaderImpl
实现AnnotationReader{
...
公共最终的getMethodAnnotation(类注释、M getter、M setter、Locatable srcPos){
a1=getter==null?null:getMethodAnnotation(annotation,getter,srcPos);
a2=setter==null?null:getMethodAnnotation(注释,setter,srcPos);
如果(a1==null){
如果(a2==null)
返回null;
其他的
返回a2;
}否则{
如果(a2==null)
返回a1;
否则{
//两人都在场
getErrorHandler().error(新的IllegalAnnotationException(
Messages.DUPLICATE_ANNOTATIONS.format(
annotation.getName(),fullName(getter),fullName(setter)),
a1,a2);
//通过忽略其中一个进行恢复
返回a1;
}
}
}
...
}
但是,我也找不到这方面的规范性参考。

明确指出,它确实适用于设定器:

public abstract class AbstractInlineAnnotationReaderImpl<T,C,F,M>
    implements AnnotationReader<T,C,F,M> {
...

    public final <A extends Annotation> A getMethodAnnotation(Class<A> annotation, M getter, M setter, Locatable srcPos) {
        A a1 = getter==null?null:getMethodAnnotation(annotation,getter,srcPos);
        A a2 = setter==null?null:getMethodAnnotation(annotation,setter,srcPos);

        if(a1==null) {
            if(a2==null)
                return null;
            else
                return a2;
        } else {
            if(a2==null)
                return a1;
            else {
                // both are present
                getErrorHandler().error(new IllegalAnnotationException(
                    Messages.DUPLICATE_ANNOTATIONS.format(
                        annotation.getName(), fullName(getter),fullName(setter)),
                    a1, a2 ));
                // recover by ignoring one of them
                return a1;
            }
        }
    }
...
}
公共抽象类AbstractInlineAnnotationReaderImpl
实现AnnotationReader{
...
公共最终的getMethodAnnotation(类注释、M getter、M setter、Locatable srcPos){
a1=getter==null?null:getMethodAnnotation(annotation,getter,srcPos);
a2=setter==null?null:getMethodAnnotation(注释,setter,srcPos);
如果(a1==null){
如果(a2==null)
返回null;
其他的
返回a2;
}否则{
如果(a2==null)
返回a1;
否则{
//两人都在场
getErrorHandler().error(新的IllegalAnnotationException(
Messages.DUPLICATE_ANNOTATIONS.format(
annotation.getName(),fullName(getter),fullName(setter)),
a1,a2);
//通过忽略其中一个进行恢复
返回a1;
}
}
}
...
}
但是,我也找不到这方面的规范性参考。

来自JAXB 2.2规范的“8.9属性和字段”(请参阅:)

对于属性,给定的注释可以应用于读取或 写入属性,但不能同时写入两者

换句话说,注释可以放在get或set方法上。根据我的经验,大多数人将注释放在get方法上。

来自JAXB2.2规范的“8.9属性和字段”部分(请参阅:)

对于属性,给定的注释可以应用于读取或 写入属性,但不能同时写入两者


换句话说,注释可以放在get或set方法上。根据我的经验,大多数人将注释放在get方法上。

我添加了一个带有规范参考的答案:我添加了一个带有规范参考的答案: