简体   繁体   English

Java EE 中的拦截器是什么?

[英]What are Interceptors in Java EE?

I'm trying to clear my concept about Interceptors in Java EE.我试图澄清我关于 Java EE 中拦截器的概念。 I have read Java EE specification but I'm little confused about it.我已经阅读了 Java EE 规范,但我对此并不感到困惑。 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. 拦截器用于从业务逻辑实现跨域关注,例如日志记录,审计和安全性。

In Java EE 5, Interceptors were allowed only on EJBs. 在Java EE 5中,仅允许在EJB上使用拦截器。 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 6中,Interceptor成为了自己的新规范,在更高层次上进行了抽象,因此它可以更广泛地应用于平台中更广泛的规范集。

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. 基本上,拦截器是一个类,其方法在调用目标类上的业务方法时调用,生命周期事件(如创建/销毁bean的方法)或EJB超时方法发生。 The CDI specification defines a type-safe mechanism for associating interceptors to beans using interceptor bindings. 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. Java EE 7还在Java Transaction API中引入了一个新的@Transactional注释。 This allows you to have container-managed transactions outside an EJB. 这允许您在EJB之外拥有容器管理的事务。 This annotation is defined as an interceptor binding and implemented by the Java EE runtime. 此批注被定义为拦截器绑定,并由Java EE运行时实现。 A working sample of @Transactional is at: @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

I like this definition: Interceptors are components that intercept calls to EJB methods. 我喜欢这个定义:拦截器是拦截EJB方法调用的组件。 They can be used for auditing and logging as and when EJBs are accessed. 它们可以在访问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 article and the following tutorial to get an idea of how they are used in a Java EE setting/environment. 但是,我仍然会查看以下文章和以下教程,以了解它们如何在Java EE设置/环境中使用。

Interceptors are used to add AOP capability to managed beans. 拦截器用于向托管bean 添加AOP功能

We can attach Interceptor to our class using @Interceptor annotation. 我们可以使用@Interceptor注释将Interceptor附加到我们的类。 Whenever a method in our class is called , the attached Interceptor will intercept that method invocation and execute its interceptor method. 每当调用我们类中的方法时 ,附加的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 difference between Interceptor and Servlet Filters is We can use Interceptor outside WebContext, but Filters are specific to Web applications. Interceptor和Servlet过滤器之间的主要区别是我们可以在WebContext之外使用Interceptor,但过滤器特定于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/

You can use Interceptor for the places where you want to perform some tasks before sending a request to the Controller class and before sending a request to a responce.在向 Controller 类发送请求之前和向响应发送请求之前,您可以将 Interceptor 用于要执行某些任务的地方。 Ex:- Think you want to validate the auth token before sending a request to the Controller class in this case, you can use Intercepters.例如:- 在这种情况下,您想在向 Controller 类发送请求之前验证身份验证令牌,您可以使用拦截器。 Sample code:示例代码:

@Component
public class AuthInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse 
response, Object handler) throws Exception {
    //Here you can write code to validate the auth token.
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM