8.4.3: basic concepts of AOP

Source: Internet
Author: User

Introduction:

If you have installed the Java decompilation tool, you can decompile the hello in the previous article. class file, we will find the hello. the class file is not created by Hello. the Hello. class added a lot of content ------ this indicates that aspectj has enhanced Hello at compilation. class, so
AspectjIt is usually calledThe AOP framework enhanced during compilation.

There is another AOP framework opposite to aspectj, which does not need to beThe target class is enhanced during compilation,The proxy class of the target class is generated at runtime. The proxy class either implements the same interface as the target class or is a subclass of the target class. In short, the proxy class enhances the target class, the former is the processing policy of JDK dynamic proxy, and the latter is the processing policy of cglib proxy.

Spring AOPGenerate a proxy class by creating a dynamic proxy. JDK dynamic proxy or cglib proxy can be used at the underlying layer.

In general, the enhanced AOP framework during compilation has more performance advantages-because the dynamically enhanced AOP framework during runtime needs to be dynamically enhanced at each runtime.

The basic concepts of AOP:

AOP extracts the aspect of the business processing process from the perspective of program running. AOP targets various steps in the program running, and wants to better combine the steps of business processing.

The AOP framework is not coupled with specific code. The AOP framework can process special pointcut in program execution, rather than coupling with a specific class. The AOP framework has the following two features:

① Good isolation between steps.

② Source code independence.

The following is aboutSome terms for Aspect-Oriented Programming:

① Aspect (aspect ):A specific step of business flow operation, that is, the focus of the application operation process. The focus may cross multiple objects, so it is often called a cross-cutting focus.

② Join point (joinpoint ):A specific point in the program execution process, such as calling a method or throwing an exception. In Spring AOP, connection points are always called by methods.

③ Advice ):The enhanced processing of the AOP framework in a specific starting point. The processing methods include round, before, and after.

④ Pointcut ):You can insert a connection point for enhanced processing. In short, when a connection point meets the specified requirements, the connection point will be added for enhanced processing, and the connection point will become the starting point. For example, the following code:

pointcut xxxPointcut()   :execution(void H*.say*())

Each method is called only as a connection point. However, if the method belongs to a class starting with h and the method name starts with "say", the call execution of the method will become a starting point. How to use expressions to define the starting point is the core of AOP. Spring usesAspectj entry point Syntax:

① Introduction:Add methods or fields to the processed class. Spring allows the introduction of new interfaces to any processed objects. For example, you can use an introduction to implement the ismodified interface for any object to simplify the cache.

② Target object:Objects that are reinforced by the AOP framework are also called reinforced objects. If the AOP framework is implemented in the runtime era, this object will be a proxy object.

③ AOP Proxy:Objects Created by the AOP framework, in short, proxy is the enhancement of the target object. The AOP proxy in spring can be JDK dynamic proxy or cglib proxy. The former is the proxy of the target object that implements the interface, and the latter is the proxy of the target object that does not implement the interface.

④ Weaving ):The process of adding the enhancement process to the target object and creating an enhanced object (AOP proxy) is woven. There are two ways to implement weaving: compile-time enhancement (such as aspectj) and runtime enhancement (such as cglib ). Like other pure Java AOP frameworks, spring completes weaving at runtime.

As described earlier:AOP proxyIt is actually an object dynamically generated by the AOP framework. This object can be used as the target object. The AOP proxy contains all the methods of the target object, but there are differences between the methods in the AOP proxy and the methods of the target object: The AOP method adds enhanced processing to a specific entry point, the method of the target object is called back.

The methods and target objects contained by the AOP proxy are as follows:

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.