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 禮物 閱讀(658) 評論(0)  編輯  收藏 所屬分類: ejb3.0
          主站蜘蛛池模板: 五家渠市| 天峨县| 沿河| 绥阳县| 安化县| 辽阳市| 吴忠市| 南靖县| 南川市| 柏乡县| 遂昌县| 潮州市| 黄骅市| 神池县| 太康县| 兴文县| 青海省| 宁武县| 新营市| 台安县| 南召县| 景德镇市| 凤城市| 康保县| 简阳市| 马龙县| 涟源市| 兴城市| 陕西省| 滁州市| 搜索| 广东省| 基隆市| 老河口市| 四平市| 德江县| 兴安盟| 莱芜市| 巴楚县| 屯留县| 仙桃市|