Terry.Li-彬

虚其心,可解天下之问;专其心,可治天下之学;静其心,可悟天下之理;恒其心,可成天下之业。

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  143 随笔 :: 344 文章 :: 130 评论 :: 0 Trackbacks
来自:http://www.javaeye.com/news/3370-ejb-3-0-and-spring-2-5-the-use-of-spring-in-ejb-3-0
Meera Subbarao说道:EJB和Spring社区的开发者为什么总是贬低对方呢?我同时使用EJB和Spring,就像所有的开发者一样,我对于两者需要大量的XML设置非常头疼,但是从Java 5发布以来,XML配置已经用annotation来替代了。但是在使用了最新的Spring 2.5和EJB 3.0,我觉得它们是互相补充的关系,而非相互竞争关系。

许多开发者理解,Spring是由Spring Source创建的最常用的非标准框架,而EJB 3.0是一个由主要的JEE厂商创建的规格。我以前曾一起工作的同事更愿意使用标准规格,选择EJB 2.X现在迁移到EJB 3.0。也有开发者愿意使用Spring而拒绝EJB。但是没有任何东西阻止开发者同时使用Spring和EJB,对不对?在Spring的配置文件增加几行就能够在Spring中无缝使用EJB 3.0组件。

下面我将展示这个过程是多么简单,我们可以通过Spring的强大的依赖注入机制来注入Customer session bean。这个Customer session bean可以使用Entity Manager来进行创建/读写/删除操作。

1。创建一个简单的JPA Entity:

Java代码
  1. package com.ejb.domain;  
  2.   
  3. import java.io.Serializable;  
  4. import javax.persistence.Column;  
  5. import javax.persistence.Entity;  
  6. import javax.persistence.Id;  
  7. import javax.persistence.Table;  
  8.   
  9. /** 
  10.  * 
  11.  * @author meerasubbarao 
  12.  */  
  13. @Entity  
  14. @Table(name = "CUSTOMER", catalog = "", schema = "ADMIN")  
  15. public class Customer implements Serializable {  
  16.     private static final long serialVersionUID = 1L;  
  17.     @Id  
  18.     @Column(name = "CUSTOMER_ID")  
  19.     private Long customerId;  
  20.     @Column(name = "FIRST_NAME")  
  21.     private String firstName;  
  22.     @Column(name = "LAST_NAME")  
  23.     private String lastName;  
  24.     @Column(name = "MIDDLE_NAME")  
  25.     private String middleName;  
  26.     @Column(name = "EMAIL_ID")  
  27.     private String emailId;  
  28.   
  29.     public Customer() {  
  30.     }  
  31.   
  32.     public Customer(Long customerId) {  
  33.         this.customerId = customerId;  
  34.     }  
  35.   
  36.     public Long getCustomerId() {  
  37.         return customerId;  
  38.     }  
  39.   
  40.     public void setCustomerId(Long customerId) {  
  41.         this.customerId = customerId;  
  42.     }  
  43.   
  44.     public String getFirstName() {  
  45.         return firstName;  
  46.     }  
  47.   
  48.     public void setFirstName(String firstName) {  
  49.         this.firstName = firstName;  
  50.     }  
  51.   
  52.     public String getLastName() {  
  53.         return lastName;  
  54.     }  
  55.   
  56.     public void setLastName(String lastName) {  
  57.         this.lastName = lastName;  
  58.     }  
  59.   
  60.     public String getMiddleName() {  
  61.         return middleName;  
  62.     }  
  63.   
  64.     public void setMiddleName(String middleName) {  
  65.         this.middleName = middleName;  
  66.     }  
  67.   
  68.     public String getEmailId() {  
  69.         return emailId;  
  70.     }  
  71.   
  72.     public void setEmailId(String emailId) {  
  73.         this.emailId = emailId;  
  74.     }  
  75.   
  76.   
  77. }  



2.创建一个EJB 3.0 Session bean.

The Interface:

Java代码
  1. package com.ejb.service;  
  2.   
  3. import com.ejb.domain.Customer;  
  4. import java.util.Collection;  
  5. import javax.ejb.Remote;  
  6.   
  7. /** 
  8.  * 
  9.  * @author meerasubbarao 
  10.  */  
  11. @Remote  
  12. public interface CustomerService {  
  13.   
  14.     Customer create(Customer info);  
  15.   
  16.     Customer update(Customer info);  
  17.   
  18.     void remove(Long customerId);  
  19.   
  20.     Collection<Customer> findAll();  
  21.   
  22.     Customer[] findAllAsArray();  
  23.   
  24.     Customer findByPrimaryKey(Long customerId);  
  25. }  


The Implementation Class:

Java代码
  1. package com.ejb.service;  
  2.   
  3. import com.ejb.domain.Customer;  
  4. import java.util.Collection;  
  5. import javax.ejb.Stateless;  
  6. import javax.jws.WebService;  
  7. import javax.jws.soap.SOAPBinding;  
  8. import javax.persistence.EntityManager;  
  9. import javax.persistence.PersistenceContext;  
  10. import javax.persistence.Query;  
  11. import javax.jws.WebMethod;  
  12.   
  13. /** 
  14.  * 
  15.  * @author meerasubbarao 
  16.  */  
  17. @WebService(name = "CustomerService", serviceName = "CustomerService", targetNamespace = "urn:CustomerService")  
  18. @SOAPBinding(style = SOAPBinding.Style.RPC)  
  19. @Stateless(name = "CustomerService")  
  20. public class CustomerServiceImpl implements CustomerService {  
  21.   
  22.     @PersistenceContext  
  23.     private EntityManager manager;  
  24.   
  25.     @WebMethod  
  26.     public Customer create(Customer info) {  
  27.         this.manager.persist(info);  
  28.         return info;  
  29.     }  
  30.   
  31.     @WebMethod  
  32.     public Customer update(Customer info) {  
  33.         return this.manager.merge(info);  
  34.     }  
  35.   
  36.     @WebMethod  
  37.     public void remove(Long customerId) {  
  38.         this.manager.remove(this.manager.getReference(Customer.class, customerId));  
  39.     }  
  40.   
  41.     public Collection<Customer> findAll() {  
  42.         Query query = this.manager.createQuery("SELECT c FROM Customer c");  
  43.         return query.getResultList();  
  44.     }  
  45.   
  46.     @WebMethod  
  47.     public Customer[] findAllAsArray() {  
  48.         Collection<Customer> collection = findAll();  
  49.         return (Customer[]) collection.toArray(new Customer[collection.size()]);  
  50.     }  
  51.   
  52.     @WebMethod  
  53.     public Customer findByPrimaryKey(Long customerId) {  
  54.         return (Customer) this.manager.find(Customer.class, customerId);  
  55.     }  
  56.   
  57.     
  58. }  


