Priority of 20Spring Facets

Source: Internet
Author: User
Tags mul

Specifies the priority of a slice by using the @order annotation, and the lower the value, the higher the priority level
Code:
Package Com.cn.spring.aop.impl;//interface classes for subtraction Public InterfaceArithmeticcalculator {intAddintIintj); intSubintIintj); intMulintIintj); intDivintIintj);}
 PackageCom.cn.spring.aop.impl;Importorg.springframework.stereotype.Component;//Implementation Class@Component Public classArithmeticcalculatorimplImplementsArithmeticcalculator {@Override Public intAddintIintj) {intresult = i +J; returnresult; } @Override Public intSubintIintj) {intresult = i-J; returnresult; } @Override Public intMulintIintj) {intresult = i *J; returnresult; } @Override Public intDivintIintj) {intresult = I/J; returnresult; }}
 PackageCom.cn.spring.aop.impl;ImportOrg.aspectj.lang.JoinPoint;ImportOrg.aspectj.lang.ProceedingJoinPoint;Importorg.aspectj.lang.annotation.*;ImportOrg.springframework.core.annotation.Order;Importorg.springframework.stereotype.Component;Importjava.util.Arrays;Importjava.util.List;//declare this class as a tangent: you first need to put the class in the IOC container, declared as a tangent plane//You can use @order annotations to specify the priority of a slice, the lower the value, the higher the priority@Order (2) @Aspect @component Public classLoggingaspect {//declares that the method is a pre-notification: Executes before the target method starts@Before ("Execution (public int arithmeticcalculator.* (int, int))")     Public voidBeforemethod (Joinpoint joinpoint) {String methodName=joinpoint.getsignature (). GetName (); List<Object> args =arrays.aslist (Joinpoint.getargs ()); System.out.println ("The method" + MethodName + "begins with" +args); }    //Post notification: Notification of execution after the target method executes (regardless of whether an exception occurs)//The results of the target method execution are not yet accessible in the post notification@After ("Execution (public int arithmeticcalculator.* (int, int))")     Public voidAftermethod (Joinpoint joinpoint) {String methodName=joinpoint.getsignature (). GetName (); List<Object> args =arrays.aslist (Joinpoint.getargs ()); System.out.println ("The method" + MethodName + "ends with" +args); }    /*** Code executed after the normal end of the method * Returns a notification that the return value of the method can be accessed *@paramJoinpoint*/@AfterReturning (Value= "Execution (public int arithmeticcalculator.* (int, int))", returning= "Result")     Public voidafterreturning (joinpoint joinpoint, Object result) {String methodName=joinpoint.getsignature (). GetName (); List<Object> args =arrays.aslist (Joinpoint.getargs ()); System.out.println ("The method ends WITD" +result); }    //code that executes when the target method exception occurs//You can access the exception object, and you can specify that the notification code be executed when a specific exception occurs@AfterThrowing (value = "Execution" (public int arithmeticcalculator.* (int, int)) ", throwing= "Ex")     Public voidafterreturning (Joinpoint joinpoint, Exception ex) {String methodName=joinpoint.getsignature (). GetName (); System.out.println ("The method" + MethodName + "Occures exception with:" +ex); }    /*** Surround notifications need to carry Proceedingjoinpoint type parameters * Surround notification is similar to the whole process of dynamic agents: Parameters of the Proceedingjoinpoint type can determine whether the target method is executed * and the surround notification must have a return value, returning Return value is the returned value of the target method *@paramProceedingjoinpoint*/@Around ("Execution (public int arithmeticcalculator.* (int, int))")     PublicObject Aroundmethod (Proceedingjoinpoint proceedingjoinpoint) {object result=NULL; String MethodName=proceedingjoinpoint.getsignature (). GetName (); //Execute target Method        Try {            //front-facing notificationsSystem.out.println ("the method" + MethodName + "begins with" +arrays.aslist (Proceedingjoinpoint.getargs ())); Result=proceedingjoinpoint.proceed (); //Back to NotificationsSystem.out.println ("The method ends with" +result); } Catch(Throwable throwable) {//Exception NotificationSystem.out.println ("the method" + MethodName + "Occures exception with:" +throwable); Throw Newruntimeexception (Throwable); }        //Post NotificationSystem.out.println ("the method" + MethodName + "ends"); returnresult; }}
 PackageCom.cn.spring.aop.impl;ImportOrg.aspectj.lang.JoinPoint;ImportOrg.aspectj.lang.annotation.Aspect;ImportOrg.aspectj.lang.annotation.Before;ImportOrg.springframework.core.annotation.Order;Importorg.springframework.stereotype.Component;Importjava.util.Arrays;/*** Created by JECYHW on 2015/6/21.*/@Order (1) @Aspect @component Public classvalidationaspect {@Before ("Execution (public int arithmeticcalculator.* (..))")     Public voidValidateargs (Joinpoint joinpoint) {System.out.println ("Validate:" +arrays.aslist (Joinpoint.getargs ())); }}
 PackageCom.cn.spring.aop.impl;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classMain { Public Static voidMain (string[] args) {//1. Create a spring IOC containerApplicationContext CTX =NewClasspathxmlapplicationcontext ("17-1.xml"); //2. Obtaining an instance of a bean from Huo in the IOC containerArithmeticcalculator arithmeticcalculator = Ctx.getbean (arithmeticcalculator.class); //3. Using Beans        intresult = Arithmeticcalculator.add (3, 6); System.out.println ("Result:" +result); //result = Arithmeticcalculator.div (3, 0); //System.out.println ("Result:" + result);    }}
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"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/context http://www.springframework.org/schema/context/spring-context.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop.xsd ">    <Context:component-scanBase-package= "Com.cn.spring.aop.impl">    </Context:component-scan>    <!--make ASPJECTJ annotations work: Automatically generate proxy objects for matching classes -    <Aop:aspectj-autoproxy></Aop:aspectj-autoproxy></Beans>

Priority of 20Spring Facets

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.