Java Reference
In-Depth Information
public void create() throws Exception {
bb
InitialContext ctx = new InitialContext();
bb
Reference reference = new Reference(
bbbb
PlaceOrderService.class.getName(),
bbbb
new StringRefAddr("beanName",
bbbb
"PlaceOrderService"),
bbbb
SpringObjectFactory.class.getName(),
bbbb
null);
bb
ctx.bind("MyTestSpringBean", reference);
}
public void destroy() throws Exception {
bb
InitialContext ctx = new InitialContext();
bb
ctx.unbind("MyTestSpringBean");
}
}
In this listing, the
@Service
annotation indicates that
SpringBeanReferenceIni-
tializer
is a service
POJO
. The
create()
method then creates a
Reference
and
binds a name to it, and the
destroy()
method unbinds the reference.
As you can see, initializing the
JNDI
references when using
JB
oss requires the
definition of a simple
POJO
service. The only other thing we must do is ensure
that the
SpringBeanReferenceInitializer
is created before any of the
EJB
s that
reference the Spring beans. Let's look at how to do this.
Annotating the session bean class
The next step is to annotate the session beans to tell the
EJB
container to inject
the Spring beans. There are three different annotations that we must use:
■
@Resource
is used on each field that references a
POJO
. It tells the
EJB
con-
tainer to inject the object bound to the specified
JNDI
name.
■
@PersistenceContexts
is used on the bean class to bind a
JNDI
name to the
EntityManager
so that it can be looked up the Spring
JndiObjectFactory-
Bean
, as we explain a bit later, and injected into the
POJO
repositories.
@Depends
is a
JB
oss-specific annotation that is used on the bean class to
ensure that the
SpringBeanReferenceInitializer
binds the
JNDI
references
before the
EJB
is deployed.
■
Here is an example of a stateless bean class that uses these annotations:
@Stateless
@Depends("jboss.j2ee:service=EJB3,type=service,
bb
➥
name=net.chrisrichardson.foodToGo.ejb3.service.
bbbbbb
➥
SpringBeanReferenceInitializerLocal")
Search WWH ::
Custom Search