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     }

 

 

你可能感兴趣的:(spring)