Java Reference
In-Depth Information
@PersistenceContexts( {
b
➥
@PersistenceContext(name = "EntityManager") })
public class PlaceOrderFacadeUsingIntegratedDependencyInjectImpl
implements
PlaceOrderFacadeUsingIntegratedDependencyInject {
@Resource(name = "RestaurantRepository")
private RestaurantRepository restaurantRepository;
@Resource(name = "PlaceOrderFacadeResultFactory")
private PlaceOrderFacadeResultFactory resultFactory;
@Resource(name = "PlaceOrderService")
private PlaceOrderService service;
…
}
When the
EJB
container instantiates the
PlaceOrderFacadeUsingIntegratedDepen-
dencyInjectImpl
class, it looks up the
JNDI
names specified by the
@Resource
annotations and retrieves the
POJO
s. Because those
JNDI
names are bound to ref-
erences, the
JNDI
implementation will end up calling the Spring bean factory,
which will create those
POJO
s and their dependencies. When the Spring bean fac-
tory creates the repositories, it will do a
JNDI
lookup to retrieve the
EntityManager
that is injected into the repositories. Let's take a look.
Injecting the EntityManager into the repositories
The Spring bean factory must inject the
EntityManager
into the repositories. The
@PersistenceContext
annotation on the session bean class binds a
JNDI
name to
the
EntityManager
, and so we just need to use the Spring
JndiObjectFactoryBean
,
which is a Spring
FactoryBean
that looks up a
JNDI
object. One complication,
however, is that the
EntityManager
is not bound when the Spring bean factory is
called. This means that we must configure the
JndiObjectFactoryBean
to delay
looking up
EntityManager
until the first time the application calls it. This is
accomplished by using the
JndiObjectFactoryBean
's
lookupOnStartup
and
proxy-
Interface
properties. Here are the bean definitions for the
JndiObjectFactory-
Bean
and an example repository:
<beans>
…
<bean id="EntityManager"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp.ejb3/env/
bbbbbb
bb
➥
EntityManager</value>
</property>
<property name="lookupOnStartup">
Specifies JNDI name to look up
Delays lookup until first access
Search WWH ::
Custom Search