当前位置: 首页 > 编程日记 > 正文

Spring.Net Aop

Spring.Net 有四种通知: IMethodBeforeAdvice,IAfterReturningAdvice,IMethodInterceptor,IThrowsAdvice

BeforeAdvice
 1     using System.Reflection;
 2     using Spring.Aop;
 3     public class  BeforeAdvice : IMethodBeforeAdvice
 4     {
 5         public void Before(MethodInfo method , object[] args , object target)
 6         {
 7             Console.Out.WriteLine("[BeforeAdvice]  method : " + method.Name);
 8             Console.Out.WriteLine(" Target : " + target);
 9             Console.Out.Write("    The arguments are   : ");
10             if(args != null)
11             {
12                 foreach(object arg in args)
13                 {
14                     Console.Out.Write(arg+"\t ");
15                 }              
16             }
17             Console.Out.WriteLine();
18         }
19     }
AfterAdvice
 1     using System.Reflection;
 2     using Spring.Aop;
 3     public class  AfterAdvice : IAfterReturningAdvice
 4     {
 5         public void AfterReturning(
 6             object returnValue , MethodInfo method , object[] args , object target)
 7         {
 8             Console.Out.WriteLine("[AfterAdvice]  method : " + method.Name);
 9             Console.Out.WriteLine("    Target  : " + target);
10             Console.Out.Write("    The arguments : ");
11             if(args != null)
12             {
13                 foreach(object arg in args)
14                 {
15                     Console.Out.Write(arg+"\t ");
16                 }              
17             }
18             Console.Out.WriteLine();
19             Console.Out.WriteLine("    The return value is : " + returnValue);
20         }
21     }
AroundAdvice
 1    using AopAlliance.Intercept;
 2     public class AroundAdvice : IMethodInterceptor
 3     {
 4         public object Invoke(IMethodInvocation invocation)
 5         {   
 6             Console.WriteLine("开始:  " + invocation.TargetType.Name + "." + invocation.Method.Name);
 7             object result = invocation.Proceed();
 8             Console.WriteLine("结束:  " + invocation.TargetType.Name + "." + invocation.Method.Name);
 9             return result;
10         }
11     }
ThrowsAdvice
1   public class  ThrowsAdvice : IThrowsAdvice
2     {
3         public void AfterThrowing(Exception ex)
4         {
5             Console.Error.WriteLine(
6                 String.Format("Advised method threw this exception : {0}" , ex.Message));
7         }
8     }

数据模型(测试数据)

IDataModel
1  public interface IDataModel
2     {
3         IList FindAll();
4         object FindOne(string id);
5         void Save(object entity);
6     }
ProductModel
 1   public class ProductModel : IDataModel
 2     {
 3         public IList FindAll()
 4         {
 5             return new ArrayList();
 6         }
 7 
 8         public object FindOne(string id)
 9         {
10             //int i=100;
11             //if(i==100)
12             //  {
13             //        throw new Exception("aaa");
14             //  }         
15 
16             return new object();
17         }
18 
19         public void Save(object entity)
20         {
21             Console.WriteLine("保存:" + entity);
22         }
23     }

Spring.Net Aop 硬编码方式:

硬编码方式
 1   using Spring.Context;
 2     using Spring.Context.Support;
 3 
 4     using Spring.Aop.Framework;
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             Console.WriteLine("硬编码:");
10             //// 创建代理工厂
11             ProxyFactory factory = new ProxyFactory(new ProductModel());
12 
13             var around=new AroundAdvice();
14             var before=new BeforeAdvice();
15             var after= new AfterAdvice();
16            
17             // 这边添加的是 所有调用函数 都添加通知
18             // factory.AddAdvice(after);
19             factory.AddAdvice(around);// 后面添加的 通知被包在里面
20             //factory.AddAdvice(before);
21 
22             Spring.Aop.Support.NameMatchMethodPointcutAdvisor aspect_around=new Spring.Aop.Support.NameMatchMethodPointcutAdvisor(after);
23             aspect_around.MappedName="FindA*";
24             factory.AddAdvisor(aspect_around);
25 
26             Spring.Aop.Support.NameMatchMethodPointcutAdvisor aspect_before=new Spring.Aop.Support.NameMatchMethodPointcutAdvisor(before);
27             aspect_before.MappedName="FindO*";
28             factory.AddAdvisor(aspect_before);
29 
30             //// 创建代理对象
31             IDataModel command = (IDataModel)factory.GetProxy();
32             command.FindOne("888");
33             Console.WriteLine();
34 
35             command.FindAll();
36             Console.WriteLine();
37 
38             command.Save("内容");
39             Console.WriteLine();
40             Console.ReadKey();
41         }
42     }

Spring.Net Aop xml配置方式:(正则表达式确定方法)

app.config
 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3 
 4   <configSections>
 5     <sectionGroup name="spring">
 6       <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
 7       <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
 8     </sectionGroup>
 9   </configSections>
10 
11   <spring>
12 
13     <context>
14       <resource uri="config://spring/objects" />
15     </context>
16 
17     <objects xmlns="http://www.springframework.net" xmlns:aop="http://www.springframework.net/aop">
18       <description>配置实现AOP</description>
19     
20       <!--代理:目标(集合)+切面列表-->
21       <object id="ProxyCreator" type="Spring.Aop.Framework.AutoProxy.ObjectNameAutoProxyCreator, Spring.Aop">
22         <property name="ObjectNames">
23           <list>
24             <value>product*</value>
25           </list>
26         </property>
27         <property name="InterceptorNames">
28           <list>         
29             <value>aroundAdvisor</value>
30             <value>aroundOneAdvisor</value>
31           </list>
32         </property>
33       </object>
34 
35       <object id="aroundAdvisor" type="Spring.Aop.Support.NameMatchMethodPointcutAdvisor, Spring.Aop">
36         <property name="Advice" ref="BeforeAdvice"/>
37         <property name="MappedNames">
38           <list>
39             <value>FindA*</value>         
40           </list>
41         </property>
42       </object>
43 
44       <object id="aroundOneAdvisor" type="Spring.Aop.Support.NameMatchMethodPointcutAdvisor, Spring.Aop">
45         <property name="Advice" ref="AfterReturning"/>       
46         <property name="MappedNames">
47           <list>
48             <value>FindOne</value>
49           </list>
50         </property>
51       </object>
52 
53       
54       <object id="aroundAdvice" type="Common.AroundAdvice, Common"/>
55       <object id="BeforeAdvice" type="Common.BeforeAdvice, Common"/>
56       <object id="AfterReturning" type="Common.AfterAdvice, Common"/>
57       <object id="eAdvice" type="Common.ThrowsAdvice, Common"/>
58       
59      
60       <object id="productmodel" type="DataModel.ProductModel, DataModel"/>
61 
62     </objects>
63 
64   </spring>
65 
66 </configuration>
测试代码
 1   using Spring.Context;
 2     using Spring.Context.Support; 
 3     using Spring.Aop.Framework;
 4     using DataModel;
 5     using Common;
 6 
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             
12             IApplicationContext ctx = ContextRegistry.GetContext();
13             IDictionary speakerDictionary = ctx.GetObjectsOfType(typeof(IDataModel));
14             foreach (DictionaryEntry entry in speakerDictionary)
15             {
16                 string name = (string)entry.Key;
17                 IDataModel service = (IDataModel)entry.Value;
18                 Console.WriteLine(name + " 拦截: ");
19 
20                 service.FindAll();
21                
22                 service.FindOne("1001");
23               
24 
25                 Console.WriteLine();
26 
27                 service.Save("数据");
28 
29                 Console.WriteLine();
30             }
31 
32 
33             Console.ReadLine();
34         }
35     }

Spring.Net Aop Atrribute 方式:

Attribute
1   public class CustomAopAttribute : Attribute
2     {
3 
4     }
AttributeModel
 1   public class AttributeModel: IDataModel
 2     {
 3         [CustomAop]
 4         public IList FindAll()
 5         {
 6             return new ArrayList();
 7         }
 8         [CustomAop]
 9         public object FindOne(string id)
10         {
11             return new object();
12         }
13 
14         [CustomAop]
15         public void Find()
16         {
17             
18         }
19         public void Save(object entity)
20         {
21             Console.WriteLine("保存:" + entity);
22         }
23     }
app.config
 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3 
 4   <configSections>
 5     <sectionGroup name="spring">
 6       <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
 7       <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
 8     </sectionGroup>
 9   </configSections>
10 
11   <spring>
12 
13     <context>
14       <resource uri="config://spring/objects" />
15     </context>
16 
17     <objects xmlns="http://www.springframework.net" xmlns:aop="http://www.springframework.net/aop">
18       <description>配置实现AOP</description>
19 
20       <!--切面:切点+通知-->      
21       <object id="aroundAdvisor" type="Spring.Aop.Support.AttributeMatchMethodPointcutAdvisor, Spring.Aop">
22         <property name="Advice" ref="aroundAdvice"/>
23         <!--切点:定义或标记 要通知的 某个或某些方法-->
24         <property name="Attribute" value="ConfigAttribute.Attributes.CustomAopAttribute, ConfigAttribute" />
25       </object>
26       
27       <!--代理:目标+切面 -->
28       <object id="proxyFactoryObject" type="Spring.Aop.Framework.ProxyFactoryObject">
29         <property name="Target">
30           <object type="ConfigAttribute.DataModel.AttributeModel, ConfigAttribute" />
31         </property>
32         <property name="InterceptorNames">
33           <list>
34             <value>aroundAdvisor</value>
35           </list>
36         </property>
37       </object>
38     <!--通知:确定做的事情-->
39       <object id="aroundAdvice" type="Common.AroundAdvice, Common"/>
40 
41     </objects>
42 
43   </spring>
44 
45 </configuration>
测试代码
 1  using Spring.Context;
 2     using Spring.Context.Support;
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             IApplicationContext ctx = ContextRegistry.GetContext();
 8             IDictionary speakerDictionary = ctx.GetObjectsOfType(typeof(IDataModel));
 9             foreach (DictionaryEntry entry in speakerDictionary)
10             {
11                 string name = (string)entry.Key;
12                 IDataModel service = (IDataModel)entry.Value;
13                 Console.WriteLine(name + " 拦截: ");
14 
15                 service.FindAll();
16                 service.FindOne("122");              
17                
18              
19                 Console.WriteLine();
20 
21                 service.Save("数据");
22 
23                 Console.WriteLine();
24             }
25 
26             Console.ReadLine();
27         }
28     }

 1   using Spring.Context;
 2     using Spring.Context.Support; 
 3     using Spring.Aop.Framework;
 4     class Program
 5     {
 6         static void Main(string[] args)
 7         {
 8             
 9             IApplicationContext ctx = ContextRegistry.GetContext();
10             IDictionary speakerDictionary = ctx.GetObjectsOfType(typeof(IDataModel));
11             foreach (DictionaryEntry entry in speakerDictionary)
12             {
13                 string name = (string)entry.Key;
14                 IDataModel service = (IDataModel)entry.Value;
15                 Console.WriteLine(name + " 拦截: ");
16 
17                 service.FindAll();
18                
19                 service.FindOne("1001");
20               
21 
22                 Console.WriteLine();
23 
24                 service.Save("数据");
25 
26                 Console.WriteLine();
27             }
28 
29 
30             Console.ReadLine();
31         }
32     }

转载于:https://www.cnblogs.com/AspDotNetMVC/archive/2013/03/16/2918395.html

相关文章:

Oracle to_char函数的使用方法

本文转载于:https://blog.csdn.net/mikyz/article/details/69397030 本文转载于:https://www.cnblogs.com/aipan/p/7941917.html 本文转载于:https://blog.csdn.net/jinlong5200/article/details/3135949 转载于:https://www.cnblogs.com/demon09/p/9485627.html

python装饰器教学_Python装饰器学习(九步入门)

这是在Python学习小组上介绍的内容&#xff0c;现学现卖、多练习是好的学习方式。第一步&#xff1a;最简单的函数&#xff0c;准备附加额外功能 # -*- coding:gbk -*-示例1: 最简单的函数,表示调用了两次def myfunc():print("myfunc() called.")myfunc()myfunc()第二…

跨平台表空间传输(linux 10g表空间跨平台迁移到window 11g)

最近公司的一个项目里的linux 系统中的oracle 10g数据库&#xff0c;需要把某个表空间里的所有数据都迁移到window 2003的11g里&#xff0c;经过我与dba的交流、测试&#xff0c;决定使用跨平台的表空间传输技术&#xff0c;目前此项任务已经完成&#xff0c;经过测试&#xff…

YY的GCD 莫比乌斯反演

&#xff5e;&#xff5e;&#xff5e;题面&#xff5e;&#xff5e;&#xff5e; 题解&#xff1a; $ans \sum_{x 1}^{n}\sum_{y 1}^{m}\sum_{i 1}^{k}[gcd(x, y) p_{i}]$其中k为质数个数 $$ans \sum_{i 1}^{k}\sum_{x 1}^{n}\sum_{y 1}^{m}[gcd(x, y) p_{i}…

python答辩结束语_Beta答辩总结

前言队名&#xff1a;拖鞋旅游队项目的链接与宣传项目总结原计划实现功能预期完成程度上传照片完美实现照片信息标注在地图上对于有地理信息的照片能够较为精确的定位足迹地图可视化能够用颜色区分出到到每个省份的程度以及显示到达的地点生成旅游故事能够生成不同的故事模板&a…

在一台电脑上使用两个github账号

问题描述&#xff1a; 我公司有一个github账号&#xff0c;每天工作把代码传上去&#xff0c;我觉得代码写的好&#xff0c;我同时想上传到自己的github账号上面去&#xff0c;但是目前只有一台电脑&#xff0c;如何在一台电脑上面进行设置&#xff0c;使这一台电脑可以同时上传…

[唐胡璐]QTP框架 - 关键字驱动测试框架之七 - Settings管理

这里主要是存放一些框架相关的Global设置的相关项&#xff0c;如图所示&#xff1a;转载于:https://www.cnblogs.com/yongfeiuall/archive/2013/03/18/4134155.html

ASP.NET MVC以ValueProvider为核心的值提供系统: DictionaryValueProvider

NameValueCollectionValueProvider采用一个NameValueCollection作为数据源&#xff0c;DictionnaryValueProvider的数据源类型自然就是一个Dictionnary。NameValueCollection和Dictionnary都是一个键值对的集合&#xff0c;它们之间的不同之处在NameValueCollection运行元素具有…

串口 发送 接收 高位_电工进阶PLC大神,必备PLC串口通讯的基本知识!

戳上方蓝字“技成电工课堂”快速关注&#xff01;&#xff01;&#xff01;电力作业人员在使用PLC的时候会接触到很多的通讯协议以及通讯接口&#xff0c;最基本的PLC串口通讯和基本的通讯接口你都了解吗&#xff1f;1&#xff0c;什么是串口通讯&#xff1f;串口是计算机上一种…

HTTP请求时connectionRequestTimeout 、connectionTimeout、socketTimeout三个超时时间的含义...

1.connectionRequestTimout 指从连接池获取连接的timeout 2.connetionTimeout 指客户端和服务器建立连接的timeout&#xff0c; 就是http请求的三个阶段&#xff0c;一&#xff1a;建立连接&#xff1b;二&#xff1a;数据传送&#xff1b;三&#xff0c;断开连接。超时后会Con…

搜索引擎优化培训教程

很详细的搜索引擎优化培训教材 View more presentations from mysqlops 转载于:https://www.cnblogs.com/macleanoracle/archive/2013/03/19/2967982.html

C语言-扫雷游戏

头文件 #ifndef __MINE_H__ #define __MINE_H__#define LINE 10 #define LIST 10 #define ROWS 6 #define COWS 6int game(char UserBoard[LINE2][LIST2], char PlayerBoard[LINE][LIST]); void PrintBoard(char Playerboard[LINE][LIST]); void MineLay(char UserBoard[LINE …

PHP的命令行脚本调用

1.使用PHP命令调用php脚本接受键盘输入然后输出 1 <?php 2 fwrite(STDOUT, "Please input your name:\t"); 3 $name trim(fgets(STDIN)); 4 fwrite(STDOUT, Hello . $name); 5 ?> 2.使用PHP命令调用php脚本并接受参数 1 <?php2 if($ar…

控制输入框只能输入数字

1.将input的属性type改为number 2.这时的输入框会有小箭头&#xff0c; 去掉小箭头的方法&#xff0c;给input添加样式 input::-webkit-outer-spin-button,input::-webkit-inner-spin-button { -webkit-appearance: none;}input[type"number"] { -moz-appearan…

main函数参数,在VS中向命令行添加参数的方法

问题描述 使用main函数的参数&#xff0c;实现一个整数计算器&#xff0c;程序可以接受三个参数&#xff0c;第一个参数“-a”选项执行加法&#xff0c;“-s”选项执行减法&#xff0c;“-m”选项执行乘法&#xff0c;“-d”选项执行除法&#xff0c;后面两个参数为操作数。 例…

2.monotouch 控件的使用

1.我们打开一个xib 右下角会看到如下图所示&#xff1a; 这一部分包含了界面和各种各样的控件。选取一个控件&#xff0c;使用鼠标拖动到界面上即可使用。 2.选中一个控件&#xff0c;该控件的相关信息会在右边进行显示。做出相关设置即可。 3.设置控件属性和绑定控件事件。 首…

python视频延迟严重_【Python】改善 VideoCapture 的影像延迟

许多的范例程序大多仅介绍该如何用 VideoCapture 撷取摄影机的画面&#xff0c;却没有充分说明其隐含的问题。以下示范一个最基本的影像撷取程序。# -*- coding: utf-8 -*-import cv2# ip camera 的撷取路径URL "rtsp://admin:admin192.168.1.1/video.h264"# 建立 V…

CentOS 6.0配置pptp ××× Client和Squid透明网关

目的&#xff1a; 构建一台单网卡Linux网关&#xff08;透明代理&#xff09;&#xff0c;该网关拨入某海外服务器&#xff0c;客户端设定该网关后&#xff0c;网络出口则为海外服务器&#xff0c;实现加速访问一些网站的目的。 环境信息&#xff1a; 硬件&#xff1a;DELL机器…

mysql汉字转拼音函数

-- 创建汉字拼音对照临时表 CREATE TABLE IF NOT EXISTS t_base_pinyin (pin_yin_ varchar(255) CHARACTER SET gbk NOT NULL,code_ int(11) NOT NULL,PRIMARY KEY (code_) ) ENGINEInnoDB DEFAULT CHARSETlatin1;-- 插入数据 INSERT INTO t_base_pinyin (pin_yin_,code_) VAL…

数据挖掘的一些经典算法

数据挖掘能做以下七种不同事情&#xff08;分析方法&#xff09;&#xff1a;数据挖掘能做以下七种不同事情 分类 &#xff08;Classification&#xff09; 估计&#xff08;Estimation&#xff09; 预测&#xff08;Prediction&#xff09; 相关性分组或关联规则&#xff08;…

python cx oracle安装_python3.6的安装及cx_oracle安装

一、创建所需目录mkdir -p /home/用户名/software/python3.6.1mkdir -p /home/用户名/priv/bydmkdir -p /home/用户名/priv/byd/src/pythonmkdir -p /home/用户名/priv/byd/org二、修改byd目录的权限cd /home/用户名/priv/llchmod 777 byd/ll三、将安装包放到byd中&#xff0c;…

opencv 无法找到tbb_debug.dll

本人环境&#xff1a;vs 2010 在opencv(你的opencv install 路径)\build\common\tbb\ia32\vc10下&#xff0c;将tbb.dll 拷贝一份&#xff0c;改名为tbb_debug.dll. 并将此路径加入到系统环境变量中即可。转载于:https://blog.51cto.com/danielllf/871369

【天命奇御】成就进度62/71的通关攻略(1·开篇前言)

天命奇御于2018.8.9号在wegame上发售 先是一周目记录&#xff1a; 可以说一周目是熟悉最终boss技能后&#xff0c;靠技术过的...... 然后是二周目记录&#xff1a; 开篇前言&#xff1a; 转载于:https://www.cnblogs.com/wuduojia/p/9494700.html

使用git上传代码到github

1. github上创建项目 github是一个服务器托管商&#xff0c;我们写好的代码可以上传到github上面去 登录github的官方网站&#xff1a;http://github.com/ 注册一个自己的用户 新建一个项目&#xff0c;我这里有我自己的一个github账号&#xff0c;我直接登录上去了&am…

gpg加密命令 linux_用 PGP 保护代码完整性(五):将子密钥移到一个硬件设备中 | Linux 中国...

在这个系列教程中&#xff0c;将为你提供使用 PGP 和保护你的私钥的最佳体验。-- Konstantin Ryabitsev致谢译自 | linux.com 作者 | Konstantin Ryabitsev译者 | LCTT / qhwdw在这个系列教程中&#xff0c;将为你提供使用 PGP 和保护你的私钥的最佳体验。在本系列教程中&#…

在Android使用XML文件控制按钮文字在各种状态下的颜色

最近在项目中遇到新的需求&#xff0c;就是在按钮在选按的时候需要将文字变为白色&#xff0c;但android默认的按钮颜色为黑色&#xff0c;之前也没有考虑过类似的问题。 通过doc文档&#xff0c;发现按钮文字的处理方式和背景的处理方式很相似&#xff0c;同样可以用一份selec…

人人网 6.0 版申请页面随着滚动条拖动背景图片滚动出现的原理

第一步是考虑静态实现。整个页面分成几大块&#xff0c;比如&#xff1a; <div class"section" id"topic-a"></div> <div class"section" id"topic-b"></div> <div class"section" id"topi…

Python内部类,内部类调用外部类属性,方法

一 Python中内部类 典型定义&#xff1a; class MyOuter:age18def __init__(self,name):self.namenameclass MyInner:def __init__(self,inner_name):self.inner_nameinner_nameoutMyOuter(lqz) innerout.MyInner(lqz_inner) print(inner.inner_name) 二 内部类调用外部类的类属…

POJ 2528 Mayor's posters(线段树)

题目大意 贴海报。每张海报的高度都是一样的&#xff0c;唯独宽度不一样。每张海报只能占用整数倍的单位线段长度&#xff0c;贴了 n(n<10000) 张海报之后&#xff0c;有几张能够看见&#xff08;有一个角能看见这张海报也算被看见了&#xff09;&#xff1f;海报的宽度最大…

xmind 模板_xmind模板打包下载

500套xmind模板【分类整合】好东西分享啦&#xff01;~百度网盘下载链接&#xff1a;https://pan.baidu.com/s/1pCf8aqM8R8m4U4oWZUfOUA提取码&#xff1a;xt1j 微云盘下载连接&#xff1a; https://share.weiyun.com/5c3vehsXMind中的思维导图结构包含一个中心根主题&#xff…