c# spring aop的简单例子

刚刚完成了一个c#的spring aop简单例子,是在mac下用Xamarin Studio开发的。代码如下:

接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aoptest
{
    public interface ISay
    {
        void Say (string name);      
    }
}

实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aoptest
{
    class MySay : ISay
    {
        public void Say(string name)
        {
            Console.WriteLine("fuck off" + name);

        }
    }
}

通知 Advice

using System;
using AopAlliance.Intercept;

namespace aoptest
{
    public class MyInterceptor :IMethodInterceptor
    {
        public MyInterceptor ()
        {
        }

        public object Invoke(IMethodInvocation invocation)
        {           
            Console.Out.WriteLine("zch before invoke method");

            object result = invocation.Proceed();

            Console.Out.WriteLine("zch after invoke method");

            return result;
        }
    }
}

配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>
  <spring>
    
    <context>
      <resource uri="config://spring/objects" />
    </context>
    
    <objects xmlns='http://www.springframework.net'
         xmlns:db="http://www.springframework.net/database"
         xmlns:tx="http://www.springframework.net/tx"
         default-autowire="byName" default-lazy-init="true">

    <object id="aroundAdvice" type="aoptest.MyInterceptor" />
     <object id="isay" type="Spring.Aop.Framework.ProxyFactoryObject">
        <property name="Target">
          <object id = "isayTarget" type="aoptest.MySay" />
        </property>
        <property name="InterceptorNames">
          <list>
            <value>aroundAdvice</value>
          </list>
        </property>
      </object>

    </objects>
  </spring>
</configuration>

调用


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spring.Context.Support;
using Spring.Context;
using Spring.Aop.Framework;

namespace aoptest
{
    class Program
    {
        static void Main (string[] args)
        {
            IApplicationContext ctx = ContextRegistry.GetContext();
            ISay command = (ISay)ctx["isay"];
            command.Say ("zch");

        }
    }
}

转载于:https://www.cnblogs.com/wardensky/p/4816674.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值