Java EE中的拦截器是什么? [英] What are Interceptors in Java EE?

查看:150
本文介绍了Java EE中的拦截器是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试清除有关Java EE中的Interceptor的概念.我已经阅读了Java EE规范,但对此并不太困惑.请提供一些有用的链接或教程,以阐明我的概念.我们如何,何时,为什么使用拦截器?

I'm trying to clear my concept about Interceptors in Java EE. I have read Java EE specification but I'm little confused about it. Please provide me some useful link or tutorial which could clear my concept. How, When, Why do we use interceptors?

推荐答案

拦截器用于从业务逻辑中实现跨领域的关注,例如日志记录,审核和安全性.

Interceptors are used to implement cross-cutting concerns, such as logging, auditing, and security, from the business logic.

在Java EE 5中,拦截器仅在EJB上被允许.在Java EE 6中,Interceptors成为了它自己的一个新规范,在更高层次上进行了抽象,以便可以更通用地应用于平台中的更广泛的规范集.

In Java EE 5, Interceptors were allowed only on EJBs. In Java EE 6, Interceptors became a new specification of its own, abstracted at a higher level so that it can be more generically applied to a broader set of specifications in the platform.

它们拦截关联目标类上的调用和生命周期事件.基本上,拦截器是一个类,当调用目标类上的业务方法,发生生命周期事件(例如创建/销毁bean的方法)或EJB超时方法时,其方法就会被调用. CDI规范定义了一种使用拦截器绑定将拦截器与bean关联的类型安全机制.

They intercept invocations and life-cycle events on an associated target class. Basically, an interceptor is a class whose methods are invoked when business methods on a target class are invoked, life-cycle events such as methods that create/destroy the bean occur, or an EJB timeout method occurs. The CDI specification defines a type-safe mechanism for associating interceptors to beans using interceptor bindings.

在以下位置找到工作代码示例:

Look for a working code sample at:

https://github.com/arun-gupta/javaee7-samples/tree /master/cdi/interceptors

Java EE 7还在Java Transaction API中引入了新的@Transactional批注.这使您可以在EJB外部进行容器管理的事务.此批注定义为拦截器绑定,并由Java EE运行时实现. @Transactional的工作示例位于:

Java EE 7 also introduced a new @Transactional annotation in Java Transaction API. This allows you to have container-managed transactions outside an EJB. This annotation is defined as an interceptor binding and implemented by the Java EE runtime. A working sample of @Transactional is at:

https://github.com/arun-gupta/javaee7-samples /tree/master/jta/transaction-scope

这篇关于Java EE中的拦截器是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