CDI services--interceptors(拦截器)
1.拦截器综述
拦截器的功能是定义在Java拦截器规范。
拦截器规范定义了三种拦截点:
- 业务方法拦截,
- 生命周期回调侦听,
- 超时拦截(EJB)方法。
在容器的生命周期中进行拦截
|
1
2
3
4
|
public class DependencyInjectionInterceptor { @PostConstruct public void injectDependencies(InvocationContext ctx) { ... }} |
EJB超时时使用的拦截器
|
1
2
3
4
5
|
public class TimeoutInterceptor { @AroundTimeout public Object manageTransaction(InvocationContext ctx) throws Exception { ... }} |
在业务上,对某一个Bean的方法进行拦截
|
1
2
3
4
5
|
public class TransactionInterceptor { @AroundInvoke public Object manageTransaction(InvocationContext ctx) throws Exception { ... }} |
@AroundInvoke注释指定了要用作拦截器的方法,拦截器方法与被拦截的业务方法执行同一个java调用堆栈、同一个事务和安全上下文中。用@AroundInvoke注释指定的方法必须遵守以下格式:public Object XXX(javax.interceptor.InvocationContext ctx) throws Exception
下面是javax.interceptor.InvocationContext封装了客户端所调用业务方法的一些信息。
|
1
2
3
4
5
6
7
8
9
|
package javax.interceptor;public interface InvocationContext{ public Object getTarget(); public Method getMethod(); public Ojbect[] getParameters(); public void setParameters(Object[] newArgs); public java.util.Map<String, Ojbect> getContextData(); public Object proceed() throws Exception;} |
- getTarget() 指向被调用的bean实例
- getMethod() 指向被拦截的业务方法
- getParameters() 获取被拦截业务方法的参数
- setParameters() 设置被拦截业务方法的参数
- getContextData() 返回一个Map对象,它在整个方法调用期间都可以被访问到。位于同一个方法调用内的不同拦截器之间可以利用它来传递上下文相关的数据。
示例:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//被拦截的方法@Interceptors(HelloInterceptor.class)public class HelloChinaBean { public String SayHello(String name) { return name +"Hello World."; }}//拦截器定义public class HelloInterceptor { @AroundInvoke public Object log(InvocationContext ctx) throws Exception { try{ if (ctx.getMethod().getName().equals("SayHello")){ System.out.println("Holle World!!!" ); } return ctx.proceed(); }catch (Exception e) { throw e; } }} |
2.拦截器绑定(Interceptor bindings)
假设我们想要申明一些bean的事务。我们先要的是一个拦截器绑定类型来指定哪些bean我们要申明.
首先定义一个注解
|
1
2
3
4
|
@InterceptorBinding@Target({METHOD, TYPE})@Retention(RUNTIME)public @interface Transactional {} |
现在我们可以很容易地指定类ShoppingCart是事务性对象:
|
1
2
|
@Transactionalpublic class ShoppingCart { ... } |
或者我们可以指定一个方法的事务
|
1
2
3
|
public class ShoppingCart { @Transactional public void checkout() { ... }} |
2.拦截器实现(Implementing interceptors)
我们实际上要实现提供了这种事务管理方面的拦截器,所以我们需要做的是创建一个标准的拦截,并配上@Interceptor和@transactional注解.
|
1
2
3
4
5
6
|
@Transactional @Interceptorpublic class TransactionInterceptor { @AroundInvoke public Object manageTransaction(InvocationContext ctx) throws Exception { ... }} |
拦截器可以利用依赖注入:
|
1
2
3
4
5
6
7
8
|
@Transactional @Interceptorpublic class TransactionInterceptor { @Resource UserTransaction transaction; @AroundInvoke public Object manageTransaction(InvocationContext ctx) throws Exception { ... }} |
多个拦截器可以使用相同的拦截器绑定类型。
@Resource和@Inject的区别:

3.启用拦截器(Enabling interceptors)
默认情况下,所有拦截器被禁用.要使用拦截器.需要在bean.xml中进行配置,以启用.从CDI 1.1起拦截器可以使用@Priority注释为整个应用程序启用。
|
1
2
3
4
5
6
7
8
9
10
|
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"> <interceptors> <class>org.mycompany.myapp.TransactionInterceptor</class> </interceptors></beans> |
这样有2个好处:
- 拦截器比较重要,在XML中确保其确定性行为
- 它让我们在部署时启用或禁用拦截器类。
当然也可以配置启用多个拦截器
|
1
2
3
4
5
6
7
8
9
10
11
|
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"> <interceptors> <class>org.mycompany.myapp.SecurityInterceptor</class> <class>org.mycompany.myapp.TransactionInterceptor</class> </interceptors></beans> |
拦截器毕竟比较重要,不推荐使用@Priority启用.
在CDI中,XML配置的优先级高于@Priority.
关于@Priority可以参考下列:
public static class Interceptor.Priority
extends Object
Priorities that define the order in which interceptors are invoked. These values should be used with the Priority annotation.
Interceptors defined by platform specifications should have priority values in the range PLATFORM_BEFORE up until LIBRARY_BEFORE, or starting at PLATFORM_AFTER.
Interceptors defined by extension libraries should have priority values in the range LIBRARY_BEFORE up until APPLICATION, or LIBRARY_AFTER up until PLATFORM_AFTER.
Interceptors defined by applications should have priority values in the range APPLICATION up until LIBRARY_AFTER.
An interceptor that must be invoked before or after another defined interceptor can choose any appropriate value.
Interceptors with smaller priority values are called first. If more than one interceptor has the same priority, the relative order of these interceptor is undefined.
For example, an extension library might define an interceptor like this:
|
1
2
3
|
@Priority(Interceptor.Priority.LIBRARY_BEFORE+10) @Interceptor public class ValidationInterceptor { ... } |
4.Interceptor bindings with members(拦截器注解属性)
假设我们想要添加一些额外的信息给我们的@transactional注解:
|
1
2
3
4
5
6
7
|
@InterceptorBinding@Target({METHOD, TYPE})@Retention(RUNTIME)public @interface Transactional { boolean requiresNew() default false;} |
下面是requiresNew为true的拦截器
|
1
2
3
4
5
6
|
@Transactional(requiresNew = true) @Interceptorpublic class RequiresNewTransactionInterceptor { @AroundInvoke public Object manageTransaction(InvocationContext ctx) throws Exception { ... }} |
如下使用:
|
1
2
|
@Transactional(requiresNew = true)public class ShoppingCart { ... } |
但是如果我们只有一个拦截器,我们希望容器拦截器绑定时忽略requiresNew的值,也许这些信息只用于拦截器实现。我们可以使用@Nonbinding注释:
|
1
2
3
4
5
6
7
|
@InterceptorBinding@Target({METHOD, TYPE})@Retention(RUNTIME)public @interface Secure { @Nonbinding String[] rolesAllowed() default {};} |
5.Multiple interceptor binding annotations(多重拦截器绑定注解)
通常我们使用拦截器绑定的组合类型绑定多个拦截器bean。例如,下面的声明将用于绑定TransactionInterceptor和SecurityInterceptor这2个拦截器到ShoppingCart.
|
1
2
|
@Secure(rolesAllowed="admin") @Transactionalpublic class ShoppingCart { ... } |
然而,在非常复杂的情况下,一个拦截器本身可能指定拦截器绑定类型:
|
1
2
|
@Transactional @Secure @Interceptorpublic class TransactionalSecureInterceptor { ... } |
那么这个拦截器可以绑定到checkout() 方法,以下任何组合都可使用:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class ShoppingCart { @Transactional @Secure public void checkout() { ... }}@Securepublic class ShoppingCart { @Transactional public void checkout() { ... }}@Transactionalpublic class ShoppingCart { @Secure public void checkout() { ... }}@Transactional @Securepublic class ShoppingCart { public void checkout() { ... }} |
6. Interceptor binding type inheritance(拦截器绑定类型继承)
Java语言支持注解的一个限制就是缺乏注解继承.注解应该重用内置已有的.就如同下面这段代码表达的意思
|
1
2
|
//实际没这写法public @interface Action extends Transactional, Secure { ... } |
幸运的是,CDI围绕Java没有的这个特性开展了一些工作.
我们会标注一个拦截器绑定类型,其有其他拦截器的绑定类型,(称为元注解)
表述起来有点费劲,就如同下面代码这样.
|
1
2
3
4
5
|
@Transactional @Secure@InterceptorBinding@Target(TYPE)@Retention(RUNTIME)public @interface Action { ... } |
现在任何Bean绑定 Action这个注解 ,其实就是绑定到了@Transactional @Secure.(就是拦截器TransactionInterceptor和拦截器SecurityInterceptor). (甚至TransactionalSecureInterceptor,如果它存在.)
7.Use of @Interceptors(同时用多个拦截器)
这个注解@Interceptors是拦截器规范定义的,cdi是支持的<使用托管bean和EJB规范>.如下:
|
1
2
3
4
5
|
@Interceptors({TransactionInterceptor.class, SecurityInterceptor.class})public class ShoppingCart { public void checkout() { ... }} |
但缺点也很明显,不推荐使用.缺点如下:
- 拦截器在代码中是硬编码.
- 拦截器在部署时不好更改.
- 拦截器命令是非全局的——它是在类级别由拦截器的顺序列出.
因此还是使用上面CDI的使用方式比较好.
CDI services--interceptors(拦截器)的更多相关文章
- (vue.js)axios interceptors 拦截器中添加headers 属性
(vue.js)axios interceptors 拦截器中添加headers 属性:http://www.codes51.com/itwd/4282111.html 问题: (vue.js)axi ...
- flume1.8 Interceptors拦截器(五)
1. Flume Interceptors Flume有能力修改/删除流程中的events.这是在拦截器(interceptor)的帮助下完成的.拦截器(Interceptors)是实现org.apa ...
- springMVC之mvc:interceptors拦截器的用法
1.配置拦截器 在springMVC.xml配置文件增加: <mvc:interceptors> <!-- 日志拦截器 --> <mvc:interceptor> ...
- angular之interceptors拦截器
<!DOCTYPE html> <html ng-app="nickApp"> <head> <meta charset="UT ...
- springMVC <mvc:interceptors>拦截器的使用
首先在springMVC.xml配置如下代码 <!-- 拦截器 --> <mvc:interceptors> <bean class="com.base.Acc ...
- angular http interceptors 拦截器使用分享
拦截器 在开始创建拦截器之前,一定要了解 $q和延期承诺api 出于全局错误处理,身份验证或请求的任何同步或异步预处理或响应的后处理目的,希望能够在将请求移交给服务器之前拦截请求,并在将请求移交给服务 ...
- Interceptors - 拦截器
1.概述 Flume有能力在运行阶段修改/删除Event,这是通过拦截器(Interceptors)来实现的. 拦截器需要实现org.apache.flume.interceptor.Intercep ...
- vue interceptors(拦截器)
拦截器 顾名思义: 就是半路个您劫持, 拦截器 其实在项目和自己写demo中,总会遇到请求方面需要在请求头里面做判断或者添加一些东西, 这时候 vue 中应用中axios的 interceptors ...
- vue拦截器Vue.http.interceptors.push
刚开始学vue,github上down了一个开源项目,看源代码的时候看到了这个地方: /** * @export * @param {any} request * @param {any} next ...
- SpringMVC 文件上传&拦截器&异常处理
文件上传 Spring MVC 为文件上传提供了直接的支持,这种支持是通过即插即用的 MultipartResolver 实现的.Spring 用 Jakarta Commons FileUpload ...
随机推荐
- 一、ESP8266入门(基于LUA开发)
序 一入坑便停不下来... 还挺有意思的哈,233,,,, 资料杂,自己一个一个去找确实浪费了不少时间,而且大多还都是英文的,需要硬着头皮看. 这次实践入门,更是对英语的重要确信无疑.Github必须 ...
- SQL反模式学习笔记8 多列属性
目标:存储多值属性 反模式:创建多个列.比如一个人具有多个电话号码.座机号码.手机号码等. 1.查询:多个列的话,查询时可能不得不用IN,或者多个OR: 2.添加.删除时确保唯一性.判断是否有值:这些 ...
- 习题集1a:研究方法入门
1.课程实践编号 课程实践编号 随着对习题集“PS 1a:研究方法入门”和其他习题集的了解,你可能会发现进度栏中的习题编号并非一直是连续的. 对于存在两个习题集的课程,如果一个习题集看上去“缺失”习题 ...
- idea报错:Invalid bound statement (not found)
在配置MyBatis接口映射的Mapper.xml时,提示Invalid bound statement (not found)异常,就算是接口和xml名字相同,路径相同也无法找到,在网上找到了几种解 ...
- [数据结构] 快速排序C语言程序
//由大到小//快速排序(待排序数组,左侧起点,右侧起点) void quickSort(int *array, int l, int r) { if ( l >= r) return; int ...
- 复杂链表的复制(Hard)
问题来源:选自LeetCode 138:复制带随机指针的链表 问题描述: 题目给定信息: 该链表中每一个节点的成员变量都有两个,一个是next指针指向该节点的下一个节点,一个是random指针指向不确 ...
- vue cli搭建项目
1.首先电脑要在安装node环境下才能运行 2.全局安装webpack:npm install webpack -g 3.安装vue脚手架: npm install vue-cli -g 4.新建文件 ...
- vue_小项目_模糊搜索(列表过滤)_结果排序
html <div id="test"> <label> <input type="text" v-model="sea ...
- ACM常用STL
转载于https://blog.csdn.net/riba2534/article/details/61929000 1. stack stack<int>st;//栈st,用于存放in ...
- 六、web应用与Tomcat
软件系统体系结构 1 常见软件系统体系结构B/S.C/S 1.1 C/S l C/S结构即客户端/服务器(Client/Server),例如QQ: l 需要编写服务器端程序,以及客户端程序,例如我们安 ...