3.编译,打包,部署到一个应用服务器上。

我使用GlassFish,用缺省的持久化提供工具TopLink,persistence.xml文件配置如下:

Xml代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">  
  3.   <persistence-unit name="SpringAndEJBPU" transaction-type="JTA">  
  4.     <provider>oracle.toplink.essentials.PersistenceProvider</provider>  
  5.     <jta-data-source>spring-ejb</jta-data-source>  
  6.     <properties>  
  7.       <property name="toplink.ddl-generation" value="drop-and-create-tables"/>  
  8.     </properties>  
  9.   </persistence-unit>  
  10. </persistence>  


当你的应用部署以后,确认session bean JNDI名称,在 GlassFish 中,点击JNDI浏览工具按钮查看:



4: 测试无状态 Session beans.



5: 创建一个 Spring Bean.

创建一个CustomerManager 接口:

Java代码
  1. package com.spring.service;  
  2.   
  3. import com.ejb.domain.Customer;  
  4.   
  5. /** 
  6.  * 
  7.  * @author meerasubbarao 
  8.  */  
  9. public interface CustomerManager {  
  10.   
  11.     public void addCustomer(Customer customer);  
  12.     public void removeCustomer(Long customerId);  
  13.     public Customer[] listCustomers();  
  14.   
  15.   
  16. }  


Java代码
  1. package com.spring.service;  
  2.   
  3. import com.ejb.domain.Customer;  
  4. import com.ejb.service.CustomerService;  
  5.   
  6. /** 
  7.  * 
  8.  * @author meerasubbarao 
  9.  */  
  10. public class CustomerManagerImpl implements CustomerManager {  
  11.   
  12.     CustomerService customerService;  
  13.   
  14.     public void setCustomerService(CustomerService customerService) {  
  15.         this.customerService = customerService;  
  16.     }  
  17.   
  18.     public void removeCustomer(Long customerId) {  
  19.         customerService.remove(customerId);  
  20.     }  
  21.   
  22.     public Customer[] listCustomers() {  
  23.         return customerService.findAllAsArray();  
  24.     }  
  25.   
  26.     public void addCustomer(Customer customer) {  
  27.         customerService.create(customer);  
  28.     }  
  29. }  


6: 注入 EJB 3.0 Session bean 进入我们的 Spring Beans.

Xml代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:context="http://www.springframework.org/schema/context"  
  5.        xmlns:jee="http://www.springframework.org/schema/jee"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  8.        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">  
  9.     <jee:jndi-lookup id="customerService" <b>jndi-name="com.ejb.service.CustomerService"</b>>  
  10.     </jee:jndi-lookup>  
  11.     <bean id="manageCustomer"  
  12.         class="com.spring.service.CustomerManagerImpl">  
  13.         <property name="customerService" ref="customerService" />  
  14.     </bean>  
  15. </beans>  


Java代码
  1. <jee:jndi-lookup id="customerService" jndi-name="com.ejb.service.CustomerService">  
  2. </jee:jndi-lookup>  


Java代码
  1. <bean id="manageCustomer"  
  2.     class="com.spring.service.CustomerManagerImpl">  
  3.     <property name="customerService" ref="customerService" />  
  4. </bean>  


7: 测试

Java代码
  1. package com.spring.client;  
  2.   
  3. import com.ejb.domain.Customer;  
  4. import com.spring.service.CustomerManager;  
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. public class SpringAndEJBMain {  
  9.   
  10.     public static void main(String[] args) {  
  11.         ApplicationContext context =  
  12.                 new ClassPathXmlApplicationContext("SpringXMLConfig.xml");  
  13.   
  14.         CustomerManager service = (CustomerManager) context.getBean("manageCustomer");  
  15.         Customer customer = new Customer();  
  16.         customer.setFirstName("Meera");  
  17.         customer.setLastName("Subbarao");  
  18.         customer.setMiddleName("B");  
  19.         customer.setEmailId("meera@springandejb.com");  
  20.         customer.setCustomerId(new Long(1));  
  21.   
  22.         service.addCustomer(customer);  
  23.         for (Customer cust : service.listCustomers()) {  
  24.             System.out.println(cust.getFirstName());  
  25.             System.out.println(cust.getLastName());  
  26.             System.out.println(cust.getMiddleName());  
  27.             System.out.println(cust.getEmailId());  
  28.   
  29.         }  
  30.         service.removeCustomer(new Long(1));  
  31.   
  32.     }  
  33. }  






整个过程结束,使用Spring和EJB 3.0能够同时或者两者的好处。
posted on 2009-04-17 17:28 礼物 阅读(648) 评论(0)  编辑  收藏 所属分类: ejb3.0