Why doesn#39;t JAXB generate setters for Lists(为什么 JAXB 不为列表生成设置器)
问题描述
当我从 XSD 生成 JAXB 类时,具有 maxOccurs="unbounded" 的元素会获得为它们生成的 getter 方法,但没有 setter 方法,如下所示:
When I generate JAXB classes from an XSD, the elements with maxOccurs="unbounded" gets a getter method generated for them, but no setter method, as follows:
/**
 * Gets the value of the element3 property.
 * 
 * <p>
 * This accessor method returns a reference to the live list,
 * not a snapshot. Therefore any modification you make to the
 * returned list will be present inside the JAXB object.
 * This is why there is not a <CODE>set</CODE> method for the element3 property.
 * 
 * <p>
 * For example, to add a new item, do as follows:
 * <pre>
 *    getElement3().add(newItem);
 * </pre>
 * 
 * 
 * <p>
 * Objects of the following type(s) are allowed in the list
 * {@link Type }
 * 
 * 
 */
public List<Type> getElement3() {
    if (element3 == null) {
        element3 = new ArrayList<Type>();
    }
    return this.element3;
}
方法注释清楚地说明了如何使用它,但我的问题如下:
为什么 JAXB 不只是生成一个 setter,遵循 Java Beans 规则?我知道我可以自己编写 setter 方法,但是生成的 getter 方法中建议的方法有什么优势吗?
The method comment makes it crystal clear on how can I use it, but my question is as follows: 
Why doesn't JAXB just generate a setter, following the Java Beans rules? I know I can write the setter method myself, but is there any advantage to the approach suggested in the generated getter method?
这是我的 XSD:
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.example.org/DoTransfer/" targetNamespace="http://www.example.org/DoTransfer/">
    <element name="CollectionTest" type="tns:CollectionTest"></element>
    <complexType name="CollectionTest">
        <sequence>
            <element name="element1" type="string" maxOccurs="1" minOccurs="1"></element>
            <element name="element2" type="boolean" maxOccurs="1" minOccurs="1"></element>
            <element name="element3" type="tns:type" maxOccurs="unbounded" minOccurs="1" nillable="true"></element>
        </sequence>
    </complexType>
    <complexType name="type">
        <sequence>
            <element name="subelement1" type="string" maxOccurs="1" minOccurs="1"></element>
            <element name="subelement2" type="string" maxOccurs="1" minOccurs="0"></element>
        </sequence>
    </complexType>
</schema>
推荐答案
这是来自 JAXB 规范的理由 - 第 60 页.
Here is the justification from the JAXB specification - page 60.
设计说明 – List 属性没有 setter 方法.这getter 通过引用返回列表.一个项目可以添加到getter 方法使用适当的方法返回的列表在 java.util.List 上定义.JAXB 1.0 中这种设计的基本原理是使实现能够包装列表并能够在列表中添加或删除内容时执行检查.
Design Note – There is no setter method for a List property. The getter returns the List by reference. An item can be added to the List returned by the getter method using an appropriate method defined on java.util.List. Rationale for this design in JAXB 1.0 was to enable the implementation to wrapper the list and be able to perform checks as content was added or removed from the List.
因此,如果 List 的实现覆盖了 add/remove 以执行验证,那么用(例如)ArrayList 替换那个特殊"的 List 会破坏这些检查.
So if the implementation of the List was overriding add/remove to perform validation, replacing that 'special' List with (for instance) an ArrayList would defeat these checks.
这篇关于为什么 JAXB 不为列表生成设置器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 JAXB 不为列表生成设置器
 
				
         
 
            
        基础教程推荐
- 验证是否调用了所有 getter 方法 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 从 python 访问 JVM 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
						 
						 
						 
						 
						 
				 
				 
				 
				