Java Reference
In-Depth Information
EJB Container
EJB
References
JNDI Reference
Creates
Spring Bean Factory
POJO
POJO
POJO
Figure 10.4
Using a JNDI
Reference
to a Spring bean
Here is an example of some code that stores a reference to the
PlaceOrderService
Spring bean in the
JNDI
tree:
Context ctx = new InitialContext();
Reference reference = new Reference(
PlaceOrderService
.class.getName(),
new StringRefAddr("beanName",
"PlaceOrderService"
),
SpringObjectFactory.class.getName(),
null);
ctx.bind("
PlaceOrderService
", reference);
This code fragment instantiates a
Reference
that contains the name of the
Place-
OrderService
class, the name of the Spring bean, and the fully qualified class
name of the
SpringObjectFactory
, which is a
JNDI
object factory that calls
Bean-
Factory.getBean()
. It then binds the name “PlaceOrderService” to the reference
in the
JNDI
tree.
When the
JNDI
implementation encounters this reference during a lookup, it
calls
SpringObjectFactory
to create the object. This class defines a
getObject-
Instance()
method that creates a Spring bean by calling a Spring bean factory:
public class SpringObjectFactory implements
ObjectFactory {
public Object getObjectInstance(Object reference,
Name name, Context nameCtx,
Hashtable<?, ?> environment)
Search WWH ::
Custom Search