bboss aop 实践(5) 拦截器(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,文件内容如下:

<?xml version="1.0" encoding='gb2312'?>

<manager-config>

    <manager id="interceptor.a"

       singlable="true">

       <provider type="A" class="com.chinacreator.spi.interceptor.A" />

       <interceptor class="com.chinacreator.spi.interceptor.Insterceptor"/>

       <!—-其它拦截器

<interceptor class="com.chinacreator.spi.interceptor.Insterceptor1"/>-->    

    </manager>

</manager-config>

 

simplemanager-interceptor.xml文件配置在主文件manager-provider.xml文件中:

<managerimport file="com/chinacreator/spi/interceptor/manager-interceptor.xml" />

 

这样我们就配置完毕了。

 

使用业务组件,拦截器作用于业务方法

 

package com.chinacreator.spi.interceptor;

 

import com.chinacreator.spi.BaseSPIManager;

import com.chinacreator.spi.SPIException;

 

public class TestInterceptor {

    public static void testInterceptor()

    {

       try {

           AI a = (AI)BaseSPIManager.getProvider("interceptor.a");

           try {

              a.testInterceptorsBeforeAfter();

           } catch (Exception e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

           }

           try {

              a.testInterceptorsBeforeThrowing();

           } catch (Exception e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

           }

       } catch (SPIException e) {

           // TODO Auto-generated catch block

           e.printStackTrace();

       }

    }

   

    public static void main(String[] args)

    {

       testInterceptor();

    }

 

}

 

综上所述,bboss aop框架提供了使用非常简单但功能强大的拦截器组件,不妨一试。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值