`

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

 

1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics