📄 lib0076.html
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>JAXB Usage Guidelines</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0075.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0077.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
<br>
<div class="chapter">
<a name="ch11"></a>
<div class="section">
<h2 class="first-section-title"><a name="363"></a><a name="ch11lev1sec4"></a>JAXB Usage Guidelines</h2><p class="first-para">The advantages of using JAXB are many. It drastically reduces the amount of custom application code needed for XML processing. JAXB-binded classes can easily be regenerated, so keeping up with XML document format changes is relatively easy. And because it's a native part of the JDK, you can be assured that JAXB will be around awhile.</p>
<p class="para">JAXB is not appropriate for situations where you want to process selected tags of XML documents and ignore the rest. It doesn't provide any searching capabilities (as X/Path does). And it is not appropriate if you merely want to reformat an XML document into some other content type (such as HTML); I would use X/Path, XSL, and XSLT for these purposes.</p>
<p class="para">Although most developers commonly use the marshaling and unmarshaling API portion of JAXB, I consider the API awkward. The designers of the API could have (and should have) written these actions as one-line calls to make them easier to use. I recommend using a shortcut, such as the one provided with CementJ, until less verbose ways to marshal and unmarshal become part of the JAXB API.</p>
<p class="para">
<b class="bold">Never directly modify code generated by JAXB.</b> If you do, those changes will be lost when you regenerate the binded classes for an XML document format change. Further, you would need to write test classes for these custom changes where no tests were necessary before.</p>
<p class="para">If an XML schema is shared across applications, you might consider centralizing JAXB-generated classes. Most applications using an XML document don't require custom generation options and can use a centrally generated class set.</p>
<p class="para">
<b class="bold">Generate JAXB into separate packages—don't commingle.</b> If you commingle generated code with custom code, developers might have difficulty knowing what source is modifiable and what they shouldn't touch. In short, it's easier to enforce the "no modify" guideline previously described if you create separate packages.</p>
<p class="last-para">
<b class="bold">Don't use generated JAXB classes as value objects in applications.</b> In many applications, JAXB-generated interfaces resemble the value objects, implying that the JAXB-generated interfaces should replace the value objects. <a name="364"></a><a name="IDX-150"></a>However, exposing JAXB classes to the rest of your application makes your application vulnerable to changes in XML document format. Further, value objects often need capabilities that don't exist in the generated JAXB interfaces (e.g., <span class="fixed">Serializable</span>, <span class="fixed">Comparable</span>, overriding <span class="fixed">equals()</span> and <span class="fixed">hashcode()</span>, etc.). And the pain of using JAXB-generated classes in your application as value objects will persist long after you've forgotten the initial development time savings.</p>
</div>
</div><br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0075.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0077.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -