Java Reference
In-Depth Information
application, this is probably perfect. If you want, you can omit the
@Local
an-
notation and the interface will still count as a local EJB interface. Alternatively,
you could have marked the interface with the
@Remote
or
@WebService
an-
notations. Remote access through the
@Remote
annotation is provided under the
hood by Java Remote Method Invocation (RMI), so this is the ideal means of re-
mote access from Java clients. If the EJB needs to be accessed by non-Java clients
like Microsoft .NET or PHP applications, SOAP-based remote access can be en-
abled using the
@WebService
annotation applied either on the interface or on
the bean class. Note also that an EJB need not have an interface at all.
•
@Inject—
As you know, the bid service depends on the bid DAO for persistence.
The
@Inject
CDI annotation injects the non-EJB DAO into the
BidService
instance variable. If you're not familiar with dependency injection, you may think
that what the
@Inject
annotation is doing is a little unusual—in a nifty, black-
magic kind of way. You might have been wondering if the
bidDao
private vari-
able is even usable because it's never set! If the container didn't intervene, you'd
get the infamous
java.lang.NullPointerException
when you tried to
be null. One interesting way to understand dependency injection is to think of it
makes the container “instantiate” the
bidDao
variable with the right DAO imple-
mentation before the variable is available for use.
Understanding statelessness
As long as calling the
addBid
method results in the creation of a new bid record each
time, the client doesn't care about the internal state of the bean. There's absolutely no need
for the stateless bean to guarantee that the value of any of its instance variables will be
the same across any two invocations. This property is what statelessness means in terms of
server-side programming.
The
BidService
session bean can afford to be stateless because the action of placing a
bid is simple enough to be accomplished in a single step. The problem is that not all busi-
ness processes are that simple. Breaking down a process into multiple steps and maintain-
ing the internal state to glue together the steps is a common technique to present complex
processes to the user in a simple way. Statefulness is particularly useful if what the user
does in a given step in a process determines what the next step is. Think of a questionnaire-
based setup wizard. The user's input for each step of the wizard is stored behind the scenes