
CONTENTS INCLUDE:
n
What is EJB3?
n
Resource Injection
n
Injection of EJB References
n
Injecting JPA Resources
n
Injecting Spring Beans in EJB 3
n
Hot Tips and more...
DZone, Inc.
|
www.dzone.com
www.dzone.com Get More Refcardz! Visit refcardz.com
Dependency Injection in
EJB 3
By Debu Panda
Enterprise JavaBeans (EJB) is a platform for building portable,
reusable, and scalable business applications using the Java
programming language. Since its initial incarnation, EJB has
been touted as a component model or framework that lets you
build enterprise Java applications without having to reinvent
services such as transactions, security, and automated persistence
for building an application.
EJB 3 greatly simplifies development by adopting a POJO
programming model. As shown in the following figure an
annotation transforms a simple POJO to an EJB.
EJB 3 not only simplifies development of session and message
driven beans but it also radically simplifies the persistence
model by implementing a simplified Object-Relational Mapping
approach similar to Oracle TopLink and JBoss Hibernate as a
part of the Java Persistence API. Note that JPA replaces EJB
2 CMP Entity beans in the EJB 3 spec, while being available
outside of the Java EE container.
Following is an example of a simple EJB 3 Stateless session bean.
import javax.ejb.Stateless;
import ejb3inaction.example.persistence.Bid;
@Stateless
public class PlaceBidBean implements PlaceBid {
...
public PlaceBidBean() {}
public Bid addBid(Bid bid) {
System.out.println(“Adding bid, bidder ID=”
+ bid.getBidderID()
+ “, item ID=” + bid.getItemID()
+ “, bid amount=”
+ bid.getBidAmount() + “.”);
return save(bid);
}
...
}
import javax.ejb.Local;
import ejb3inaction.example.persistence.Bid;
@Local public interface PlaceBid {
Bid addBid(Bid bid);
}
WHAT IS EJB 3?
With EJB 3, dependency injection has greatly simplified accessing
both EJB resources—such as JDBC DataSource, JMS Objects,
and JPA Entity Manager—and services—such as Timer, User
Transaction, and Web Services. You will find this Refcard useful
when building enterprise Java applications with EJB 3 and JPA.
It lists all metadata annotations, describes them and provides
examples. It also provides descriptions for XML elements that
you can use for injection.
Most enterprise Java applications use external resources
and services such as Data Source, EJB, or web services. EJB 3
makes using resources and services simpler by implementing
dependency injection.
Dependency injection allows you to simply declare component
dependencies and let the EJB container deal with the complexities
of instantiating, initializing, and sequencing resources and
supplying service or resource references to clients as required.
Development frameworks like Spring framework originally
popularized dependency injection.
In EJB 3, you may think of dependency injection as the inverse of
JNDI. It is the responsibility of the container to inject an object
based on the dependency declaration. The figure below com-
pares dependency injection with JNDI.
DEPENDENCY INJECTION IN EJB 3
Dependency Injection in EJB 3
n
Authoritative content
n
Designed for developers
n
Written by top experts
n
Latest tools & technologies
n
Hot tips & examples
n
Bonus content online
n
New issue every 1-2 weeks
Subscribe Now for FREE!
Refcardz.com
Get More Refcardz
(
They’re free!
)
tech facts at your fingertips
tech facts at your fi ngertips
Element Description
<description> Used to describe a Spring context or an individual bean.
<import> Imports another Spring context definition.
<description> Documents the bean. Although ignored by the container,
<description> can be used by tools that document Spring
contexts.
<lookup-method> Enables getter-injection by way of method replacement.
Specifies a method that will be overridden to return a
specific bean. Commonly known as getter-injection.
<meta> Allows for meta-configuration of the bean. Only useful
when there are beans configured that interprets and acts
on the meta information.
<property> Injects a value or a bean reference into a specific property
of the bean. Commonly known as setter-injection.
<replaced-method> Replaces a method of the bean with a new implementation.
Element Description
<alias> Creates an ali as for a bean definition.
<bean> Defines a bean in the Spring container.
<constructor-arg> Injects a value or a bean reference into an argument of the
bean’s constructor. Commonly known as constructor injection.
Attribute Description
abstract If true, the bean is abstract and will not be instantiated by
the Spring container.
autowire Declares how and if a bean should be autowired. Valid
values are byType, byName, constructor, autodetect, or
no for no autowiring.
autowire-candidate If false, the bean is not a c andidate for autowiring in to
another bean.
class The fully-qualified class name of the bean.
dependency-check Determines how Spring should enforce property setting on the
bean. simple indicates that all primitive type properties should
be set; objects indicates that all complex type properties
should be set. Other value values are default, none, or all.
depends-on Identifies a bean that should be instantiated by the
container before this bean is instantiated.
Lookup
EJB JNDI lookup
Resources
Beans
Dependency
injection
EJB
Container Injection
Resources
Beans
+ @ =
J
POJO Annotation EJB
#6