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 不为列表生成设置器
基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01