Java Reference
In-Depth Information
5-4. Creating EJB 3.0 Components in Spring
Problem
Happily, creating EJB 3.0 components is much simpler than with EJB 2.x. Indeed, an EJB can be as
simple as a business interface and a POJO implementation class. In EJB 3.1, even this restriction is lifted,
so that_like Spring_you can simply specify a POJO and expose that a service. However, writing EJB 3
beans could be unpleasant if you need access to beans in a Spring application context. Without special
support, there is no way to have beans auto-wired into the EJB.
Solution
Use the org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor interceptor to let
Spring configure @Autowired elements on your EJB.
How It Works
First, build an EJB component. You will need to specify an implementation and at minimum a remote
interface. The business interface for our EJB 3.0 stateless session bean will be the same as for the EJB 2.0
example. The bean implementation class can be a simple Java class that implements this interface and is
annotated with the EJB annotations. A remote stateless session bean requires the @Stateless and
@Remote annotations. In the @Remote annotation, you have to specify the remote interface for this EJB
component.
package com.apress.springenterpriserecipes.post;
public interface PostageService {
public double calculatePostage(String country, double weight);
}
Next, we must create an implementation.
package com.apress.springenterpriserecipes.post;
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Stateless
@Remote( { PostageService.class })
public class PostageServiceBean implements PostageService {
public double calculatePostage(String country, double weight) {
return 1.0;
}
}
You specify the remote interface for the session bean using the @Remote annotation that decorates
the class. That is all that's required for the coding to create a working EJB3 bean. Compile and then
package these classes into a .jar . Then, deploy them to OpenEJB in the same manner as for the EJB 2.x
example.
 
Search WWH ::




Custom Search