來自: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:
2.創建一個EJB 3.0 Session bean.
The Interface:
The Implementation Class:
3.編譯,打包,部署到一個應用服務器上。
我使用GlassFish,用缺省的持久化提供工具TopLink,persistence.xml文件配置如下:
當你的應用部署以后,確認session bean JNDI名稱,在 GlassFish 中,點擊JNDI瀏覽工具按鈕查看:
4: 測試無狀態 Session beans.
5: 創建一個 Spring Bean.
創建一個CustomerManager 接口:
6: 注入 EJB 3.0 Session bean 進入我們的 Spring Beans.
7: 測試
整個過程結束,使用Spring和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代碼
- package com.ejb.domain;
- import java.io.Serializable;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.Id;
- import javax.persistence.Table;
- /**
- *
- * @author meerasubbarao
- */
- @Entity
- @Table(name = "CUSTOMER", catalog = "", schema = "ADMIN")
- public class Customer implements Serializable {
- private static final long serialVersionUID = 1L;
- @Id
- @Column(name = "CUSTOMER_ID")
- private Long customerId;
- @Column(name = "FIRST_NAME")
- private String firstName;
- @Column(name = "LAST_NAME")
- private String lastName;
- @Column(name = "MIDDLE_NAME")
- private String middleName;
- @Column(name = "EMAIL_ID")
- private String emailId;
- public Customer() {
- }
- public Customer(Long customerId) {
- this.customerId = customerId;
- }
- public Long getCustomerId() {
- return customerId;
- }
- public void setCustomerId(Long customerId) {
- this.customerId = customerId;
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public String getMiddleName() {
- return middleName;
- }
- public void setMiddleName(String middleName) {
- this.middleName = middleName;
- }
- public String getEmailId() {
- return emailId;
- }
- public void setEmailId(String emailId) {
- this.emailId = emailId;
- }
- }
2.創建一個EJB 3.0 Session bean.
The Interface:
Java代碼
- package com.ejb.service;
- import com.ejb.domain.Customer;
- import java.util.Collection;
- import javax.ejb.Remote;
- /**
- *
- * @author meerasubbarao
- */
- @Remote
- public interface CustomerService {
- Customer create(Customer info);
- Customer update(Customer info);
- void remove(Long customerId);
- Collection<Customer> findAll();
- Customer[] findAllAsArray();
- Customer findByPrimaryKey(Long customerId);
- }
The Implementation Class:
Java代碼
- package com.ejb.service;
- import com.ejb.domain.Customer;
- import java.util.Collection;
- import javax.ejb.Stateless;
- import javax.jws.WebService;
- import javax.jws.soap.SOAPBinding;
- import javax.persistence.EntityManager;
- import javax.persistence.PersistenceContext;
- import javax.persistence.Query;
- import javax.jws.WebMethod;
- /**
- *
- * @author meerasubbarao
- */
- @WebService(name = "CustomerService", serviceName = "CustomerService", targetNamespace = "urn:CustomerService")
- @SOAPBinding(style = SOAPBinding.Style.RPC)
- @Stateless(name = "CustomerService")
- public class CustomerServiceImpl implements CustomerService {
- @PersistenceContext
- private EntityManager manager;
- @WebMethod
- public Customer create(Customer info) {
- this.manager.persist(info);
- return info;
- }
- @WebMethod
- public Customer update(Customer info) {
- return this.manager.merge(info);
- }
- @WebMethod
- public void remove(Long customerId) {
- this.manager.remove(this.manager.getReference(Customer.class, customerId));
- }
- public Collection<Customer> findAll() {
- Query query = this.manager.createQuery("SELECT c FROM Customer c");
- return query.getResultList();
- }
- @WebMethod
- public Customer[] findAllAsArray() {
- Collection<Customer> collection = findAll();
- return (Customer[]) collection.toArray(new Customer[collection.size()]);
- }
- @WebMethod
- public Customer findByPrimaryKey(Long customerId) {
- return (Customer) this.manager.find(Customer.class, customerId);
- }
- }
3.編譯,打包,部署到一個應用服務器上。
我使用GlassFish,用缺省的持久化提供工具TopLink,persistence.xml文件配置如下:
Xml代碼
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <persistence-unit name="SpringAndEJBPU" transaction-type="JTA">
- <provider>oracle.toplink.essentials.PersistenceProvider</provider>
- <jta-data-source>spring-ejb</jta-data-source>
- <properties>
- <property name="toplink.ddl-generation" value="drop-and-create-tables"/>
- </properties>
- </persistence-unit>
- </persistence>
當你的應用部署以后,確認session bean JNDI名稱,在 GlassFish 中,點擊JNDI瀏覽工具按鈕查看:

4: 測試無狀態 Session beans.

5: 創建一個 Spring Bean.
創建一個CustomerManager 接口:
Java代碼
- package com.spring.service;
- import com.ejb.domain.Customer;
- /**
- *
- * @author meerasubbarao
- */
- public interface CustomerManager {
- public void addCustomer(Customer customer);
- public void removeCustomer(Long customerId);
- public Customer[] listCustomers();
- }
Java代碼
- package com.spring.service;
- import com.ejb.domain.Customer;
- import com.ejb.service.CustomerService;
- /**
- *
- * @author meerasubbarao
- */
- public class CustomerManagerImpl implements CustomerManager {
- CustomerService customerService;
- public void setCustomerService(CustomerService customerService) {
- this.customerService = customerService;
- }
- public void removeCustomer(Long customerId) {
- customerService.remove(customerId);
- }
- public Customer[] listCustomers() {
- return customerService.findAllAsArray();
- }
- public void addCustomer(Customer customer) {
- customerService.create(customer);
- }
- }
6: 注入 EJB 3.0 Session bean 進入我們的 Spring Beans.
Xml代碼
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:jee="http://www.springframework.org/schema/jee"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd">
- <jee:jndi-lookup id="customerService" <b>jndi-name="com.ejb.service.CustomerService"</b>>
- </jee:jndi-lookup>
- <bean id="manageCustomer"
- class="com.spring.service.CustomerManagerImpl">
- <property name="customerService" ref="customerService" />
- </bean>
- </beans>
Java代碼
- <jee:jndi-lookup id="customerService" jndi-name="com.ejb.service.CustomerService">
- </jee:jndi-lookup>
Java代碼
- <bean id="manageCustomer"
- class="com.spring.service.CustomerManagerImpl">
- <property name="customerService" ref="customerService" />
- </bean>
7: 測試
Java代碼
- package com.spring.client;
- import com.ejb.domain.Customer;
- import com.spring.service.CustomerManager;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class SpringAndEJBMain {
- public static void main(String[] args) {
- ApplicationContext context =
- new ClassPathXmlApplicationContext("SpringXMLConfig.xml");
- CustomerManager service = (CustomerManager) context.getBean("manageCustomer");
- Customer customer = new Customer();
- customer.setFirstName("Meera");
- customer.setLastName("Subbarao");
- customer.setMiddleName("B");
- customer.setEmailId("meera@springandejb.com");
- customer.setCustomerId(new Long(1));
- service.addCustomer(customer);
- for (Customer cust : service.listCustomers()) {
- System.out.println(cust.getFirstName());
- System.out.println(cust.getLastName());
- System.out.println(cust.getMiddleName());
- System.out.println(cust.getEmailId());
- }
- service.removeCustomer(new Long(1));
- }
- }


整個過程結束,使用Spring和EJB 3.0能夠同時或者兩者的好處。