DZone, Inc.
|
www.dzone.com
CONTENTS INCLUDE:
n
Bijection in a Nutshell
n
Contextual Components
n
Common Application Configuration
n
Seam Security
n
Application Framework
n
Hot Tips and More...
Seam is a next generation web framework that integrates standard
Java EE technologies with a wide variety of nonstandard
technologies into a consistent, unified, programming model. Seam
drove the development of the Web Beans specification (JSR-299)
and continues to develop innovations that are changing the face
of web development as well as Java EE technologies. If you
haven’t taken a look at Seam, I suggest you do.
As you develop Seam applications, you'll find this quick reference
a handy guide for understanding core concepts, configuration,
and tool usage. This quick reference is not intended to cover all
of what Seam provides, but will cover the most commonly used
annotations and XML elements as of Seam 2.1. In addition, this
guide will point you to examples distributed with Seam to see real
examples of how the configuration options can be used in practice.
When a method is invoked on a component, its dependencies
are injected from the current context. Seam performs a context
lookup in the following order of precedence: Event Scope,
Page Scope, Conversation Scope, Session Scope, Business
Scope, Application Scope.
Component Annotations
Component Definition Annotations
In order for your Seam components to take advantage of bijection,
you must register them with the Seam container. Registering
your component with Seam is as simple as annotating it with
@Name. The following annotations will register your component
and define its lifecycle.
Dependency injection is an inversion of control technique that
forms the core of modern-day frameworks. Traditionally objects
have held the responsibility for obtaining references to the
objects they collaborate with. These objects are extroverted as
they reach out to get their dependencies. This leads to tight
coupling and hard to test code.
Dependency injection allows us to create introverted objects.
The objects dependencies are injected by a container or by
some external object (e.g. a test class). Bijection is described
by the following formula:
dependency injection + context = bijection
With bijection, when dependencies are injected context counts!
Dependencies are injected prior to each component method
invocation. In addition, components can contribute to the
context by outjecting values.
As you can see the
HotelBookingAction is scoped to and
executes within a context. This behavior allows us to quit
worrying about shuffling values into and out of contexts like the
HttpSession, allows components to hold state, and unifies the
component model across application tiers.
ABOUT SEAM CONTEXTUAL COMPONENTS
BIJECTION IN A NUTSHELL
Get support for Seam 2
JBoss Enterprise Application Platform
now includes Seam 2
• JBoss Enterprise Application Platform pre-integrates
JBoss Application Server, Seam, and Hibernate
• Latest feature pack includes support for Seam 2
• Includes caching, clustering, messaging, transactions,
and integrated web services stack
• Support for industry-leading Java and newer technologies,
including JAX-WS, EJB 3.0, JPA 1.0, JSF 1.2, and JTA 1.1
Download today: jboss.com/download
© 2008 Red Hat Middleware, LLC. All Rights Reserved. Red Hat, Red Hat Enterprise Linux, the
Shadowman logo and JBoss are registered trademarks of Red Hat, Inc. in the U.S. and other
countries. Linux is a registered trademark of Linus Torvalds.
Core Seam
By Jacob Orshalick
Annotation Use Description
@Name
Type Declares a Seam component by name. The component is
registered with Seam and can be referenced by name through
Expression Language (EL), injection, or a context lookup.
@Scope
Type Defines the scope (or context) the Seam component will be
placed into by the container when created.
@AutoCreate
Type Specifies that a component should be created when being
injected if not available in the current context.
@Startup
Type Indicates that an application scoped component should be
created when the application is initialized or that a session
component should be created when a session is started. Not
valid for any other contexts.
@Install
Type Declares whether a Seam component should be installed based
on availability of dependencies or precedence.
@Role
Type Defines an additional name and scope associated with the
component. The
@Roles annotation allows definition of
multiple roles.
Core Seam www.dzone.com Get More Refcardz! Visit refcardz.com
#31
About Seam Core Quick Reference
Seam is a next generation web framework that integrates standard Java EE
technologies with a wide variety of nonstandard technologies into a consistent,
unified, programming model. Seam drove the development of the Web Beans
specification (JSR-299) and continues to develop innovations that are changing the
face of web development as well as Java EE technologies. If you haven't taken a
look at Seam, I suggest you do.
As you develop Seam applications, you'll find this quick reference a handy guide for
understanding of core concepts, configuration, and tool usage. This quick reference
is not intended to cover all of what Seam provides, but will cover the most
commonly used annotations and XML elements as of Seam 2.1. In addition, this
guide will point you to examples distributed with Seam to see real examples of how
the configuration options can be used in practice.
Bijection in a Nutshell
Dependency injection is an inversion of control technique that forms the core of
modern-day frameworks. Traditionally objects have held the responsibility for
obtaining references to the objects they collaborate with. These objects are
extroverted as they reach out to get their dependencies. This leads to tight coupling
and hard to test code.
Dependency injection allows us to create introverted objects. The objects
dependencies are injected by a container or by some `external object (e.g. a test
class). Bijection is described by the following formula:
dependency injection + context = bijection
With bijection, when dependencies are injected context counts! Dependencies are
injected prior to each component method invocation. In addition, components can
contribute to the context by outjecting values.
As you can see the HotelBookingAction is scoped to and executes within a
context. This behavior allows us to quit worrying about shuffling values into and
out of contexts like the HTTPSession, allows components to hold state, and
unifies the component model across application tiers.
Contextual Components
When a method is invoked on a component, its dependencies are injected from the
current context. Seam performs a context lookup in the following order of
precedence: Event Scope, Page Scope, Conversation Scope, Session Scope,
Business Scope, Application Scope.
Component Annotations
Component Definition Annotations
In order for the Seam container to provide bijection to your components you must
register your components. Fortunately registering your component with Seam is as
simple as annotating it with
@Name. The following annotations will register your
component and define its lifecycle.
Annotation Use Description
@Name
Type Declares a Seam component by name. The component is
registered with Seam and can be referenced by name
through EL, injection, or a context lookup.
@Scope
Type Defines the scope (or context) the Seam component will
be placed into by the container when created.
@AutoCreate
Type Specifies that a component should be created when being
injected if not available in the current context.
@Startup
Type Indicates that an application scoped component should be
created when the application is initialized or that a
session component should be created when a session is
started. Not valid for any other contexts.
@Install
Type Declares whether a Seam component should be installed
based on availability of dependencies or precedence.
@Role
Type Defines an additional name and scope associated with the
component. The
@Roles annotation allows definition of
multiple roles.
Component Bijection Annotations
Once you have defined a component, you can specify the dependencies of your
component and what the component contributes back to the context.
Annotation Use Description
@In
Field,
Method
Declares a dependency that will be injected from the
context, according to context precedence, prior to a
method invocation. Note that these attributes will be
disinjected (or set to null) after the invocation
completes.
@Out
Field,
Method
Declares a value that will be outjected after a method
invocation to the context of the component (implicit) or a
specified context (explicit).
Component Lifecycle Annotations
The following annotations allow you to control the lifecycle of a component either
by reacting to events or wrapping the component entirely.
Annotation Use Description
@Create
Method Declares that a method should be called after component
instantiation.
@Destroy
Method Declares that a method should be called just before
component destruction.
@Factory
Method Marks a method as a factory method for a context variable.
A factory method is called whenever no value is bound to
the named context variable and either initializes and
outjects the value or simply returns the value to the context.
@Unwrap
Method Declares that the object returned by the annotated getter
method is to be injected instead of the component itself.
Referred to as the "manager component" pattern.
Component Events Annotations
Through Seam's event model, components can raise events or listen for events raised
by other components through simple annotation. In addition, Seam defines a
number of built-in events that the application can use to perform special kinds of
framework integration (see
http://seamframework.org/Documentation,
Contextual Events).
Annotation Use Description
@RaiseEvent
Method Declares that a named event should be raised after the
method returns a non-null result without exception.
@Observer
Method Declares that a method should be invoked on occurrence
of a named event or multiple named events.
The Components Namespace
Schema URI
http://jboss.com/products/seam/components
Schema XSD
http://jboss.com/products/seam/components-2.1.xsd
So far we've seen how components can be declared using annotations. In most cases
this is the preferred approach, but there are some situations when component
definition through annotations is not an option:
● when a class from a library outside of your control is to be exposed as a
component
● when the same class is being configured as multiple components
In addition, you may want to configure values into a component that could change
by environment, e.g. ip-addresses, ports, etc. In any of these cases, we can use XML
HotelBookingAction
bookHotel()
Hotel
Before you begin,
here is the hotel to book...
Booking
Okay then, I'm done,
here's the booking!
Context
Brought to you by...