为了创建一个简单的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