Spring AOP -- Return notification, exception notification, and surround notification, springaop --

Source: Internet
Author: User

Spring AOP -- Return notification, exception notification, and surround notification, springaop --

In the previous article, I learned Spring AOP and pre-notifications and post-notifications. Address: http://www.cnblogs.com/dreamfree/p/4095858.html

In this article, we will continue to learn about the previous article and continue to learn about return notifications, exception notifications, and surround notifications. For more information, see code comments.

1 package com. yl. spring. aop; 2 3 import java. util. arrays; 4 5 import org. aspectj. lang. joinPoint; 6 import org. aspectj. lang. proceedingJoinPoint; 7 import org. aspectj. lang. annotation. after; 8 import org. aspectj. lang. annotation. afterReturning; 9 import org. aspectj. lang. annotation. afterThrowing; 10 import org. aspectj. lang. annotation. around; 11 import org. aspectj. lang. annotation. aspect; 12 import org. aspec Tj. lang. annotation. before; 13 import org. springframework. stereotype. component; 14 15 @ Component16 @ Aspect17 public class LoggingAspect {18 19/** 20 * on com. yl. spring. aop. run code 21 */22 @ Before ("execution (public int com. yl. spring. aop. arithmeticCalculator. *(..)) ") 23 public void beforeMethod (JoinPoint joinPoint) {24 String methodName = joinPoint. getSignature (). getNa Me (); 25 Object [] args = joinPoint. getArgs (); 26 System. out. println ("The method" + methodName + "begins with" + Arrays. asList (args); 27} 28 29/** 30 * in com. yl. spring. aop. after each method of each implementation class of the ArithmeticCalculator interface is executed, code 31 * No matter whether the method has an exception 32 */33 @ After ("execution (public int com. yl. spring. aop. arithmeticCalculator. *(..)) ") 34 public void afterMethod (JoinPoint joinPoint) {35 String methodName = joinPoint. ge TSignature (). getName (); 36 Object [] args = joinPoint. getArgs (); 37 System. out. println ("The method" + methodName + "ends with" + Arrays. asList (args )); 38} 39 40/** 41 * Code executed after the method ends normally 42 * The returned notification is 43 */44 @ AfterReturning (value = "execution (public int com. yl. spring. aop. arithmeticCalculator. *(..)) ", returning =" result ") 45 public void afterReturning (JoinPoint joinPoint, Object result) {46 String me ThodName = joinPoint. getSignature (). getName (); 47 System. out. println ("The method" + methodName + "return with" + result ); 48} 49 50/** 51 * Code executed when a method exception occurs 52 * The exception object can be accessed, you can specify 53 */54 @ AfterThrowing (value = "execution (public int com. yl. spring. aop. arithmeticCalculator. *(..)) ", throwing =" ex ") 55 public void afterThrowing (JoinPoint joinPoint, Exception ex) {56 String methodName = joinPoint. GetSignature (). getName (); 57 System. out. println ("The method" + methodName + "occurs exception:" + ex ); 58} 59 60/** 61 * the surround notification must carry the ProceedingJoinPoint parameter 62 *. The surround notification is similar to the full process of the dynamic Proxy: the parameter of the ProceedingJoinPoint type can determine whether to execute the target method. 63 * and the surround notification must have a return value. The returned value is the return value of the target Method 64 */65 @ Around ("execution (public int com. yl. spring. aop. arithmeticCalculator. *(..)) ") 66 public Object aroundMethod (ProceedingJoinPoint pjd) {67 Object result = null; 68 String methodName = pjd. getSignature (). getName (); 69 // Method for executing the target 70 try {71 // 72 System for pre-notification. out. println ("The method" + methodName + "begins with" + Arrays. asList (pjd. getArgs (); 73 result = pjd. proceed (); 74 // return notification 75 System. out. println ("The method" + methodName + "ends with" + Arrays. asList (pjd. getArgs (); 76} catch (Throwable e) {77 // exception notification 78 System. out. println ("The method" + methodName + "occurs expection:" + e); 79 throw new RuntimeException (e); 80} 81 // post notification 82 System. out. println ("The method" + methodName + "ends"); 83 return result; 84} 85 86}

 

Slice priority

Add a new face Cutting class for the project to verify the function. You need to specify the order of face cutting. That is, the plane priority. The specific method is to add the @ Order annotation to the partition class and specify a specific number. The smaller the value, the higher the priority.

1 package com. yl. spring. aop; 2 3 import java. util. arrays; 4 5 import org. aspectj. lang. joinPoint; 6 import org. aspectj. lang. annotation. aspect; 7 import org. aspectj. lang. annotation. before; 8 import org. springframework. core. annotation. order; 9 import org. springframework. stereotype. component; 10 11/** 12 * you can use the @ Order annotation to specify the plane priority. The smaller the value, the higher the priority. 13 * @ author yul14 * 15 */16 @ Order (2) 17 @ Component18 @ Aspect19 public class ValidationAspect {20 21 @ Before ("execution (public int com. yl. spring. aop. arithmeticCalculator. *(..)) ") 22 public void vlidateArgs (JoinPoint joinPoint) {23 System. out. println ("validate:" + Arrays. asList (joinPoint. getArgs (); 24} 25}

Reuse of cut-point expressions:

In the LoggingAspect class, the cut point expression can be defined first and used.

1 package com. yl. spring. aop; 2 3 import java. util. arrays; 4 5 import org. aspectj. lang. joinPoint; 6 import org. aspectj. lang. proceedingJoinPoint; 7 import org. aspectj. lang. annotation. after; 8 import org. aspectj. lang. annotation. afterReturning; 9 import org. aspectj. lang. annotation. afterThrowing; 10 import org. aspectj. lang. annotation. around; 11 import org. aspectj. lang. annotation. aspect; 12 import org. aspec Tj. lang. annotation. before; 13 import org. aspectj. lang. annotation. pointcut; 14 import org. springframework. core. annotation. order; 15 import org. springframework. stereotype. component; 16 @ Order (1) 17 @ Component18 @ Aspect19 public class LoggingAspect {20 21/** 22 * defines a method to declare the entry point expression. Average, in this method, you do not need to add other Code 23 * use @ Pointcut to declare the 24 x of the entry point expression. Other notifications following this expression can directly use the method name to directly reference the method name 25 */26 @ Pointcut ("execution (public int com. yl. spring. aop. arithmeticCalculator. *(..)) ") 27 public void declareJoinPointExpression () {28 29} 30 31/** 32 * at com. yl. spring. aop. each method of each implementation class of the ArithmeticCalculator interface runs a code section 33 */34 @ Before ("declareJoinPointExpression ()") 35 public void beforeMethod (JoinPoint joinPoint) {36 String meth OdName = joinPoint. getSignature (). getName (); 37 Object [] args = joinPoint. getArgs (); 38 System. out. println ("The method" + methodName + "begins with" + Arrays. asList (args); 39} 40 41/** 42 * at com. yl. spring. aop. after each method of each implementation class of the ArithmeticCalculator interface is executed, execute code 43 * No matter whether the method has an exception 44 */45 @ After ("declareJoinPointExpression ()") 46 public void afterMethod (JoinPoint joinPoint) {47 String methodName = joinPoint. g EtSignature (). getName (); 48 Object [] args = joinPoint. getArgs (); 49 System. out. println ("The method" + methodName + "ends with" + Arrays. asList (args )); 50} 51 52/** 53 * Code executed after the method ends normally 54 * The returned notification is 55 */56 @ AfterReturning (value = "declareJoinPointExpression () ", returning =" result ") 57 public void afterReturning (JoinPoint joinPoint, Object result) {58 String methodName = joinPoint. getSignature (). g EtName (); 59 System. out. println ("The method" + methodName + "return with" + result ); 60} 61 62/** 63 * Code executed when a method exception occurs 64 * The exception object can be accessed, you can specify 65 */66 @ AfterThrowing (value = "declareJoinPointExpression ()", throwing = "ex") 67 public void afterThrowing (JoinPoint joinPoint, exception ex) {68 String methodName = joinPoint. getSignature (). getName (); 69 System. out. println ("The method" + methodName + "Occurs exception:" + ex); 70} 71 72/** 73 * the surround notification requires a parameter 74 * of the ProceedingJoinPoint type, which is similar to the entire process of a dynamic Proxy: the parameter of the ProceedingJoinPoint type can determine whether to execute the target method. 75 * and the surround notification must have a return value. The return value is the return value of the target method 76 */77 @ Around ("declareJoinPointExpression ()") 78 public Object aroundMethod (ProceedingJoinPoint pjd) {79 Object result = null; 80 String methodName = pjd. getSignature (). getName (); 81 // execution target Method 82 try {83 // pre-notification 84 System. out. println ("The method" + methodName + "begins with" + Arrays. asList (pjd. getArgs (); 85 result = pjd. proceed (); 86 // return notification 87 System. out. println ("The method" + methodName + "ends with" + Arrays. asList (pjd. getArgs (); 88} catch (Throwable e) {89 // Error Notification 90 System. out. println ("The method" + methodName + "occurs expection:" + e); 91 throw new RuntimeException (e); 92} 93 // post-Notification 94 System. out. println ("The method" + methodName + "ends"); 95 return result; 96} 97 98}

When you are in different classes or even different packages, you can use the package name, class name, and method name.

The Code is as follows:

1 package com. yl. spring. aop; 2 3 import java. util. arrays; 4 5 import org. aspectj. lang. joinPoint; 6 import org. aspectj. lang. annotation. aspect; 7 import org. aspectj. lang. annotation. before; 8 import org. springframework. core. annotation. order; 9 import org. springframework. stereotype. component; 10 11/** 12 * you can use the @ Order annotation to specify the plane priority. The smaller the value, the higher the priority. 13 * @ author yul14 * 15 */16 @ Order (2) 17 @ Component18 @ Aspect19 public class ValidationAspect {20 21 @ Before ("com. yl. spring. aop. loggingAspect. declareJoinPointExpression () ") 22 public void vlidateArgs (JoinPoint joinPoint) {23 System. out. println ("validate:" + Arrays. asList (joinPoint. getArgs (); 24} 25}

 

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.