Java Reference
In-Depth Information
Note To compile an EJB component against OpenEJB, you'll need the javaee-api-5.0-2 jar from the lib
folder on your classpath.
Thus far, you've created a very simple working service in almost no code, and haven't needed to
use Spring. Let's have Spring inject a resource, which might be useful for a host of reasons: proxying
Spring services with EJB3s, injecting custom resources configured in Spring, or even using Spring to
isolate your EJBs from acquiring references to other distributed resources such as a REST endpoint or an
RMI endpoint.
To do this, use Spring's SpringBeanAutowiringInterceptor class to provide configuration for the EJB.
Here is the implementation class ( PostageServiceBean ) with a few extra lines. First: an @Interceptors
annotation decorates the PostageServiceBean . This tells Spring to handle @Autowired injection points
in the class. The interceptor obtains beans, by default, from a ContextSingletonBeanFactoryLocation ,
which in turn looks for an XML application context named beanRefContext.xml , which is presumed to be
on the classpath. The second new line of interest here is an example injection of a Spring JdbcTemplate
instance.
package com.apress.springenterpriserecipes.post;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor;
@Stateless
@Remote( { PostageService.class })
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class PostageServiceBean implements PostageService {
@Autowired
private JdbcTemplate jdbcTemplate;
public double calculatePostage(String country, double weight) {
// use the jdbcTemplate …
return 1.0;
}
}
5-5. Accessing EJB 3.0 Components in Spring
Problem
EJB 3.0 offers some improvements on EJB 2.x. First, the EJB interface is a simple Java interface whose
methods don't throw RemoteException , while the implementation class is a simple Java class annotated
with EJB annotations. Moreover, the concept of home interface has been eliminated in order to simplify
the EJB lookup process. You can look up an EJB reference from JNDI directly in EJB 3.0. However, the
JNDI lookup code is still complex, and you also have to handle NamingException .
 
Search WWH ::




Custom Search