one, intercept surround notice (around advice):The most basic notification type in Spring.net is the Intercept surround notification (interception around advice), which is the method interceptor. The Intercept surround notification inherits the Imethodinterceptor interface. Note the invocation of the Imethodinvocation.proceed () method. This method invokes the other interceptors on the interceptor chain in turn. Most interceptors need to call this method and return its return value. Of course, you can also not call the proceed method and return a different value or throw an exception, but this is generally not the way to do it.
second, the pre-notice (before advise):is the notification before the Imethodinterceptor.proceed () method call. Inherits from the Imethodbeforeadvice interface.
third, exception notification (throws advise):is the notification that an exception occurred when the Imethodinterceptor.proceed () method was called. Inherits from the Ithrowsadvice interface. The Ithrowsadvice interface does not define any method: It is an Identity interface (by: The identity interface is used for two reasons: 1, in the notification method, only the last parameter is required.) If the method is declared as an interface, the parameter list is fixed. 2, if the first reason can be solved with overloaded interface method, then this reason is to use the identity interface is sufficient reason: the class that implements this interface must declare one or more notification methods, the interface method does not do this, to indicate that the class that implements it declares one or more strongly typed exception notification methods.
IV. Post Notification (after returning advise):is the notification after the Imethodinterceptor.proceed () method call. Inherits from the Iafterreturningadvice interface. The post notification has no effect on the execution of the pointcut, and if the notification throws an exception, it is thrown up along the interceptor chain, interrupting the continuation of the Interceptor chain.
namespacespring.net{classProgram {Static voidMain (string[] args) {Proxyfactory Factory=NewProxyfactory (NewOrderService ()); Factory. Addadvice (Newaroundadvise ()); Factory. Addadvice (Newbeforeadvise ()); Factory. Addadvice (Newafterreturningadvise ()); Factory. Addadvice (Newthrowsadvise ()); IOrderService Service=(IOrderService) factory. GetProxy (); Objectresult = Service. Save (1); Console.WriteLine (); Console.WriteLine (string. Format ("Client return value: {0}", result)); Console.ReadLine (); } } Public InterfaceIOrderService {ObjectSave (ObjectID); } Public classOrderservice:iorderservice {/// <summary> ///Intercept This method/// </summary> /// <param name= "id" ></param> /// <returns></returns> Public ObjectSave (ObjectID) {Throw NewException ("error saving due to xxx reason"); return "Save:"+ID. ToString (); } } /// <summary> ///Surround Notifications/// </summary> Public classAroundadvise:imethodinterceptor { Public ObjectInvoke (imethodinvocation invocation) {Console.Out.WriteLine (string. Format ("Surround Notification: Called method ' {0} '", invocation. Method.name)); Console.WriteLine (); ObjectReturnValue =NULL; Try{returnvalue=invocation. Proceed (); } Catch{Console.Out.WriteLine ("Surround Notification: An exception occurred"); Console.WriteLine (); } Console.Out.WriteLine (String.Format ("Surround Notification: Return value ' {0} '", returnvalue)); returnreturnvalue; } } /// <summary> ///front-facing notifications/// </summary> Public classBeforeadvise:imethodbeforeadvice { Public voidBefore (MethodInfo method,Object[] args,Objecttarget) {Console.Out.WriteLine ("pre-Notification: Called Method name:"+method. Name); Console.Out.WriteLine ("Pre-notification: target:"+target); Console.Out.WriteLine ("pre-notification: parameters are:"); if(Args! =NULL) { foreach(ObjectArginchargs) {Console.Out.WriteLine ("\ t:"+Arg); }} Console.WriteLine (); } } /// <summary> ///Exception Notification/// </summary> Public classThrowsadvise:ithrowsadvice { Public voidafterthrowing (Exception ex) {stringErrorMsg =string. Format ("Exception Notification: Exception thrown by method: {0}", ex. Message); Console.Error.WriteLine (ERRORMSG); Console.WriteLine (); } } /// <summary> ///Post Notification/// </summary> Public classAfterreturningadvise:iafterreturningadvice { Public voidAfterreturning (ObjectReturnValue, MethodInfo method,Object[] args,Objecttarget) {Console.Out.WriteLine ("Post Notification: Method invocation succeeded, method name:"+method. Name); Console.Out.WriteLine ("Post notification: The target is:"+target); Console.Out.WriteLine ("Post Notification: parameters:"); if(Args! =NULL) { foreach(ObjectArginchargs) {Console.Out.WriteLine ("\ t:"+Arg); }} Console.Out.WriteLine ("Post notification: The return value is:"+returnvalue); Console.WriteLine (); } }}
Spring.net's AOP notification