Priority of five notifications and facets for SPRING-AOP

Source: Internet
Author: User
Tags getmessage

SPRINGAOP's notice is divided into the following five types:

1 front-facing notification (@before)
Code executed before the connection point is executed

2 post notification (@after)
Code executed after the connection point executes, regardless of whether an exception occurs after the connection point executes, the post-notification executes, but the connection point return value cannot be accessed

3 Return notification
Return notification: The return value of the connection point can be obtained.
After the connection point is executed, if there is no exception, the return notification is executed and the notification is returned after the post-notification is executed.

4 Exception Notifications
If an exception occurs when the connection point executes, an exception notification is executed, and the corresponding exception notification can be defined according to the exception type

5 Surround Notification
is equivalent to a dynamic agent, which means that its kinetic energy includes the previous four notification functions,

Example code:

Package Com.jeremy.aop.example;import Org.aspectj.lang.joinpoint;import Org.aspectj.lang.ProceedingJoinPoint; Import Org.aspectj.lang.annotation.after;import Org.aspectj.lang.annotation.afterreturning;import Org.aspectj.lang.annotation.afterthrowing;import Org.aspectj.lang.annotation.around;import Org.aspectj.lang.annotation.aspect;import Org.aspectj.lang.annotation.before;import org.springframework.stereotype.component;/* * Steps to use AOP * 1 declares a slice, which must be in an IOC container, so you must add a @component annotation, and it is a facet, So also add @aspect Note * 2 definition notification: There are five notifications, namely, pre-notification, post-notification, return notification, exception notification, surround notification, according to business requirements to define the required notification * 3 in the notification when the notification is executed by that method of the class * 4 in the spring configuration file automatic scanning <context:component-scan> and Configuration AspectJ * Configuration AspectJ is when we call a target method, and this target method matches the method of my annotation declaration, Then the AOP framework should automatically generate a proxy object for the class that the method is in, * the corresponding method to execute the slice before my target method executes */@Aspect @componentpublic class Loggingaspect {@Before (" Execution (* com.jeremy.aop.example.arithmeticcalculator.* (..)) ") private void Beforemethod () {System.out.println ("The Method begins ..."); @After ("Execution (* Com.jeremy.aop.example.Arithmeticcalculator.* (..)) ") private void Aftermethod () {//TODO auto-generated method StubSystem.out.println ("The method after ..... ...");} @AfterReturning (value= "Execution (* com.jeremy.aop.example.arithmeticcalculator.* (..))", returning= "result") public void afterreturning (joinpoint joinpoint,object result) {System.out.println ("the ' Method after ... ')" + result);} @AfterThrowing (value= "Execution (* com.jeremy.aop.example.arithmeticcalculator.* (..))", throwing= "E") public void Afterthrowing (Joinpoint joinpoint,exception e) {System.out.println ("the Method afterthrowing ......)" + E.getmessage (). toString ());} @Around (value= "Execution (* com.jeremy.aop.example.arithmeticcalculator.* (..))") public Object Aroundtest (Proceedingjoinpoint pjp) {String methodname=pjp.getsignature (). GetName (); object Object=null ; try {///SYSTEM.OUT.PRINTLN ("The Method" +methodname+ "before ..."); Object=pjp.proceed ();//-- Returns the notification System.out.println (object);} catch (Throwable e) {//TODO Auto-generatEd catch block//Exception Notification System.out.println (E.getmessage (). toString ());} System.out.println ("The Method" +methodname+ "after ...................."); return object;}}

  

Tangent priority: That is, the section of the operation of the order

For example, when we have a system involving permissions, logs, authentication: And so on various functions we generally need to verify that there is no such permission before printing the log, but the system does not know how to identify which aspect to use

So we're going to have to make a hard rule about which section to use first (using @order annotations is both possible)
Example:


Priority of five notifications and facets for SPRING-AOP

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.