回調(diào)是由企業(yè)Bean的生命周期可以截獲的機(jī)制。 EJB 3.0規(guī)范已經(jīng)指定的回調(diào),而回調(diào)處理方法是要?jiǎng)?chuàng)建。 EJB容器調(diào)用這些回調(diào)。我們可以定義在EJB類本身或在一個(gè)單獨(dú)的類回調(diào)方法。 EJB 3.0已經(jīng)回調(diào)提供了許多注解。
下面是回調(diào)的注解無狀態(tài)bean的列表。
注解 | 描述 |
---|---|
@PostConstruct | 首次創(chuàng)建一個(gè)bean時(shí)的調(diào)用方法 |
@PreDestroy | 當(dāng)一個(gè)bean從Bean池中刪除或破壞時(shí)的調(diào)用方法。 |
下面是回調(diào)的注解狀態(tài)bean的列表。
注解 | 描述 |
---|---|
@PostConstruct | 首次創(chuàng)建一個(gè)bean時(shí)的調(diào)用方法 |
@PreDestroy | 當(dāng)一個(gè)bean從Bean池中刪除或破壞時(shí)的調(diào)用方法。 |
@PostActivate | 當(dāng)要加載一個(gè)bean時(shí)使用的調(diào)用方法。 |
@PrePassivate | 當(dāng)一個(gè)bean被放回Bean池的調(diào)用方法。 |
以下是回調(diào)的注解消息驅(qū)動(dòng)bean的列表。
注解 | 描述 |
---|---|
@PostConstruct | 首次創(chuàng)建一個(gè)豆時(shí)方法被調(diào)用 |
@PreDestroy | 當(dāng)一個(gè)bean從Bean池刪除或銷毀方法被調(diào)用。 |
以下是回調(diào)的注解為實(shí)體bean的列表。
注解 | 描述 |
---|---|
@PrePersist | 當(dāng)數(shù)據(jù)庫中創(chuàng)建一個(gè)實(shí)體的方法被調(diào)用。 |
@PostPersist | 在數(shù)據(jù)庫中創(chuàng)建一個(gè)實(shí)體后,調(diào)用方法。 |
@PreRemove | 當(dāng)一個(gè)實(shí)體從數(shù)據(jù)庫中刪除方法被調(diào)用。 |
@PostRemove | 一個(gè)實(shí)體從數(shù)據(jù)庫中刪除之后方法被調(diào)用。 |
@PreUpdate | 前一個(gè)實(shí)體是在數(shù)據(jù)庫中被更新方法被調(diào)用。 |
@PostLoad | 當(dāng)記錄從數(shù)據(jù)庫中讀取并加載到實(shí)體方法被調(diào)用。 |
讓我們創(chuàng)建一個(gè)測(cè)試EJB應(yīng)用測(cè)試EJB中各種回調(diào)。
步驟 | 描述 |
---|---|
1 | 用包com.tutorialspoint.stateless下一個(gè)名字EjbComponent在EJB作為解釋的創(chuàng)建項(xiàng)目-創(chuàng)建應(yīng)用程序一章。您也可以使用EJB創(chuàng)建的項(xiàng)目-持久章這樣本章以各種回調(diào)添加到EJB的。 |
2 | 創(chuàng)建LibrarySessionBean.java和LibrarySessionBeanRemote作為EJB解釋-創(chuàng)建應(yīng)用程序一章。保持不變的文件其余部分。 |
3 | 使用在創(chuàng)建EJB豆-持久的篇章。添加回調(diào)方法,如下圖所示。保持不變的文件其余部分。 |
4 | 創(chuàng)建包下com.tutorialspoint.callback一個(gè)Java類BookCallbackListener。本課程將演示回調(diào)方法的分離。 |
5 | 清理并生成應(yīng)用程序,確保業(yè)務(wù)邏輯正在按要求。 |
6 | 最后,部署JBoss應(yīng)用服務(wù)器上的jar文件的形式應(yīng)用。如果尚未啟動(dòng)JBoss應(yīng)用服務(wù)器將自動(dòng)被啟動(dòng)。 |
7 | 現(xiàn)在創(chuàng)建EJB客戶端,以同樣的方式一個(gè)基于控制臺(tái)的應(yīng)用程序在EJB解釋-創(chuàng)建應(yīng)用程序一章的主題創(chuàng)建客戶機(jī)訪問EJB。 |
package com.tutorialspoint.callback; import javax.persistence.PrePersist; import javax.persistence.PostLoad; import javax.persistence.PostPersist; import javax.persistence.PostRemove; import javax.persistence.PostUpdate; import javax.persistence.PreRemove; import javax.persistence.PreUpdate; import com.tutorialspoint.entity.Book; public class BookCallbackListener { @PrePersist public void prePersist(Book book){ System.out.println("BookCallbackListener.prePersist:" + "Book to be created with book id: "+book.getId()); } @PostPersist public void postPersist(Object book){ System.out.println("BookCallbackListener.postPersist::" + "Book created with book id: "+((Book)book).getId()); } @PreRemove public void preRemove(Book book) { System.out.println("BookCallbackListener.preRemove:" + " About to delete Book: " + book.getId()); } @PostRemove public void postRemove(Book book) { System.out.println("BookCallbackListener.postRemove::" + " Deleted Book: " + book.getId()); } @PreUpdate public void preUpdate(Book book) { System.out.println("BookCallbackListener.preUpdate::" + " About to update Book: " + book.getId()); } @PostUpdate public void postUpdate(Book book) { System.out.println("BookCallbackListener.postUpdate::" + " Updated Book: " + book.getId()); } @PostLoad public void postLoad(Book book) { System.out.println("BookCallbackListener.postLoad::" + " Loaded Book: " + book.getId()); } }
package com.tutorialspoint.entity; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.EntityListeners; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="books") public class Book implements Serializable{ private int id; private String name; public Book(){ } @Id @GeneratedValue(strategy= GenerationType.IDENTITY) @Column(name="id") public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.tutorialspoint.stateful; import java.util.ArrayList; import java.util.List; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.ejb.PostActivate; import javax.ejb.PrePassivate; import javax.ejb.Stateful; @Stateful public class LibraryStatefulSessionBean implements LibraryStatefulSessionBeanRemote { List<String> bookShelf; public LibraryStatefulSessionBean(){ bookShelf = new ArrayList<String>(); } public void addBook(String bookName) { bookShelf.add(bookName); } public List<String> getBooks() { return bookShelf; } @PostConstruct public void postConstruct(){ System.out.println("LibraryStatefulSessionBean.postConstruct::" + " bean created."); } @PreDestroy public void preDestroy(){ System.out.println("LibraryStatefulSessionBean.preDestroy:" + " bean removed."); } @PostActivate public void postActivate(){ System.out.println("LibraryStatefulSessionBean.postActivate:" + " bean activated."); } @PrePassivate public void prePassivate(){ System.out.println("LibraryStatefulSessionBean.prePassivate:" + " bean passivated."); } }
package com.tutorialspoint.stateful; import java.util.List; import javax.ejb.Remote; @Remote public interface LibraryStatefulSessionBeanRemote { void addBook(String bookName); List getBooks(); }
package com.tutorialspoint.stateless; import com.tutorialspoint.entity.Book; import java.util.List; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @Stateless public class LibraryPersistentBean implements LibraryPersistentBeanRemote { public LibraryPersistentBean(){} @PersistenceContext(unitName="EntityEjbPU") private EntityManager entityManager; public void addBook(Book book) { entityManager.persist(book); } public List<Book> getBooks() { return entityManager.createQuery("From Book") .getResultList(); } @PostConstruct public void postConstruct(){ System.out.println("postConstruct:: LibraryPersistentBean session bean" + " created with entity Manager object: "); } @PreDestroy public void preDestroy(){ System.out.println("preDestroy: LibraryPersistentBean session" + " bean is removed "); } }
package com.tutorialspoint.stateless; import com.tutorialspoint.entity.Book; import java.util.List; import javax.ejb.Remote; @Remote public interface LibraryPersistentBeanRemote { void addBook(Book bookName); List<Book> getBooks(); }
一旦你部署JBoss上EjbComponent項(xiàng)目,注意jboss的日志。
JBoss已經(jīng)具備自動(dòng)創(chuàng)建我們的會(huì)話bean JNDI入口- LibraryPersistentBean /remote。
我們將使用此查找字符串來獲得類型的遠(yuǎn)程業(yè)務(wù)對(duì)象- com.tutorialspoint.stateless.LibraryPersistentBeanRemote
... 16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI: LibraryPersistentBean/remote - EJB3.x Default Remote Business Interface LibraryPersistentBean/remote-com.tutorialspoint.stateless.LibraryPersistentBeanRemote - EJB3.x Remote Business Interface 16:30:02,723 INFO [SessionSpecContainer] Starting jboss.j2ee:jar=EjbComponent.jar,name=LibraryPersistentBean,service=EJB3 16:30:02,723 INFO [EJBContainer] STARTED EJB: com.tutorialspoint.stateless.LibrarySessionBeanRemote ejbName: LibraryPersistentBean ...
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces java.naming.provider.url=localhost
這些屬性用于初始化Java命名服務(wù)的InitialContext對(duì)象
InitialContext對(duì)象將被用于查找無狀態(tài)會(huì)話bean
package com.tutorialspoint.test; import com.tutorialspoint.stateful.LibrarySessionBeanRemote; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; import java.util.Properties; import javax.naming.InitialContext; import javax.naming.NamingException; public class EJBTester { BufferedReader brConsoleReader = null; Properties props; InitialContext ctx; { props = new Properties(); try { props.load(new FileInputStream("jndi.properties")); } catch (IOException ex) { ex.printStackTrace(); } try { ctx = new InitialContext(props); } catch (NamingException ex) { ex.printStackTrace(); } brConsoleReader = new BufferedReader(new InputStreamReader(System.in)); } public static void main(String[] args) { EJBTester ejbTester = new EJBTester(); ejbTester.testEntityEjb(); } private void showGUI(){ System.out.println("**********************"); System.out.println("Welcome to Book Store"); System.out.println("**********************"); System.out.print("Options 1. Add Book 2. Exit Enter Choice: "); } private void testEntityEjb(){ try { int choice = 1; LibraryPersistentBeanRemote libraryBean = (LibraryPersistentBeanRemote) ctx.lookup("LibraryPersistentBean/remote"); while (choice != 2) { String bookName; showGUI(); String strChoice = brConsoleReader.readLine(); choice = Integer.parseInt(strChoice); if (choice == 1) { System.out.print("Enter book name: "); bookName = brConsoleReader.readLine(); Book book = new Book(); book.setName(bookName); libraryBean.addBook(book); } else if (choice == 2) { break; } } List<Book> booksList = libraryBean.getBooks(); System.out.println("Book(s) entered so far: " + booksList.size()); int i = 0; for (Book book:booksList) { System.out.println((i+1)+". " + book.getName()); i++; } } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); }finally { try { if(brConsoleReader !=null){ brConsoleReader.close(); } } catch (IOException ex) { System.out.println(ex.getMessage()); } } } }
EJBTester執(zhí)行下列任務(wù)。
從jndi.properties加載屬性和初始化InitialContext對(duì)象。
在testStatelessEjb()方法,JNDI查找與名行 - “LibrarySessionBean /遠(yuǎn)程”,以獲得遠(yuǎn)程業(yè)務(wù)對(duì)象(無狀態(tài)EJB)。
然后用戶顯示庫存儲(chǔ)用戶界面和他(她)被要求輸入選擇/。
如果用戶輸入1,系統(tǒng)要求書名并保存使用無狀態(tài)會(huì)話bean addBook()方法的書。會(huì)話bean存儲(chǔ)該書在數(shù)據(jù)庫中。
如果用戶輸入2,系統(tǒng)檢索使用無狀態(tài)會(huì)話bean getBooks()方法,并退出書籍。
在項(xiàng)目資源管理器中找到EJBTester.java。右鍵單擊EJBTester類并選擇運(yùn)行文件 。
驗(yàn)證Netbeans的控制臺(tái)下面的輸出。
run: ********************** Welcome to Book Store ********************** Options 1. Add Book 2. Exit Enter Choice: 1 Enter book name: Learn Java ********************** Welcome to Book Store ********************** Options 1. Add Book 2. Exit Enter Choice: 2 Book(s) entered so far: 1 1. Learn Java BUILD SUCCESSFUL (total time: 13 seconds)
您可以在JBoss的日志中記錄以下條目回調(diào)
14:08:34,293 INFO [STDOUT] postConstruct:: LibraryPersistentBean session bean created with entity Manager object ... 16:39:09,484 INFO [STDOUT] BookCallbackListener.prePersist:: Book to be created with book id: 0 16:39:09,531 INFO [STDOUT] BookCallbackListener.postPersist:: Book created with book id: 1 16:39:09,900 INFO [STDOUT] BookCallbackListener.postLoad:: Loaded Book: 1 ...
更多建議: