- EJB教程
 - EJB - 主页
 - EJB - 概述
 - EJB - 环境设置
 - EJB - 创建应用程序
 - EJB-无状态Bean
 - EJB - 有状态 Bean
 - EJB-持久性
 - EJB - 消息驱动 Bean
 - EJB - 注释
 - EJB-回调
 - EJB-定时器服务
 - EJB - 依赖注入
 - EJB-拦截器
 - EJB - 可嵌入对象
 - EJB - Blob/Clob
 - EJB - 事务
 - EJB-安全性
 - EJB - JNDI 绑定
 - EJB - 实体关系
 - EJB - 访问数据库
 - EJB - 查询语言
 - EJB - 异常处理
 - EJB-Web 服务
 - EJB - 打包应用程序
 
- EJB 有用资源
 - EJB - 快速指南
 - EJB - 有用的资源
 - EJB - 讨论
 
EJB-拦截器
EJB 3.0 提供了使用 @AroundInvoke 注释注释的方法来拦截业务方法调用的规范。ejbContainer 在拦截业务方法调用之前调用拦截器方法。以下是拦截器方法的示例签名
@AroundInvoke
public Object methodInterceptor(InvocationContext ctx) throws Exception {
   System.out.println("*** Intercepting call to LibraryBean method: " 
   + ctx.getMethod().getName());
   return ctx.proceed();
}
拦截器方法可以在三个级别上应用或绑定。
默认- 为部署中的每个 bean 调用默认拦截器。默认拦截器只能通过 xml (ejb-jar.xml) 应用。
Class - 为 bean 的每个方法调用类级别拦截器。类级别拦截器可以通过 xml(ejb-jar.xml) 的注释来应用。
Method - 为 bean 的特定方法调用方法级拦截器。方法级拦截器可以通过 xml(ejb-jar.xml) 的注释来应用。
我们在这里讨论类级别的拦截器。
拦截器类
package com.tutorialspoint.interceptor;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
public class BusinessInterceptor {
   @AroundInvoke
   public Object methodInterceptor(InvocationContext ctx) throws Exception {
      System.out.println("*** Intercepting call to LibraryBean method: " 
      + ctx.getMethod().getName());
      return ctx.proceed();
   }
}
远程接口
import javax.ejb.Remote;
@Remote
public interface LibraryBeanRemote {
   //add business method declarations
}
拦截的无状态 EJB
@Interceptors ({BusinessInterceptor.class})
@Stateless
public class LibraryBean implements LibraryBeanRemote {
   //implement business method 
}
应用示例
让我们创建一个测试 EJB 应用程序来测试拦截的无状态 EJB。
| 步 | 描述 | 
|---|---|
| 1 | 按照EJB - 创建应用程序一章中的说明,在com.tutorialspoint.interceptor包下创建一个名为EjbComponent的项目。您还可以使用本章中的EJB - 创建应用程序章节中创建的项目来了解拦截的 EJB 概念。  | 
| 2 | 按照EJB - 创建应用程序一章中的说明,在com.tutorialspoint.interceptor包下创建LibraryBean.java和LibraryBeanRemote。保持其余文件不变。  | 
| 3 | 清理并构建应用程序,以确保业务逻辑按照要求运行。  | 
| 4 | 最后,将应用程序以jar文件的形式部署在JBoss应用服务器上。如果 JBoss 应用服务器尚未启动,它将自动启动。  | 
| 5 | 现在创建 ejb 客户端,这是一个基于控制台的应用程序,其方式与主题“创建客户端以访问 EJB”下的“EJB - 创建应用程序”一章中所述的方式相同。  | 
EJB组件(EJB模块)
LibraryBeanRemote.java
package com.tutorialspoint.interceptor;
import java.util.List;
import javax.ejb.Remote;
@Remote
public interface LibraryBeanRemote {
   void addBook(String bookName);
   List getBooks();
}
LibraryBean.java
package com.tutorialspoint.interceptor;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
@Interceptors ({BusinessInterceptor.class})
@Stateless
public class LibraryBean implements LibraryBeanRemote {
    
   List<String> bookShelf;    
   public LibraryBean() {
      bookShelf = new ArrayList<String>();
   }
   public void addBook(String bookName) {
      bookShelf.add(bookName);
   }    
   public List<String> getBooks() {
      return bookShelf;
   }   
}
在 JBOSS 上部署 EjbComponent 项目后,请注意 jboss 日志。
JBoss 已经自动为我们的会话 bean 创建了一个 JNDI 条目 - LibraryBean/remote。
我们将使用此查找字符串来获取类型为 - com.tutorialspoint.interceptor.LibraryBeanRemote的远程业务对象
JBoss应用服务器日志输出
... 16:30:01,401 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI: LibraryBean/remote - EJB3.x Default Remote Business Interface LibraryBean/remote-com.tutorialspoint.interceptor.LibraryBeanRemote - EJB3.x Remote Business Interface 16:30:02,723 INFO [SessionSpecContainer] Starting jboss.j2ee:jar=EjbComponent.jar,name=LibraryBean,service=EJB3 16:30:02,723 INFO [EJBContainer] STARTED EJB: com.tutorialspoint.interceptor.LibraryBeanRemote ejbName: LibraryBean 16:30:02,731 INFO [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI: LibraryBean/remote - EJB3.x Default Remote Business Interface LibraryBean/remote-com.tutorialspoint.interceptor.LibraryBeanRemote - EJB3.x Remote Business Interface ...
EJBTester(EJB 客户端)
jndi.属性
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命名服务的InitialContext对象。
InitialContext 对象将用于查找无状态会话 bean。
EJBTester.java
package com.tutorialspoint.test;
   
import com.tutorialspoint.stateful.LibraryBeanRemote;
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.testInterceptedEjb();
   }
   
   private void showGUI() {
      System.out.println("**********************");
      System.out.println("Welcome to Book Store");
      System.out.println("**********************");
      System.out.print("Options \n1. Add Book\n2. Exit \nEnter Choice: ");
   }
   
   private void testInterceptedEjb() {
      try {
         int choice = 1; 
         LibraryBeanRemote libraryBean =
         LibraryBeanRemote)ctx.lookup("LibraryBean/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 执行以下任务 -
从 jndi.properties 加载属性并初始化 InitialContext 对象。
在 testInterceptedEjb() 方法中,使用名称“LibraryBean/remote”进行 jndi 查找,以获取远程业务对象(无状态 EJB)。
然后,用户会看到一个图书馆商店用户界面,并要求他/她输入一个选择。
如果用户输入 1,系统会询问书籍名称并使用无状态会话 bean addBook() 方法保存书籍。会话 Bean 将书存储在其实例变量中。
如果用户输入 2,系统将使用无状态会话 bean getBooks() 方法检索书籍并退出。
运行客户端访问EJB
在项目资源管理器中找到 EJBTester.java。右键单击 EJBTester 类并选择运行文件。
在 Netbeans 控制台中验证以下输出。
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应用服务器日志输出
验证 JBoss 应用程序服务器日志输出中的以下输出。
.... 09:55:40,741 INFO [STDOUT] *** Intercepting call to LibraryBean method: addBook 09:55:43,661 INFO [STDOUT] *** Intercepting call to LibraryBean method: getBooks