bboss aop 实践(5) 拦截器(Interceptor)

简介: bboss项目下载列表 在sourceforge访问地址为:https://sourceforge.net/project/showfiles.php?group_id=238653  bboss aop框架中,可以为业务组件配置1到多个拦截器(Interceptor)。

bboss项目下载列表 在sourceforge访问地址为:
https://sourceforge.net/project/showfiles.php?group_id=238653 

bboss aop框架中,可以为业务组件配置1到多个拦截器(Interceptor)。这些拦截器必须实现com.frameworkset.proxy.Interceptor接口。拦截器可以对执行方法的4个时间点进行拦截:

l         执行前

l         执行后

l         抛出异常时

l         方法finally

 

这些点分别对应com.frameworkset.proxy.Interceptor接口提供的4个方法:

    public void before(Method method,Object[] args) throws Throwable;

   

    public void after(Method method,Object[] args) throws Throwable;

  

    public void afterThrowing(Method method,Object[] args,Throwable throwable) throws Throwable;

   

    public void afterFinally(Method method,Object[] args) throws Throwable;

 

通过实现上述4个方法,bboss aop框架就可以方便地实施对业务组件方法的拦截功能。目前系统缺省提供了一个数据库事务管理拦截器:

com.chinacreator.spi.interceptor.TransactionInterceptor

用来实现bboss persistent框架的声明式事务管理功能,参考博客文章《bboss persistent事务管理介绍》。

 

下面举例说明拦截器的定义、配置和使用。

 

定义业务组件和拦截器

l         定义拦截器如下:

**

 * 方法拦截器

 *

 * <p>Title: Insterceptor.java</p>

 *

 * <p>Description: </p>

 *

 * <p>Copyright: Copyright (c) 2007</p>

 *

 * <p>bboss workgroup</p>

 * @Date Sep 5, 2008 4:43:47 PM

 * @author biaoping.yin

 * @version 1.0

 */

public class Insterceptor implements Interceptor {

 

    public void after(Method method, Object[] args) throws Throwable {

       System.out.println("Insterceptor.after(" + method.getName() + ", Object[] args)");

 

    }

 

    public void afterFinally(Method method, Object[] args) throws Throwable {

       System.out.println("Insterceptor.afterFinally(" + method.getName() + ", Object[] args)");

    }

 

    public void afterThrowing(Method method, Object[] args, Throwable throwable)

           throws Throwable {

       System.out.println("Insterceptor.afterThrowing(" + method.getName() + ", Object[] args, Throwable throwable)");

 

    }

 

    public void before(Method method, Object[] args) throws Throwable {

       System.out.println("Insterceptor.before(" + method.getName() + ", Object[] args)");

    }

}

 

l         定义的业务组件接口如下:

package com.chinacreator.spi.interceptor;

 

import com.chinacreator.spi.constructor.ConstructorInf;

 

public interface AI {

    public void testInterceptorsBeforeafterWithTX() throws Exception;

    public void testInterceptorsBeforeAfter() throws Exception;

    public void testInterceptorsBeforeThrowing() throws Exception;

    public void testInterceptorsBeforeThrowingWithTX() throws Exception;

    public void setConst(ConstructorInf inf)

    ;

 

}

 

l         业务组件实现如下:

 

public class A implements AI{

 

    public void testInterceptorsBeforeAfter() throws Exception {

//     System.out.println("testInterceptorsBeforeAfter()");

    }

 

    public void testInterceptorsBeforeThrowing() throws Exception {

//     System.out.println("testInterceptorsBeforeThrowing()");

       throw new Exception("testInterceptorsBeforeThrowing");

      

    }

 

    public void testInterceptorsBeforeThrowingWithTX() throws Exception {

      

//     System.out.println("testInterceptorsBeforeThrowingWithTX()");

       throw new Exception("testInterceptorsBeforeThrowingWithTX");

    }

 

    public void testInterceptorsBeforeafterWithTX() throws Exception {

//     System.out.println("testInterceptorsBeforeafterWithTX()");

    }

 

    public void setConst(ConstructorInf inf) {

       // TODO Auto-generated method stub

      

    }

}

 

配置业务组件和拦截器:

在包com.chinacreator.spi.interceptor下建立文件

simplemanager-interceptor.xml

 

目录
相关文章
|
3月前
|
Java Spring
Spring 源码阅读 72:基于 CGLIB 的 AOP 代理的原理(2)- 拦截器的查找与执行
【1月更文挑战第7天】本文分析了基于 CGLIB 的 AOP 代理如何查找和执行拦截器链,其主要的逻辑在 DynamicAdvisedInterceptor 的intercept方法执行。
35 1
|
3月前
|
缓存 Java Spring
Spring 源码阅读 66:基于 JDK 的 AOP 代理如何获取拦截器链(4)- 将 Advice 封装为拦截器
【1月更文挑战第1天】本文分析了 Advice 被封装成 MethodInterceptor 的过程,Spring AOP 用到的五种 Advice 中,有些本身就是 MethodInterceptor 的实现类,而有些需要通过适配器的封装。
44 0
|
3月前
|
XML Java 数据格式
Spring 源码阅读 70:基于 JDK 的 AOP 代理拦截器链执行(4)- 容易被忽略的 ExposeInvocationInterceptor
【1月更文挑战第5天】本文分析了 Spring AOP 拦截器链中的一个特殊拦截器 ExposeInvocationInterceptor 的注册的时机以及它的作用。至此,基于 JDK 的 AOP 代理拦截器链执行的逻辑就分析完了。
389 0
|
3月前
|
Java 索引 Spring
Spring 源码阅读 69:基于 JDK 的 AOP 代理拦截器链执行(3)- MethodInterceptor 分析
【1月更文挑战第4天】本文详细分析了 Spring AOP 中五种增强类型对应的拦截器中增强方法的执行逻辑,结合上一篇中分析的 ReflectiveMethodInvocation 中proceed方法的执行逻辑,就组成了完整的拦截器链递归调用的逻辑。
34 0
|
3月前
|
Java 索引 Spring
Spring 源码阅读 68:基于 JDK 的 AOP 代理拦截器链执行(2)- ReflectiveMethodInvocation 分析
【1月更文挑战第3天】本文分析了 ReflectiveMethodInvocation 类中的proceed方法,通过对这个方法的分析,了解了连接器链中的增强逻辑是如何逐层执行的,以及目标方法是什么时候被调用的。
32 0
|
3月前
|
Java Spring
Spring 源码阅读 67:基于 JDK 的 AOP 代理拦截器链执行(1)- 执行前的准备工作
【1月更文挑战第2天】本文总结了 JdkDynamicAopProxy 的invoke方法在获取到拦截器链之后,是如何开始执行增强逻辑的。对于拦截器链为空的情况,会直接调用目标方法,而存在拦截器的情况下,会将拦截器链和目标方法调用的信息封装成一个 MethodInterceptor 对象,执行其proceed方法,来完成增强逻辑和目标方法的执行。
21 0
|
4月前
|
Java Spring
spring boot aop 实践---记录日志
spring boot aop 实践---记录日志
28 0
|
4月前
|
安全 前端开发 Java
Java反射详解,学以致用,实战案例(AOP修改参数、Mybatis拦截器实现自动填充)3
Java反射详解,学以致用,实战案例(AOP修改参数、Mybatis拦截器实现自动填充)
81 0
|
4月前
|
Java 数据库连接 API
Java反射详解,学以致用,实战案例(AOP修改参数、Mybatis拦截器实现自动填充)2
Java反射详解,学以致用,实战案例(AOP修改参数、Mybatis拦截器实现自动填充)
44 0
|
4月前
|
存储 Java 数据库连接
Java反射详解,学以致用,实战案例(AOP修改参数、Mybatis拦截器实现自动填充)1
Java反射详解,学以致用,实战案例(AOP修改参数、Mybatis拦截器实现自动填充)
53 0