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();
}
拦截器方法可以应用或绑定三个层面
Default-在部署中每个 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 | 用包com.tutorialspoint.interceptor下一个名字EjbComponent在EJB作为解释的创建项目-创建应用程序一章。您也可以使用EJB创建的项目-创建应用程序章这样本章了解拦截EJB概念。 |
| 2 | 包下com.tutorialspoint.interceptor创建LibraryBean.java和LibraryBeanRemote作为EJB解释-创建应用程序一章。保持不变的文件其余部分。 |
| 3 | 清理并生成应用程序,确保业务逻辑正在按要求。 |
| 4 | 最后,部署JBoss应用服务器上的jar文件的形式应用。如果尚未启动JBoss应用服务器将自动被启动。 |
| 五 | 现在创建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
1. Add Book
2. Exit
Enter 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()方法,JNDI查找与名行 - “LibraryBean中/远程”,以获得远程业务对象(无状态EJB)。
然后用户显示库存储用户界面和他(她)被要求输入选择/。
如果用户输入1,系统要求书名并保存使用无状态会话bean addBook()方法的书。会话bean存储本书的实例变量。
如果用户输入2,系统检索使用无状态会话bean getBooks()方法,并退出书籍。
运行客户端访问EJB
在项目资源管理器中找到EJBTester.java。右键单击EJBTester类并选择运行文件(run file) 。
验证以下在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

免费 AI IDE


更多建议: