concept xml in category seam

This is an excerpt from Manning's book Seam in Action.
One way that Seam cuts down on the configuration overhead of Java EE is by eliminating needless XML. Although once thought to be desirable because of is flexibility, XML is external configuration and quickly becomes out of sync (and out of touch) with the application logic. Seam brings configuration back in line with the code where it is easier to locate and can be refactored.
When the temptation arises to define JSF managed beans in XML, Seam just says “No!,” a tenet that is captured in figure 1.4. Seam reduces the declaration of a component to a single annotation, @Name, placed above the class definition. Seam components can take the place of JSF managed beans.
Figure 1.4. Seam cuts down on superfluous XML configuration that’s difficult to keep in sync with the source code.
![]()
With enough dedication, you can avoid the use of XML in Seam altogether, which is quite surprising given the number of places it could be warranted. Seam only resorts to XML when annotations do not suffice or to isolate deployment overrides. If you are not a fan of annotations, don’t go running for the door just yet. Seam still allows you to define components using XML, which is the main topic of chapter 5. Annotations are just more concise and easier to maintain, in my opinion.
Component definitions are another example where XML proves useful. Annotations such as @Name are easy enough to add to classes under your control. However, if the class you intend to declare as a Seam component is sealed in a third-party JAR file or maintained by another team, annotations don’t do you much good. There may also be situations where you need to alter an existing component definition—either one from your application or one of Seam’s built-in components—or assign property values to that component. In these cases, you have to resort to external configuration, which this chapter covers.
In the previous chapter, you gained an appreciation for the simplicity that annotations bring to the development of Seam components. If you are content working with annotations, I encourage you to skip ahead to the next chapter to learn about another set of annotations that are used to wire components together and to initialize context variables. On the other hand, if you are interested in learning how to define and configure components in XML, this chapter shows how this task is accomplished using the Seam component descriptor. The component descriptor contains XML-based metadata that offers a way to keep the component definition separate from the class. You can also use this file to assign initial property values to component instances, wire components together, override the settings of existing components, and control built-in Seam functionality. The XML in Seam isn’t all old-fashioned, though. Thanks to XML namespaces, you may almost mistake the XML for a real language. In addition to XML, you’ll learn that Java properties files can be used to accomplish certain types of configuration in Seam. We’ll look at Seam’s internationalization (i18n) support as an example of configuring a built-in Seam component and how keys in a message bundle can in turn be used to supply locale-specific values to the properties of a Seam component. By the end of the chapter, you’ll appreciate that both XML and Java properties serve as a valuable supplement to Seam’s primarily annotation-based approach.
Listing 5.1 shows a simple component descriptor with two components defined. For the components shown, it is assumed that the @Name and @Scope annotations are absent from the class definitions. Instead, these classes are declared to be components using XML. If the classes had @Name annotations equivalent to these definitions, an exception would result for reasons that are described in section 5.4 (which covers component definitions overrides).
Listing 5.1. A component descriptor with two component definitions
<components xmlns="http://jboss.com/products/seam/components" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.0.xsd"> <component name="newGolfer" class="org.open18.model.Golfer" scope="event"/> <component name="passwordBean" class="org.open18.auth.PasswordBean" scope="event" auto-create="true"/> </components>If Spring hasn’t made you tired of XML yet, then these definitions don’t look so bad. The <component> element defines a new Seam component for the class specified in the class attribute. The name attribute is equivalent to the @Name annotation and the scope attribute is equivalent to the @Scope annotation. The mappings between component definition annotations and the XML <component> element attributes are shown in table 5.2.
There’s also the possibility that you simply prefer XML over annotations. In that case, you can use the component descriptor to define all of your Seam components. Seam affords you that flexibility, though I don’t recommend that approach. Either way, Seam has a wealth of built-in functionality that is only an XML element away. Likewise, you can use the component descriptor to dress up your own components once they are set in stone (i.e., compiled).