Java Reference
In-Depth Information
}
public PlaceOrderServiceImpl(
PendingOrderRepository pendingOrderRepository,
RestaurantRepository restaurantRepository) {
bb
this.pendingOrderRepository = pendingOrderRepository;
bb
this.restaurantRepository = restaurantRepository;
}
The
restaurantRepository
and
pendingOrderRepository
fields have an
@EJB
anno-
tation that tells the
EJB
container to initialize them. Like the
PlaceOrderFacade
, the
PlaceOrderService
has two constructors: a default constructor for the
EJB
con-
tainer to use and another for the mock object tests.
Annotating the repositories
The repositories are also configured as stateless session beans. They are injected
with the
EntityManager
. Here is the bean class for the
PendingOrderRepository
:
@Stateless
public class EJB3PendingOrderRepository implements
PendingOrderRepository {
@PersistenceContext
private EntityManager entityManager;
public EJB3PendingOrderRepository() {
}
public EJB3PendingOrderRepository(EntityManager entityManager) {
bb
this.entityManager = entityManager;
}
The
entityManager
field has an
@PersistenceContext
annotation that tells the
EJB
container to initialize the field with a reference to the
EntityManager
. The other
repositories are annotated in a similar fashion. Once we have made these
changes, the
EJB
container will wire together the
PlaceOrderFacade
and its com-
ponents and configure the repositories with the
EntityManager
.
EJB 3
dependency injection is certainly a simple yet effective mechanism for
wiring together components that are implemented as
EJB
s. Unfortunately, it
might not make sense or even be possible to implement all of the components as
session beans. Let's look at how to inject
POJO
s into session beans.
10.4.2
Integrating Spring and EJB dependency injection
The Spring framework has a very powerful dependency injection mechanism.
Spring beans are arbitrary
POJO
s and can be injected with other Spring beans as
Search WWH ::
Custom Search