Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 459|回复: 0

我心中的核心组件(可插拔的AOP)~第四回 异常拦截器

[复制链接]
  • TA的每日心情
    奋斗
    昨天 18:38
  • 签到天数: 753 天

    [LV.10]以坛为家III

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    706722
    发表于 2021-8-31 14:28:53 | 显示全部楼层 |阅读模式

    回到目录

    之前说过有关拦截器的文章,第二回  缓存拦截器,事实上,在那讲里说的最多是AOP和缓存组件,对于拦截的概念并没有详细的说明,这一讲,不说AOP,主要说一下拦截器,拦截器Interception,主要是在方法执行前或者执行后,动态添加一些行为,而这个行为主要包含缓存,日志,异常处理及你可以想到的所有的一切,呵呵。

    这一讲是异常拦截器,它的主要意义在于,当你的一个方法被执行时,你可以通过配置文件去管理这个方法执行前与执行后是否附加统一异常处理行为。

    拦截器组件我们还是用Unity.InterceptionExtension,它依附于Unity,当你没有安装Unity时,Unity.InterceptionExtension在安装时会自己帮你添加上,这是正确的,呵呵。

    对于我们所开发的拦截器,必须要实现IInterceptionBehavior这个接口才可以,这是接口的内容

     // 摘要:
        //     Interception behaviors implement this interface and are called for each invocation
        //     of the pipelines that they're included in.
        public interface IInterceptionBehavior
        {
            // 摘要:
            //     Returns a flag indicating if this behavior will actually do anything when
            //     invoked.
            //
            // 备注:
            //     This is used to optimize interception. If the behaviors won't actually do
            //     anything (for example, PIAB where no policies match) then the interception
            //     mechanism can be skipped completely.
            bool WillExecute { get; }
    
            // 摘要:
            //     Returns the interfaces required by the behavior for the objects it intercepts.
            //
            // 返回结果:
            //     The required interfaces.
            IEnumerable<Type> GetRequiredInterfaces();
            //
            // 摘要:
            //     Implement this method to execute your behavior processing.
            //
            // 参数:
            //   input:
            //     Inputs to the current call to the target.
            //
            //   getNext:
            //     Delegate to execute to get the next delegate in the behavior chain.
            //
            // 返回结果:
            //     Return value from the target.
            IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext);
        }
    View Code

    其中Invoke方法就是拦截方法之前要执行的方法,当进行目标方法时,首先会检测是否在此方法的信息,如果有,就进行拦截行为,本例为异常拦截行为。

    异常拦截器代码:

      /// <summary>
        /// 表示用于异常日志记录的拦截行为。
        /// </summary>
        public class ExceptionLoggingBehavior : IInterceptionBehavior
        {
            #region IInterceptionBehavior Members
            /// <summary>
            /// 获取当前行为需要拦截的对象类型接口。
            /// </summary>
            /// <returns>所有需要拦截的对象类型接口。</returns>
            public IEnumerable<Type> GetRequiredInterfaces()
            {
                return Type.EmptyTypes;
            }
    
            /// <summary>
            /// 通过实现此方法来拦截调用并执行所需的拦截行为。
            /// </summary>
            /// <param name="input">调用拦截目标时的输入信息。</param>
            /// <param name="getNext">通过行为链来获取下一个拦截行为的委托。</param>
            /// <returns>从拦截目标获得的返回信息。</returns>
            public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
            {
                var methodReturn = getNext().Invoke(input, getNext);
                if (methodReturn.Exception != null)
                {
                    Utils.Log(methodReturn.Exception);
                }
                return methodReturn;
            }
            /// <summary>
            /// 获取一个<see cref="Boolean"/>值,该值表示当前拦截行为被调用时,是否真的需要执行
            /// 某些操作。
            /// </summary>
            public bool WillExecute
            {
                get { return true; }
            }
    
            #endregion
        }

    而要想使拦截器起作用,我们可以在配置文件中进行配置,下面是缓存拦截与异常拦截的配置代码,它们隶属于Unity节点

      <!--BEGIN: Unity-->
      <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
        <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
        <container>
          <extension type="Interception" />
          <register type="Infrastructure.Caching.ICacheProvider,  DDD_AOP_WCF.Infrastructure" mapTo="Infrastructure.Caching.EntLibCacheProvider,  DDD_AOP_WCF.Infrastructure" />
          <!--对数据上下文的注入-->
          <register type="DDD_AOP_WCF.Domain.Repository.IProductRepository, DDD_AOP_WCF.Domain" mapTo="DDD_AOP_WCF.Infrastructure.Repository.ProductRepository, DDD_AOP_WCF.Infrastructure" />
          <!--对WCF的访问进行的注入与缓存和异常的拦截-->
          <register type="DDD_AOP_WCF.ServiceContracts.IProductService, DDD_AOP_WCF.ServiceContracts" mapTo="DDD_AOP_WCF.Service.Implements.ProductServiceImpl, DDD_AOP_WCF.Service">
            <!--  <interceptor type="VirtualMethodInterceptor" />-->
            <interceptor type="InterfaceInterceptor" />
            <interceptionBehavior type="Infrastructure.InterceptionBehaviors.CachingBehavior, DDD_AOP_WCF.Infrastructure" />
            <interceptionBehavior type="Infrastructure.InterceptionBehaviors.ExceptionLoggingBehavior, DDD_AOP_WCF.Infrastructure" />
          </register>
        </container>
      </unity>
      <!--END: Unity-->

    怎么样,大家是否对拦截器有一个很清楚的了解和认识了呢,呵呵!

    回到目录

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-6-24 19:42 , Processed in 0.055451 second(s), 27 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表