Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 4. Components and contexts > Defining components using annotations

4.3. Defining components using annotations

In Seam, you can define components in one of two ways: you can use either annotations or XML. The goal of Seam is to reduce as much XML coding as possible. Therefore, annotations are the preferred mechanism for defining a Seam component. Two common alternatives are the XML-based Seam component descriptor, covered in chapter 5, and Seam’s pluggable container mechanism—the integration that allows Spring beans to serve as Seam components—which is explored in chapter 15 (online). This chapter focuses on the annotation approach. The annotations that dictate how a component is defined are listed in table 4.4. As the chapter develops, I’ll introduce you to each one in detail.

Table 4.4. Seam annotations that define components and declare how they are instantiated
AnnotationWhat it does
@NameDeclares a class as a Seam component and assigns it a name. When the component name is requested, Seam instantiates a new instance of the class and binds it to the context variable with the same name.
@ScopeSpecifies the default scope in which the context variable is stored when an instance of the component is instantiated.
@Role (@Roles)Defines alternate name and scope pairings for the component that can be usedto instantiate parallel instances of the same component for different purposes. Multiple @Role declarations are wrapped in a @Roles annotation.
@StartupInstructs Seam to instantiate the component automatically when its assigned context starts (only applies to application-scoped and session-scoped components).
@NamespaceBinds a URI to a Java package and used to define custom XML namespaces in the Seam component descriptor.
@InstallUsed to make installation of a component conditional or to provide a precedence to override another component definition. Conditions include the presence of a class on the classpath, the presence of another component, or the debug mode setting.
@AutoCreateTells Seam to create a new instance of the component when the component name is first requested, even if the calling code doesn’t request for it to be created.



  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial