5. Several notifications of Spring AOP (XML)

Source: Internet
Author: User

1. Front, back, back, exception and surround

1  Public InterfaceCalculator {2 3     intAddintAintb);4 5     intSubintAintb);6 }7 8  Public classCalculatorimplImplementsCalculator {9 Ten @Override One      Public intAddintAintb) { A         returnA +b; -     } -  the @Override -      Public intSubintAintb) { -         Try { -             inti = 1/0; +}Catch(Exception e) { -         } +         returnAb; A     } at  -}
1  Public voidbefore (Joinpoint joinpoint) {2System.out.println ("Pre-notification ... ");3String Joinpointname =joinpoint.getsignature (). GetName ();4System.out.println ("Start execution" + Joinpointname + "Method ... Parameters: "+arrays.tostring (Joinpoint.getargs ()));5     }6 7      Public voidAfter (Joinpoint joinpoint) {8System.out.println ("Post-notification ... ");9String Joinpointname =joinpoint.getsignature (). GetName ();TenSystem.out.println (Joinpointname + "method execution complete ... "); One     } A  -      Public voidafterreturning (joinpoint joinpoint, Object result) { -SYSTEM.OUT.PRINTLN ("Return notification ... "); theString Joinpointname =joinpoint.getsignature (). GetName (); -System.out.println (Joinpointname + "method execution complete ... \ n The return value is: "+result); -     } -      +      Public voidafterthrowing (Joinpoint joinpoint, Exception e) { -SYSTEM.OUT.PRINTLN ("Exception notification ... "); +String Joinpointname =joinpoint.getsignature (). GetName (); ASystem.out.println (Joinpointname + "method throws an exception ... \ n Exception information: "+e.getmessage ()); at     } -      -      PublicObject Around (proceedingjoinpoint pjoinpoint) { -SYSTEM.OUT.PRINTLN ("Surround notifications ... "); -String Joinpointname =pjoinpoint.getsignature (). GetName (); -object[] args =Pjoinpoint.getargs (); inSystem.out.println ("Start execution" + Joinpointname + "Method ... Parameters: "+arrays.tostring (args)); -Object result =NULL; to         Try { +result =pjoinpoint.proceed (); - //result = Pjoinpoint.proceed (args); the}Catch(Throwable e) { * e.printstacktrace (); $         }Panax Notoginseng          -System.out.println (Joinpointname + "method execution complete ... "); the          +         returnresult; A}

Configuration:

1 <BeanID= "Calculator"class= "Demo." Calculatorimpl " />2 3     <BeanID= "Calculatorloggingaspect"class= "Demo." Calculatorloggingaspect " />4     5     <Aop:config>6         <Aop:pointcutID= "PointCut"expression= "Execution (* demo.*.add (..))" />7         <Aop:aspectID= "Logging"ref= "Calculatorloggingaspect">8             <!--<aop:before method= "before" pointcut-ref= "Pointcut"/> -9             <!--<aop:after method= "after" pointcut-ref= "Pointcut"/> -Ten             <!--<aop:after-returning method= "afterreturning" returning= "result" pointcut-ref= " pointcut"/> - One             <!--<aop:after-throwing method= "afterthrowing" throwing= "E" pointcut-ref= "Pointcut"/> - A             <Aop:aroundMethod= "Around"Pointcut-ref= "PointCut"/> -         </Aop:aspect> -     </Aop:config>

2. Introduction of notifications

1  Public InterfaceMincalculator {2 3     intMinintAintb);4     5 }6 7  Public classMincalculatorimplImplementsmincalculator{8 9 @OverrideTen      Public intMinintAintb) { One         returnA < b?a:b; A     } -  -}

Configuration:

1 <  types-matching= "demo.*"2                implement-interface= "Demo." Mincalculator "  default-impl=" Demo. Mincalculatorimpl "/>

3. Testing

1 Object obj = Ctx.getbean ("calculator"); 2 Calculator cal = (Calculator) obj; 3 int r = Cal.add (1, 1); 4 System.out.println (r); 5         6 mincalculator min = (mincalculator) obj; 7 r = Min.min (1, 3); 8 System.out.println (R);

5. Several notifications of Spring AOP (XML)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.