Spring Beginner's XML implements AOP pre-notification, post-notification, return notification, exception notification, and more

Source: Internet
Author: User
Tags mul

Implements the subtraction of two integers, printing the log before and after each method execution.

Arithmeticcalculator.java:

 Package Spring.aop.impl.xml;  Public Interface arithmeticcalculator {        int Add ( int i,int  j);     int Sub (int i,int  j);     int mul (int i,int  j);     int div ( int i,int  j);    }

Arithmeticcalculatorimpl.java:

 PackageSpring.aop.impl.xml; Public classArithmeticcalculatorimplImplementsArithmeticcalculator { Public intAddintIintj) {intresult = i+J; returnresult; }     Public intSubintIintj) {intresult = IJ; returnresult; }     Public intMulintIintj) {intresult = i*J; returnresult; }     Public intDivintIintj) {intresult = i/J; returnresult; }}

Loggingaspect.java:

 PackageSpring.aop.impl.xml;Importjava.util.Arrays;Importjava.util.List;ImportOrg.aspectj.lang.JoinPoint;ImportOrg.aspectj.lang.ProceedingJoinPoint;ImportOrg.aspectj.lang.annotation.After;Importorg.aspectj.lang.annotation.AfterReturning;Importorg.aspectj.lang.annotation.AfterThrowing;ImportOrg.aspectj.lang.annotation.Around;ImportOrg.aspectj.lang.annotation.Aspect;ImportOrg.aspectj.lang.annotation.Before;ImportOrg.aspectj.lang.annotation.Pointcut;ImportOrg.springframework.core.annotation.Order;Importorg.springframework.stereotype.Component; Public classLoggingaspect { Public voidBeforemethod (Joinpoint joinpoint) {String methodName=joinpoint.getsignature (). GetName (); List<Object> args=arrays.aslist (Joinpoint.getargs ()); System.out.println ("The method" +methodname+ "begins" +args); }         Public voidAftermethod (Joinpoint joinpoint) {String methodName=joinpoint.getsignature (). GetName (); List<Object> args=arrays.aslist (Joinpoint.getargs ()); System.out.println ("The method" +methodname+ "ends"); }         Public voidAfterreturnmethod (joinpoint joinpoint,object result) {String methodName=joinpoint.getsignature (). GetName (); System.out.println ("The method" +methodname+ "ends with afterreturning" +result); }         Public  voidafterthrowing (Joinpoint joinpoint,exception ex) {String methodName=joinpoint.getsignature (). GetName (); System.out.println ("The method" +methodname+ "occurs exection:" +ex); }         PublicObject Aroundmethod (Proceedingjoinpoint joinpoint) {object result=NULL; String MethodName=joinpoint.getsignature (). GetName (); Try {            //front-facing notificationsSYSTEM.OUT.PRINTLN ("---->the method" +methodname+ "begins with" +arrays.aslist (Joinpoint.getargs ())); //Execute target Methodresult=joinpoint.proceed (); //Back to NotificationsSYSTEM.OUT.PRINTLN ("---->" +result); } Catch(Throwable e) {e.printstacktrace (); //Exception NotificationSYSTEM.OUT.PRINTLN ("---->" +e); }                //Post NotificationSYSTEM.OUT.PRINTLN ("---->the method" +methodname+ "ends"); returnresult; }}

Applicationcontext.xml:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-4.3.xsd ">    <!--Configuration Bean -    <BeanID= "Arithmeticcalculator"class= "Spring.aop.impl.xml.ArithmeticCalculatorImpl">    </Bean>        <!--Bean that configures facets -    <BeanID= "Loggingaspect"class= "Spring.aop.impl.xml.LoggingAspect">    </Bean>        <!--Configuring AOP -    <Aop:config>        <!--To Configure a pointcut expression -        <Aop:pointcutexpression= "Execution (* spring.aop.impl.xml.arithmeticcalculator.* (..))"ID= "Pointcut"/>                <!--Configure facets and notifications -        <Aop:aspectref= "Loggingaspect"Order= "1">             <Aop:beforeMethod= "Beforemethod"Pointcut-ref= "Pointcut"/>             <Aop:afterMethod= "Aftermethod"Pointcut-ref= "Pointcut"/>             <aop:after-throwingMethod= "Afterthrowing"Pointcut-ref= "Pointcut"throwing= "Ex"/>        </Aop:aspect>    </Aop:config>    </Beans>

Test:

 Packagespring.aop.impl.xml.test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportSpring.aop.impl.xml.ArithmeticCalculator; Public classMain { Public Static voidMain (string[] args) {ApplicationContext ctx=NewClasspathxmlapplicationcontext ("Applicationcontext-xml.xml"); Arithmeticcalculator Arithmeticcalculator= (arithmeticcalculator) ctx.getbean ("Arithmeticcalculator"); intResult=arithmeticcalculator.add (10, 20); System.out.println ("Result:" +result); Result=arithmeticcalculator.div (10, 0); System.out.println ("Result:" +result); }}

Output:

The method add begins [10, 20]
The method Add ends
Result:30
The method div begins [10, 0]
The method Div ends
The method div occurs exection:java.lang.ArithmeticException:/by zero
Exception in thread "main" java.lang.ArithmeticException:/By zero
At Spring.aop.impl.xml.ArithmeticCalculatorImpl.div (arithmeticcalculatorimpl.java:22)
At Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
At Sun.reflect.NativeMethodAccessorImpl.invoke (nativemethodaccessorimpl.java:62)
At Sun.reflect.DelegatingMethodAccessorImpl.invoke (delegatingmethodaccessorimpl.java:43)
At Java.lang.reflect.Method.invoke (method.java:497)
At Org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection (aoputils.java:333)
At Org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint (Reflectivemethodinvocation.java : 190)
At Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (reflectivemethodinvocation.java:157)
At Org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke ( METHODBEFOREADVICEINTERCEPTOR.JAVA:52)
At Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (reflectivemethodinvocation.java:179)
At Org.springframework.aop.aspectj.AspectJAfterAdvice.invoke (aspectjafteradvice.java:47)
At Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (reflectivemethodinvocation.java:179)
At Org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke (aspectjafterthrowingadvice.java:62)
At Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (reflectivemethodinvocation.java:179)
At Org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke (exposeinvocationinterceptor.java:92)
At Org.springframework.aop.framework.ReflectiveMethodInvocation.proceed (reflectivemethodinvocation.java:179)
At Org.springframework.aop.framework.JdkDynamicAopProxy.invoke (jdkdynamicaopproxy.java:213)
At Com.sun.proxy. $Proxy 2.div (Unknown Source)
At Spring.aop.impl.xml.test.Main.main (main.java:19)

Spring Beginner's XML implements AOP pre-notification, post-notification, return notification, exception notification, and more

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.