Spring's AOP personal understanding and use

Source: Internet
Author: User
Tags aop
Spring's AOP personal understanding and use

1 What is AOP:AOP is aspect-oriented programming, which means programming for a functional module, a typical application is spring's declarative transaction,
Spring's AOP transaction parsing:
In the previous transaction management is to be fused in the logical code, in the logical code determines whether the transaction commits or rolls back, it is easy to cause the code is difficult to maintain, code redundancy
However, with spring's declarative transactions, you can manage the operation by simply annotating the transaction on the database processing method, and the transaction settings and logical code are separated and easy to maintain.
What does 2AOP do?
: Aspect-oriented programming, such as a feature point, we only need to extract crosscutting concerns, and then let the methods that are needed to handle these function points invoke the proxy

What is the composition of 3AOP
1 Facets: crosscutting concerns modular objects---functional modularity
2 Notice: Is the aspect needs to complete the function---is the function implementation Method 1, Method 2 ...
3 Connection point: is a specific location of the program execution, such as the method before the method, but also the location of the notification of concern
4 target: The object being notified, and the object where the connection point resides
5 Proxy: An object created after the notification is applied to the target object
4AOP How to use
1: Extracting cross-cutting concerns: that is, extracting the functionality that we need to program for the facets, and then the modular object
2: Then declare the modular object as a slice
3: The module object is fine-grained, the function is subdivided into various methods
4: Connection point for declaring notifications and notifications
Login Example:

Need to call a kinetic energy before and after logging in----> that is, crosscutting concerns.
1 Facets: The modular object of crosscutting concerns--"logincheck

Package COM.AOP;

Import Org.aspectj.lang.annotation.After;
Import Org.aspectj.lang.annotation.Aspect;
Import Org.aspectj.lang.annotation.Before;
Import org.springframework.stereotype.Component;

@Aspect
@Component Public
class Logincheck {public


    void Beforelogin () {
        System.out.println (" Pre-logon Check----------> ");
    }
    
    public void Afterlogin () {
        System.out.println ("Check----------after login >");
    }
}

2 The function of the module object is fine-grained, subdivided into various methods the above code has been refined granulation

3 declaration notifications and Connection points:

@Before (value = "Execution (* com.aop.entityservice.* (..))")
public void Beforelogin (Joinpoint joinpoint) {
System.out.println (Joinpoint.getsignature (). GetName () + "Pre-logon Check---------->");
}

@After (value = "Execution (* com.aop.entityservice.* (..))")
public void Afterlogin (Joinpoint joinpoint) {
System.out.println (Joinpoint.getsignature (). GetName () + "Check----------after login >" +joinpoint.getstaticpart (). Getsignature (). Getdeclaringtype (). GetName ());
}

Login class:

Package COM.AOP;

Import org.springframework.stereotype.Repository;

@Repository public
class Entityservice {
    
    private int id;
    Private String name= "Jeremy";
    
    public void Login () {
        System.out.println (this.name+ "login------>");}
    
}

Test code:

public static void Main (string[] args) throws SQLException {ApplicationContext app
        Licationcontext=new classpathxmlapplicationcontext ("Applicationcontext.xml");
        Entityservice Entityservice=applicationcontext.getbean (Entityservice.class);
    Entityservice.login (); }

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.