Spring AOP Terminology Interpretation

Source: Internet
Author: User
Tags throw exception

The first look at AOP, comes up with a lot of terminology, but also a name for the wind, face-cutting programming, are said to be a useful complement of OOP and so on. Suddenly let you at a loss, thought: No wonder many people and I said that AOP is more difficult. When I looked into it, I realized that it was a simple and unpretentious application on Java basis, including the IOC, and many of these nouns, all original aim.

2. Why Use AOP

1 is for the sake of convenience, look at a foreign very famous Master said, programming people are "lazy", because he did all of the things let the program do. It's enough to use AOP to get you to write a lot less code.
2 is for clearer logic that allows your business logic to focus on your own business without thinking about other things, including security, things, logs, and so on.
nbsp 3. The terminology of those AOP
At first look at so many terms, all of a sudden is not good to accept, slowly, will soon understand.
1. Notice (Advice)
Is the function you want, that is, the security, the things, the logs, and so on. You define the first and then use it where you want to use it.
2. Connection point (Joinpoint)
This is a better explanation, that is, spring allows you to use the place of notification, that is really more, the basic of each method before, after (both have a row), or throw an exception can be a connection point, Spring only supports method connection points. Other as ASPECTJ can also let you in the constructor or attribute injection, but that is not our concern, just remember, and method related to the back and forth (throw exception), is the connection point.
3. Entry point (Pointcut)
On the basis of the connection point above, to define the pointcut, one of your classes, there are 15 methods, then there are dozens of connection points of the pair, but you do not want to use the notice near all methods (using called weaving, later), you just want to make a few of them, before calling these methods, To do something later or throw an exception, use the Pointcut to define these methods, let the pointcut filter the connection points, and select the ones you want.
4. Facets (Aspect)
Facets are a combination of notifications and pointcuts. Now find out, no connection point what things, connection point is to let you understand the pointcut, get out, understand the concept on the line. The notice explains what to do and when to do it (when you know by Before,after,around in the method name), and the pointcut shows where to go (specifying exactly which method), which is a complete definition of the facet.
5. Introduction (Introduction)
Allows us to add a new method property to an existing class. This is not to use the tangent (that is, the new method attribute: notification definition) in the target class?
6. Goal (target)
The target class mentioned in the introduction, that is, the object to be notified, which is the real business logic, can be woven into the plane without knowing it. And you focus on the logic of the business itself.
7. Agent (proxy)
How to implement the whole set of AOP mechanism, is through the proxy, this one will give detail.
8. Weave in (weaving)
The process of applying facets to the target object to create a new proxy object. There are 3 ways that spring uses the runtime and why it is run-time, explained later.
The point is: the pointcut defines which connection points are notified

4. I understand the principles of AOP

Spring uses proxy classes to wrap facets and weave them into spring-managed beans. In other words, the proxy class is disguised as the target class, it intercepts the call to the method in the target class, so that the caller calls the target class first to call the Masquerade class, the masked class first executes the slice, and then forwards the call to the real target bean.
Now you can think about it, how to get out of this camouflage class, will not be discovered by the caller (over the JVM check, Java is a strong type check, where to check the type).
1. Implement the same interface as the target class, I also implement the same interface as you, anyway, the upper layer is the interface level of the call, so I disguised as the same class as the target class (implemented the same interface, we are brothers), also escaped the type check, to the Java runtime, Using polymorphic late binding (so spring uses runtime), the camouflage class (proxy class) becomes the real implementation of the interface, and he wrapped the real target class, and finally achieve the specific function of the target class, but the camouflage class in the past to do something (log, security, things, etc.).
It's like, a person let you do one thing, every time this time, your brother will first out, of course, he does not come out, thought is you, you this brother although can't do this matter, but he knew you can do, so agreed to come down, and received a little gift (write a log), received the gift, give the thing to others do Ah, So your brother came to see you again, and finally it was done or you. But you do not know your brother has received a gift, you just concentrate on this matter well.

Along this line of thought, if itself this class does not implement an interface, how you disguise me, I have no chance to let you get out of this twin brother, then use the 2nd proxy way, create a subclass of the target class, a son, let the Son disguise me

2. Generate subclass calls, this time with subclasses as camouflage class, of course, this can also escape the strong type of JVM check, I inherited it, of course, the sub-class rewrite all the methods of the target class, of course, in these overridden methods, not only to achieve the function of the target class, but also before these functions, Implements some other (write log, security check, things, etc.).
This time the contrast is, the son first from father that skill all learned, all look for son to do things, but son every time and father the same thing, all need to take a small gift (write log), and then to do the real thing. Of course, Dad didn't know his son did that. Here is one thing to say, some of the ability is the father's unique (final), the son can not learn, can not do this thing, can not do this thing, nature can not accept the gift of others.

The former sibling pattern, Spring uses the JDK's Java.lang.reflect.Proxy class, which allows spring to dynamically generate a new class to implement the necessary interfaces, weave notifications, and forward any calls to those interfaces to the target class.

In the latter parent-child mode, Spring uses the Cglib library to generate a subclass of the target class, and when the subclass is created, spring weaves the notification and delegates the call to the subclass to the target class.
In contrast, the brother mode is better, he can be more loosely coupled, especially in today's shouting interface-oriented programming situation, the parent-child mode is only when the interface is not implemented, can also weave notification, should be treated as an exception.

Spring AOP Terminology Interpretation

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.