Ejb全称为Enterprise Java Bean Component,也称为服务器端软件组件;它将主要用作应用程序的业务逻辑。用于运行时环境的 Web 容器包括软件组件、计算机安全、servlet 生命周期管理、事务处理和 Web 服务。 EJb 是运行在计算机网络服务器端的 Java 编程语言编写的架构风格,因此它在应用程序中遵循客户端-服务器模型。而且,ejb 是一种在客户端分发 Web 组件的 Java bean 技术,它具有在多个 Web 应用程序中称为可重用组件的功能。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
语法:
java编程中EJB模型的基本语法如下。
import javax.ejb.*; import java packages; class classname { Main method() { -----Some programming logics---- } }
以上代码是使用ejb包开发应用程序的基本步骤。
以下是提到的示例:
界面:
package JPAEJB; import java.util.List; import javax.ejb.Remote; @Remote public interface CustomerInterface { void addBooks(String name); List getBooks(); }
实施:
import java.util.ArrayList; import java.util.List; import javax.ejb.Stateless; import JPAEJB.CustomerInterface; @Stateless public class CustomerClass implements CustomerInterface { List<String>books; public CustomerClass() { books = new ArrayList<String>(); } public void addBooks(String name) { books.add(name); } public List<String> getBooks() { return books; } }
客户界面:
import JPAEJB.CustomerInterface; 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 CustomerInterfaceImplem { BufferedReader brConsoleReader = null; Properties p; InitialContext c; { p = new Properties(); try { p.load(new FileInputStream("jndi.properties")); } catch (IOException ex) { ex.printStackTrace(); } try { c = new InitialContext(p); } catch (NamingException ex) { ex.printStackTrace(); } brConsoleReader = new BufferedReader(new InputStreamReader(System.in)); } public static void main(String[] args) { CustomerInterfaceImplem cust = new CustomerInterfaceImplem(); cust.testStatelessEjb(); } private void show() { System.out.println("Welcome to my domain"); System.out.print("Options \n1. Add the Books\n2. Exit \nEnter your Choice: "); } private void StatelessEjb() { try { intc = 1; CustomerInterface cust = (CustomerInterface)ctx.lookup("CustomerInterface/remote"); while (c != 2) { String books; show(); String str = brConsoleReader.readLine(); c = Integer.parseInt(str); if (c == 1) { books = brConsoleReader.readLine(); cust.addBooks(books); }elseif (c == 2) { break; } } List<String>books1 = cust.getBooks(); System.out.println(books1.size()); for (inti = 0; i<books1.size(); ++i) { System.out.println((i+1)+". " + books1.get(i)); } CustomerInterface cust1 = (CustomerInterface)ctx.lookup("cust/remote"); List<String>books2 = cust.getBooks(); System.out.println(books2.size()); for (inti = 0; i<books2.size(); ++i) { System.out.println((i+1)+". " + books2.get(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()); } } } }
示例输出:
Web 服务中的 EJB:
package JPAEJB; import java.util.List; import Customers.CustomerInterfaceImplem; public class Ejbclients{ public static void main(String[] args) { for(CustomerInterface cust:getBooks()) { System.out.println(cust.getBooks()); } } private static List <CustomerInterface> getBooks() { CustomerInterfaceImplem s = new CustomerInterfaceImplem(); CustomerInterface c = s.getCustomerInterfacePort(); return c.getBooks(); } }
EJB 安全性:
import javax.ejb.* @Stateless @DeclareRoles({"customer""books"}) public class CustSecurity implements CustomerInterface { @RolesAllowed({"books"}) public void delete(CustomerInterface cust) { System.out.println("customer delete the books"); } @PermitAll public void showBooks(CustomerInterface cust) { System.out.println("customer viewed the books"); } @DenyAll public void deleteAll() { System.out.println("customer delete all the books in libraries"); } }
安全 XML:
<?xml version="1.0"encoding="UTF-8"?> <!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 EJB 3.0//EN""http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd"> <ejb-jar> <security-role-mapping> <role-name>customer</role-name> <group-name>customer-groups</group-name> </security-role-mapping> <security-role-mapping> <role-name>Books</role-name> <group-name>Books-group</group-name> </security-role-mapping> <enterprise-beans/> </ejb-jar>
说明:以上三个示例是相同的输出,我们使用了ejb的不同功能,第一个示例我们使用客户将使用jndi(java命名目录接口)添加和删除书籍第二个示例我们使用客户的 Web 服务通过 Web 完成操作 最后一个示例我们使用了额外的安全性。
通常,EJB 充当业务逻辑应用程序之外的接口;与其他业务逻辑框架相比,它具有更多安全方面的浏览器兼容性功能。它还维护系统级交易。
以上是Java 中的 EJB的详细内容。更多信息请关注PHP中文网其他相关文章!