爲了創建一個簡單的EJB模塊,我們將使用NetBeans“New project”嚮導。在下面給出的示例中,我們將創建一個名爲Component的EJB模塊項目。
一、創建項目
在NetBeans IDE中,選擇File>New Project>。您將看到以下屏幕。
在類別Java EE下選擇project type,project type作爲EJB模塊。單擊 next 按鈕。您將看到以下屏幕。
輸入項目名稱和位置。單擊 next 按鈕。您將看到以下屏幕。
選擇JBoss應用服務器。單擊“完成”按鈕。您將看到NetBeans創建的以下項目。
二、創建EJB示例
創建一個簡單的EJB,我們將使用NetBeans中的“New”嚮導。在下面給出的示例中,我們將在EjbComponent項目下創建一個名爲librarySessionBean的無狀態EJB類。
在project explorer窗口中選擇project EjbComponent並右鍵單擊它。選擇,New > Session Bean.。您將看到新會話Bean嚮導。
輸入會話bean名稱和包名稱。單擊“完成”按鈕。您將看到以下由NetBeans創建的EJB類。
-
LibrarySessionBean − stateless session bean
-
LibrarySessionBeanLocal − local interface for session bean
我正在將本地接口更改爲遠程接口,因爲我們將在基於控制檯的應用程序中訪問我們的EJB。遠程/本地接口用於公開EJB必須實現的業務方法。
LibrarySessionBeanLocal重命名爲LibrarySessionBeanRemote,LibrarySessionBean實現LibrarySessionBeanRemote接口。
LibrarySessionBeanRemote
package com.tutorialspoint.stateless;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface LibrarySessionBeanRemote {
void addBook(String bookName);
List getBooks();
}
LibrarySessionBean
package com.tutorialspoint.stateless;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
@Stateless
public class LibrarySessionBean implements LibrarySessionBeanRemote {
List<String> bookShelf;
public LibrarySessionBean() {
bookShelf = new ArrayList<String>();
}
public void addBook(String bookName) {
bookShelf.add(bookName);
}
public List<String> getBooks() {
return bookShelf;
}
}
這個博客先分享到這裏,下個博客 應用創造中 繼續。
參考:
https://www.tutorialspoint.com/ejb/ejb_create_application.htm