Spring (vi)----Spring AOP Overview and underlying implementation principles

Source: Internet
Author: User

Overview of AOP 1. What is the technology of AOP.
In the software industry, AOP is the abbreviation for Aspect oriented programming, meaning: aspect-oriented programming
AOP is a programming paradigm that is subordinate to the software category and guides the developer on how to organize the program structure
AOP was first proposed by the organization of AOP alliances, and a set of specifications was developed. Spring introduces AOP ideas into the framework and must adhere to the specifications of the AOP Alliance
A technique to realize the unified maintenance of program function by pre-compiling and running-time dynamic agent
AOP is a continuation of OOP, a hotspot in software development, an important content in the spring framework, and a derivative model of functional programming.
AOP enables the isolation of parts of the business logic, which reduces the coupling between parts of the business logic, improves the reusability of the program, and improves the efficiency of development.
2. AOP: Aspect-oriented programming (thought---solving oop problems)
3. AOP takes the horizontal extraction mechanism instead of the traditional vertical inheritance system repetitive code (performance monitoring, transaction management, security checks, caching)
4. Why to learn AOP
The program can be enhanced without modifying the source code.
The underlying implementation of the spring Framework's AOP The srping framework's AOP technology is also used by the agent technology, the proxy way provides two kinds of
1. JDK-based dynamic agent
Must be interface-oriented, only classes that implement a specific interface can generate proxy objects. Generates a proxy object at run time.
2. Based on cglib dynamic agent
For classes that do not implement an interface, you can also generate proxies that produce a subclass of this class. It is possible to generate proxy classes at compile time, and it is possible to generate proxy objects when classes are loaded. In any case, the proxy object will be generated.
Spring's traditional AOP uses different proxy methods depending on whether the class implements interfaces
1. If you implement a class interface, use the JDK dynamic agent to complete the AOP
2. If the interface is not implemented, use Cglib dynamic Agent to complete the AOP


Self-interpretation: in fact, the essence of the agent is, through the birth of a proxy object. Then, the proxy object is accessed to determine the execution of the target object.
Dynamic agent for JDK (code understanding, Understanding Principle)


1. Some code for generating proxy objects using the proxy class is as follows:

/** 
 * Generate proxy objects using JDK * 
 /Public  
class Myproxyutils {public  
    static Userdao getproxy (Final Userdao dao) { c4/>//uses the proxy class to generate  
        the Agent object Userdao proxy = (Userdao) proxy.newproxyinstance (Dao.getclass (). getClassLoader (),  
                Dao.getclass (). Getinterfaces (), new Invocationhandler () {  
  
                    //proxy object method is always line, the Invoke method executes a public  
                    object Invoke ( Object Proxy, method, object[] args) throws Throwable {  
                        if ("Save". Equals (Method.getname ())) {  
                            System.out.println ("Logging ...");  
                            Open transaction  
                        }  
                        //COMMIT TRANSACTION  
                        //Let the DAO class's Save or Update method execute normally  
                        return Method.invoke (dao, args);}  
                );  
        //Return proxy object return proxy  
        ;  
    }  
}  
JDK-based dynamic agent Cglib's agent Technology (code understanding)1. Introduction of Cblib Development Package
* If you want to use Cglib technology to generate proxy objects, then you need to introduce Cglib's developed jar package, the Cglib development package has been introduced in the Spring Framework core package. So you can directly introduce the spring core development package


2. Writing the relevant code

public class Mycglibutils {/** * uses Cglib to generate proxy objects *//generate subclasses, use parent class to receive public static Bookdaoim  
        PL getproxy () {Enhancer enhancer = new enhancer (); Sets the parent class.  
        Because Cglib is to create subclasses Enhancer.setsuperclass (Bookdaoimpl.class); Sets the callback function Enhancer.setcallback (new Org.springframework.cglib.proxy.MethodInterceptor () {//proxy object's party Method executes, the callback function executes//NOTE: Methodproxy is the previous parameter method, the underlying generates a proxy object @Override public object  
                Intercept (Object obj, method method, object[] args, Methodproxy methodproxy) throws Throwable {  
                if (Method.getname (). Equals ("Save")) {System.out.println ("Logging ...");  
            }//Normal execution return methodproxy.invokesuper (obj, args);  
        }  
        });  
  
        Generate proxy object Bookdaoimpl proxy = (Bookdaoimpl) enhancer.create (); Return ProXY   }  
}
If you write a program that provides an interface, spring chooses the JDK dynamic agent. If no interface is provided, Spring chooses Cglib.
The JDK dynamic agent is a dynamically generated proxy object during runtime. Cglib is a subclass that is generated when the class is loaded.
The way of Cglib

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.