Implementing the details of AOP using spring annotations

Source: Internet
Author: User
Tags aop throwable

We've already started using spring annotations to implement AOP, and now we're going to learn some of the details of AOP using spring annotations. This article builds on the case of using spring annotations to get started with AOP.
This article is to explain the use of spring annotations to implement some of the details of AOP, in fact, is to learn how to use various notifications, such as pre-notification, post-notification, exception notification, final notification, surround notification, etc., before we have learned the pre-notification, now to learn the remaining notifications.
Let's take a look at the post notification, and we need to modify the code of the Myinterceptor class to read:

/** *
 section * @author li Ayun
 *
 *
/@Aspect public
class Myinterceptor {
    @Pointcut ("execution (* cn.itcast.service.impl.personserviceimpl.* (..)) ")
    private void Anymethod () {}//declares a pointcut, Anymethod is the pointcut name

    //declares that the method is a pre-notification: Executes @Before before the target method starts 
    ("Anymethod ()") Public
    void Doaccesscheck () {
        System.out.println ("Pre-Notification");

    @AfterReturning ("Anymethod ()") Public
    void Doafterreturning () {
        System.out.println ("post Notification");}

Test the Interceptortest () method of the Springaoptest class to discover the Eclipse console printing:

This means that the post-notification method is executed after the target method executes.
We'll look at the final notice, and we need to change the code of the Myinterceptor class to:

/** *
 section * @author li Ayun
 *
 *
/@Aspect public
class Myinterceptor {
    @Pointcut ("execution (* cn.itcast.service.impl.personserviceimpl.* (..)) ")
    private void Anymethod () {}//declares a pointcut, Anymethod is the pointcut name

    //declares that the method is a pre-notification: Executes @Before before the target method starts 
    ("Anymethod ()") Public
    void Doaccesscheck () {
        System.out.println ("Pre-Notification");

    @AfterReturning ("Anymethod ()") Public
    void Doafterreturning () {
        System.out.println ("post notification");

    @After ("Anymethod ()") Public
    void Doafter () {
        System.out.println ("Final Notice");
    }
}

Test the Interceptortest () method of the Springaoptest class to discover the Eclipse console printing:

Then, let's take a look at the exception notification, the exception notification is executed when the target method throws an exception, so we should change the code of the Personserviceimpl class to:

public class Personserviceimpl implements Personservice {

    @Override public
    void Save (String name) {
        throw New RuntimeException ("I am Abnormal");
        System.out.println ("I Am the Save () method");
    }

    @Override public
    void Update (String name, Integer ID) {
        System.out.println ("I Am the update () method");
    }

    @Override public
    String getpersonname (Integer id) {
        System.out.println ("I am Getpersonname () method");
        return "XXX";
    }

}

Then declare the exception notification method in the Myinterceptor class:

/** *
 section * @author li Ayun
 *
 *
/@Aspect public
class Myinterceptor {
    @Pointcut ("execution (* cn.itcast.service.impl.personserviceimpl.* (..)) ")
    private void Anymethod () {}//declares a pointcut, Anymethod is the pointcut name

    //declares that the method is a pre-notification: Executes @Before before the target method starts 
    ("Anymethod ()") Public
    void Doaccesscheck () {
        System.out.println ("Pre-Notification");

    @AfterReturning ("Anymethod ()") Public
    void Doafterreturning () {
        System.out.println ("post notification");

    @After ("Anymethod ()") Public
    void Doafter () {
        System.out.println ("Final Notice");

    @AfterThrowing ("Anymethod ()") Public
    void Doafterthrowing () {
        System.out.println ("Exception notification");
    }
}

Test the Interceptortest () method of the Springaoptest class to discover the Eclipse console printing:

And also throws an exception.
Finally, let's take a look at surround notifications, where the interceptors provided by STRUTS2 are surround notifications, and surround notifications are heavily used when we do a privilege system. At this point, we will return the code of the Personserviceimpl class to:

public class Personserviceimpl implements Personservice {

    @Override public
    void Save (String name) {
        //Throw New RuntimeException ("I am Abnormal");
        System.out.println ("I Am the Save () method");
    }

    @Override public
    void Update (String name, Integer ID) {
        System.out.println ("I Am the update () method");
    }

    @Override public
    String getpersonname (Integer id) {
        System.out.println ("I am Getpersonname () method");
        return "XXX";
    }

}

The wrapping notification method is then declared in the Myinterceptor class, and the notation for the surround notification method is fixed and shaped like this:

Public Object dobasicprofiling (Proceedingjoinpoint pjp) throws Throwable {
    ...
}

Thus, the code for the Myinterceptor class should be:

/** * section * @author Li Ayun * */@Aspect public class Myinterceptor {@Pointcut ("Execution (* cn.itcast.service.imp
    l.personserviceimpl.* (..)) ") private void Anymethod () {}//declares a pointcut, Anymethod as the pointcut name//declares that the method is a pre-notification: Executes @Before before the target method starts ("Anymethod ()") PU
    Blic void Doaccesscheck () {System.out.println ("Pre-notification");
    } @AfterReturning ("Anymethod ()") public void doafterreturning () {System.out.println ("post notification");
    } @After ("Anymethod ()") public void Doafter () {System.out.println ("final Notice");
    } @AfterThrowing ("Anymethod ()") public void doafterthrowing () {SYSTEM.OUT.PRINTLN ("exception notification");
         } @Around ("Anymethod ()") Public Object dobasicprofiling (Proceedingjoinpoint pjp) throws Throwable {/**
         * Surround notification internal It is important to ensure that the method is executed, and if the method is not executed, the intercepted method in the business bean will not be executed. * When the method is executed, if there is a tangent, it should be executed in the following order: The next slice is executed first, and if there is no tangent, * then the business method of the final target object is executed.
         If this method is not executed, then the method of the business bean will not be executed for the subsequent slice. *//if () { Determine if the user has permission, System.out.println ("access Method");
        Object result = Pjp.proceed ();
        System.out.println ("Exit Method");
    } return result; }
}

