- 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) 应用。
类 - 类级别拦截器在 Bean 的每个方法中被调用。类级别拦截器可以通过注解或 xml(ejb-jar.xml) 应用。
方法 - 方法级别拦截器在 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 | 在 com.tutorialspoint.interceptor 包下创建一个名为 EjbComponent 的项目,如EJB - 创建应用程序章节所述。您也可以使用EJB - 创建应用程序章节中创建的项目,以便在本章节中理解被拦截的 EJB 概念。 |
| 2 | 在 com.tutorialspoint.interceptor 包下创建 LibraryBean.java 和 LibraryBeanRemote,如EJB - 创建应用程序章节所述。保持其他文件不变。 |
| 3 | 清理并构建应用程序,以确保业务逻辑按要求工作。 |
| 4 | 最后,将应用程序以 jar 文件的形式部署到 JBoss 应用服务器上。如果 JBoss 应用服务器尚未启动,它将自动启动。 |
| 5 | 现在创建 ejb 客户端,一个与EJB - 创建应用程序章节中创建客户端以访问 EJB主题下所述相同的基于控制台的应用程序。 |
EJBComponent(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.properties
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