• spring.net aop 讲解


    spring.net aop几个术语:

    切面:针对类

    切点:针对方法

    object.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <objects xmlns="http://www.springframework.net" xmlns:aop="http://www.springframework.net/aop">
    
        <object id="user" type="TestAop.User" ></object>
    
        <object id="newUser" type="TestAop.NewUser" ></object>
    
        <object id="advisor" type="TestAop.Advisor"></object>
        
        <aop:config>
            <aop:advisor pointcut-ref="pointcut" advice-ref="advisor"/>
        </aop:config>
    
        <object id="pointcut" type="Spring.Aop.Support.SdkRegularExpressionMethodPointcut, Spring.Aop">
            <property name="pattern" value="TestAop.User.Whoami"/>
        </object>
    </objects>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace TestAop
    {
        public class Advisor : AopAlliance.Intercept.IMethodInterceptor
        {
            
            public object Invoke(AopAlliance.Intercept.IMethodInvocation invocation)
            {
                NewUser newUser = new NewUser();
                newUser.BeforeWhoami();
                var result = invocation.Proceed();
                newUser.AfterWhoami();
                return result;
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace TestAop
    {
        public class NewUser
        {
            public void BeforeWhoami()
            {
                Console.WriteLine("I am before");
            }
    
            public void AfterWhoami()
            {
                Console.WriteLine("I am after");
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace TestAop
    {
    
        public interface  IUser
        {
            void Whoami();
        }
        public class User:IUser
        {
            public void Whoami()
            {
                Console.WriteLine("I am zhangwei");
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace TestAop
    {
        class Program
        {
            static void Main(string[] args)
            {
                Spring.Context.IApplicationContext context
                   = new Spring.Context.Support.XmlApplicationContext("objects.xml");
                IUser user = context.GetObject("user") as IUser;
                user.Whoami();
                Console.ReadKey();
            }
        }
    }

    运行:

  • 相关阅读:
    log4cxx在vs2013的静态编译
    windows下sqlite3静态库和动态库的编译
    iconv gbk字符转utf8字符
    wchar_t与char、wstring与string的相互转换
    获取当前时间并格式化
    快速获取文件大小
    cryptopp开源库的使用(二):base64加密
    cryptopp开源库的使用(零):windows下使用visual studio编译
    cryptopp开源库的使用(一):md5加密
    Docker 安装Oracle
  • 原文地址:https://www.cnblogs.com/zhangwei595806165/p/4673606.html
Copyright © 2020-2023  润新知