zoukankan      html  css  js  c++  java
  • net 5 aop

      接口处 声明一个切入逻辑

     标记接口 所有的方法都要aop 了

       [Intercept(typeof(CustomAutofactAOP))]
        public interface Itest
        {
            public void hello();
         
        }
     public class CustomAutofactAOP : IInterceptor
        {
            public void Intercept(IInvocation invocation)
            {
                {
                    Console.WriteLine("执行前。。。。");
                }
                invocation.Proceed();// 
                {
                    Console.WriteLine("执行后。。。。");
                }
            }
        }

    针对接口注册

           public static void  RegsterType(this ContainerBuilder builder)
            {
    
                builder.RegisterType<CustomAutofactAOP>();
                builder.RegisterType<test>().As<Itest>().EnableInterfaceInterceptors();
                builder.RegisterType<CustomAuthorizationHandler>().As<IAuthorizationHandler>();
                builder.RegisterType<World>().As<IWorld>().PropertiesAutowired(new CustomPropertySelector());
            }


    调用hello 方法
    
    
    
    运行结果 


    
    

     还有通过虚方法aop  这样细节到具体方法 而不是所有接口类都aop

    
    
    
               1 builder.RegisterType<test>().As<Itest>().EnableInterfaceInterceptors(); 换成 builder.RegisterType<test>().As<Itest>().Enablec1assInterceptors();
    2 在具体实现的抽象类上标记拦截器   [Intercept(typeof(CustomAutofactAOP))] 放在整个实现类上 需要aop的方法 标记虚方法 virtual 即可完成aop


  • 相关阅读:
    ConcurrentHashMap源码阅读
    java中Scanner类nextLine()和next()的区别和使用方法
    setuid
    lsattr
    设置umask
    touch
    od
    vi 搜索
    nl
    cat 参数
  • 原文地址:https://www.cnblogs.com/jasontarry/p/15330703.html
Copyright © 2011-2022 走看看