Java Reference
In-Depth Information
Solution
By using Spring's JndiObjectFactoryBean , you can easily declare a JNDI object reference in the Spring
IoC container. You can use this factory bean to declare a reference to an EJB 3.0 component.
How It Works
Now that you've created and deployed an EJB component, you can create a client. If you have chosen
OpenEJB as your EJB container, the default JNDI name for a remote EJB 3.0 component is the EJB class
name with Remote as its suffix ( PostageServiceBeanRemote, in this case). Note that the JNDI name isn't
specified in the standard in EJB 3.0, so this may change from container to container. In EJB 3.1, this is
remedied, and beans are prescribed a predictable naming scheme so that bean JNDI names are portable
across implementations.
Accessing EJB 3.0 Components with Spring's Support
Accessing EJB 3.0 components is simpler than EJB 2.x. You can look up an EJB reference from JNDI
directly without looking up its home interface first, so you don't need to handle CreateException .
Moreover, the EJB interface is a business interface that doesn't throw a RemoteException .
Although accessing EJB 3.0 components is simpler than EJB 2.x, the JNDI lookup code is still too
complex, and you have to handle NamingException . With Spring's support, your FrontDeskImpl class can
define a setter method for this EJB component's business interface for Spring to inject the EJB reference
that is looked up from JNDI.
package com.apress.springenterpriserecipes.post;
public class FrontDeskImpl implements FrontDesk {
private PostageService postageService;
public void setPostageService(PostageService postageService) {
this.postageService = postageService;
}
public double calculatePostage(String country, double weight) {
return postageService.calculatePostage(country, weight);
}
}
Spring offers the factory bean JndiObjectFactoryBean to declare a JNDI object reference in its IoC
container. You declare this bean in the front desk system's bean configuration file.
<beans xmlns=" http://www.springframework.org/schema/beans"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
Search WWH ::




Custom Search