Note: It is important to ensure that the proceed () method is executed inside the surround notification, and that the intercepted method in the business bean will not be executed if the method is not executed. When the method is executed, and if there is a tangent, it should be executed in the following order: The subsequent slice is executed first, and if there is no tangent, then the business method of the final target object is executed. If this method is not executed, then the method of the business bean will not be executed for the subsequent slice .
In fact, we can only use surround notification to achieve the effect of pre-notification, post-notification, exception notification, final notification and so on.
Test the Interceptortest () method of the Springaoptest class to discover the Eclipse console printing:

In the previous section we have learned how to use various notifications, such as pre-notification, post-notification, exception notification, final notification, surround notification, and so on, and now let's look at the other details of implementing AOP using spring annotations.
Detail one: If I need to get input parameters, such as in the pre-notification, get the user input data. At this point, the preceding notification method must be modified to:

@Before ("Anymethod () && args (name)") Public
void Doaccesscheck (String name) {
    System.out.println (" Pre-notification: "+ name";
}

The @Before ("Anymethod () && args (name)") matches a method in the Personserviceimpl class that has a parameter of type string, the Save () method.
Test the Interceptortest () method of the Springaoptest class to discover the Eclipse console printing:

Detail two: If I want to get the return parameter of the Getpersonname () method in the Personserviceimpl class. At this point, the Post notification method must be modified to:

@AfterReturning (pointcut= "Anymethod ()", returning= "result") public
void doafterreturning (String result) {
    SYSTEM.OUT.PRINTLN ("Post notification:" + result);
}

The @AfterReturning (pointcut= "Anymethod ()", returning= "result") matches a method in the Personserviceimpl class that returns a value of type string. And the returning property passes the return value inside the post-notification method.
We also want to modify the code for the Springaoptest class as:

public class Springaoptest {

    @Test public
    void Interceptortest () {
        ApplicationContext cxt = new Classpathxmlapplicationcontext ("Beans.xml");
        Personservice Personservice = (personservice) cxt.getbean ("Personservice");
        Personservice.getpersonname (2);
    }

}

Test the Interceptortest () method to discover the Eclipse console printing:

Detail three: Gets the exception thrown when the target method has an exception. To facilitate experimentation, we need to modify the code of the Personserviceimpl class to:

public class Personserviceimpl implements Personservice {

    @Override public
    void Save (String name) {
        throw New RuntimeException ("I am Abnormal");
        System.out.println ("I Am the Save () method");
    }

    @Override public
    void Update (String name, Integer ID) {
        System.out.println ("I Am the update () method");
    }

    @Override public
    String getpersonname (Integer id) {
        System.out.println ("I am Getpersonname () method");
        return "XXX";
    }

}

Then modify the exception notification method to:

@AfterThrowing (pointcut= "Anymethod ()", throwing= "E") public
void doafterthrowing (Exception e) {
    SYSTEM.OUT.PRINTLN ("Exception notification:" + e);
}

Finally, let's change the code of the Springaoptest class to:

public class Springaoptest {

    @Test public
    void Interceptortest () {
        ApplicationContext cxt = new Classpathxmlapplicationcontext ("Beans.xml");
        Personservice Personservice = (personservice) cxt.getbean ("Personservice");
        Personservice.save ("xxx");
    }

}

Test the Interceptortest () method to discover the Eclipse console printing:

This is where AOP is realized based on the spring annotation approach. To view the source code, you can download the details of AOP using the spring annotation method .

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.