AOP__ programming for cutting-plane programming in layman's

Source: Internet
Author: User
Tags aop

One of the important things in the spring framework is AOP, which summarizes aop--aspect-oriented programming today.

First, what is AOP, to put it simply, is that AOP is an idea (also called a pattern). It is a way of dealing with problems and is a norm.

Related Concepts in AOP:

Aspect: Facets, which is the first thing we need to find using AOP, is the basis for using AOP.

Joint point: Connection points, which can be a behavior in the execution of a program, such as a method in a class.

Pointcut: A pointcut used to capture a connection point, such as a call to a capture method

Advice: Notification, the action that "tangent" produces for a "connection point". For example, authentication, logging, and so on.

The entire implementation of AOP can be simply understood as the Pointcut (PointCut) captures a call to the connection point (Joinpoint) and adds the notification (advice what we want to do) to the calling process.

Look at the following picture:

From the diagram above, let's look at an example:

First we define the Usermanager interface and the implementation class Usermanagerimpl, and we give only usermanager examples here.

Java code public interface Usermanager {public void AddUser (string username, string password);                  public void Deluser (int userId);                  Public String Finduserbyid (int userId);     public void ModifyUser (int userId, string Username, string password); }

  Java code   public class usermanagerimpl implements usermanager {               public void adduser (string username ,  string password)  {              System.out.println ("---------usermanagerimpl.add ()--------");         }               public void deluser (int  userid)  {             system.out.println ("---------Usermanagerimpl.deluser ()--------");         }               public string finduserbyid (int  USERID)  {             system.out.println ("---- -----UsermanAgerimpl.finduserbyid ()--------");              return  "Zhang San";         }               public void modifyuser (Int userid, string username,  string password)  {              System.out.println ("---------usermanagerimpl.modifyuser ()--------");          }    }      

Here we use the annotation way, so that we can see the various parts of AOP more intuitively, of course, we remember to enable AOP in the configuration file. The reader can also complete the example completely using the configuration file. Java code  //As above, all accesses are verified, then we find the tangent aspect.     @Aspect      Public  class SecurityHandler {         // We can simply interpret pointcut as: the expression that determines which methods are to be intercepted, and the method that starts with add in the example.           @Pointcut ("Execution (* add* (..))")   //  define Pointcut, the name is the following identity "Addaddmethod"          private  void addaddmethod () {};    //This method is just an identity, equivalent to the ID of the pointcut defined in the configuration file, this method has no return value and parameter                    @Before (" Addaddmethod () ")   //defines the advice that represents advice to which pointcut subscriptions joinpoint           private void checksecurity ()  {              system.out.println ("-------Checksecurity-------");         }            }   

Here we see aspect, Pointcut, Advice. The rest is Joinpoint, the Usermanager method of the AddUser class in the example.

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.