21Spring re-usable pointcut expression

Source: Internet
Author: User
Tags mul

Look directly at the code:

 package com.cn.spring.aop.impl;  subtraction interface class public  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);                 
PackageCom.cn.spring.aop.impl;ImportOrg.springframework.stereotype.Component;//Implementation class@ComponentPublicClass ArithmeticcalculatorimplImplementsArithmeticcalculator {@OverridePublicint Add (int I,Intj) {int result = i +JReturnResult } @OverridePublicint Sub (int I,Intj) {int result = i- J; return result;} @Override public int mul (int I, " Span style= "color: #0000ff;" >int J) {int result = I * J ; return result;} @Override public int Div (int I, " Span style= "color: #0000ff;" >int J) {int result = I/ J ; return      
 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 {/*** Define a method that declares a pointcut expression, which, in general, does not need to be added to other code in the method * use @pointcut to declare other notifications following the pointcut expression * to refer to the current pointcut expression directly using the method name*/@Pointcut (Value= "Execution (public int arithmeticcalculator.* (..))")     Public voiddeclarejoinpointexpression () {}//declares that the method is a pre-notification: Executes before the target method starts@Before ("Declarejoinpointexpression ()")     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 ("Declarejoinpointexpression ()")     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= "Declarejoinpointexpression ()", 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 = "declarejoinpointexpression ()", 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 ("Declarejoinpointexpression ()")     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; @Order (1) @Aspect @component Public classvalidationaspect {@Before ("Loggingaspect.declarejoinpointexpression ()")//reuse tangent expression 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;PublicClassMain {PublicStaticvoidMain (string[] args) {//1. Create a spring IOC container applicationcontext CTX =new Classpathxmlapplicationcontext ("17-1.xml"); //2. Gets the instance of the bean from the IOC container huo arithmeticcalculator arithmeticcalculator = Ctx.getbean (arithmeticcalculator.  Class); //3. Use bean int result = 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-scan base-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>  

21Spring re-usable pointcut expression

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.