Java EE 中的拦截器是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18853221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are Interceptors in Java EE?
提问by Umair
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?
我试图澄清我关于 Java EE 中拦截器的概念。我已经阅读了 Java EE 规范,但我对此并不感到困惑。请为我提供一些有用的链接或教程,它们可以清除我的概念。我们如何、何时、为什么使用拦截器?
采纳答案by Arun Gupta
Interceptors are used to implement cross-cutting concerns, such as logging, auditing, and security, from the business logic.
拦截器用于从业务逻辑中实现横切关注点,例如日志记录、审计和安全性。
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.
在 Java EE 5 中,只允许在 EJB 上使用拦截器。在 Java EE 6 中,拦截器成为了它自己的新规范,在更高级别进行了抽象,以便它可以更通用地应用于平台中更广泛的规范集。
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.
它们拦截关联目标类上的调用和生命周期事件。基本上,拦截器是一个类,它的方法在目标类上的业务方法被调用、生命周期事件(例如创建/销毁 bean 的方法)或 EJB 超时方法发生时被调用。CDI 规范定义了一种类型安全机制,用于使用拦截器绑定将拦截器与 bean 相关联。
Look for a working code sample at:
在以下位置查找工作代码示例:
https://github.com/arun-gupta/javaee7-samples/tree/master/cdi/interceptors
https://github.com/arun-gupta/javaee7-samples/tree/master/cdi/interceptors
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:
Java EE 7 还在 Java Transaction API 中引入了一个新的 @Transactional 注释。这允许您在 EJB 之外拥有容器管理的事务。该注解被定义为拦截器绑定并由 Java EE 运行时实现。@Transactional 的工作示例位于:
https://github.com/arun-gupta/javaee7-samples/tree/master/jta/transaction-scope
https://github.com/arun-gupta/javaee7-samples/tree/master/jta/transaction-scope
回答by blackpanther
I like this definition: Interceptors are components that intercept calls to EJB methods. They can be used for auditing and logging as and when EJBs are accessed.
我喜欢这个定义:拦截器是拦截对 EJB 方法调用的组件。它们可用于在访问 EJB 时进行审计和记录。
In another situation, they can be used in a situation where we need to check whether a client has the authority or clearance to execute a transaction on a particular object in the database. Well, this is where Interceptors come in handy; they can check whether the client/user has that authority by checking whether he/she can invoke that method on that database object or EJB.
在另一种情况下,它们可用于我们需要检查客户端是否有权或许可对数据库中的特定对象执行事务的情况。嗯,这就是拦截器派上用场的地方;他们可以通过检查客户端/用户是否可以在该数据库对象或 EJB 上调用该方法来检查他/她是否具有该权限。
However, I would still have a look at the following articleand the following tutorialto get an idea of how they are used in a Java EE setting/environment.
回答by Sundararaj Govindasamy
Interceptors are used to add AOP capabilityto managed beans.
拦截器用于向托管 bean添加 AOP 功能。
We can attach Interceptor to our class using @Interceptor annotation. Whenever a method in our class is called, the attached Interceptor will intercept that method invocation and execute its interceptor method.
我们可以使用 @Interceptor 注释将 Interceptor 附加到我们的类。 每当我们类中的方法被调用时,附加的拦截器将拦截该方法调用并执行其拦截器方法。
This can be achieved using @AroundInvoke annotation ( see example below ).
这可以使用 @AroundInvoke 注释来实现(见下面的例子)。
We can intercept life cycle events of a class ( object creation,destroy etc) using @AroundConstruct annotation.
我们可以使用@AroundConstruct 注释拦截类的生命周期事件(对象创建、销毁等)。
Main differencebetween Interceptor and Servlet Filtersis We can use Interceptor outside WebContext, but Filters are specific to Web applications.
Interceptor 和Servlet Filters 的主要区别是我们可以在 WebContext 之外使用 Interceptor,但 Filters 是特定于 Web 应用程序的。
Common uses of interceptors are logging, auditing, and profiling.
拦截器的常见用途是日志记录、审计和分析。
For more detailed introduction, you can read this article. https://abhirockzz.wordpress.com/2015/01/03/java-ee-interceptors/
更详细的介绍,可以阅读这篇文章。 https://abhirockzz.wordpress.com/2015/01/03/java-ee-interceptors/