1. What I know about AOPAt first glance, there is a lot of terminology, and there is a name for the wind, for the aspect programming, is a useful complement of OOP and so on. Let you at a loss, thought: Not many people and I said that AOP is more difficult. When I look inside, I can only find out: He is a simple and unpretentious application on Java basis, including the IOC (see the Spring IOC (dependency injection, inversion of control) conceptual understanding), including many of these nouns, are original aim.
2. Why use AOP1) is for the sake of convenience, see a foreign very famous Master said, programming people are "lazy", because he did things to let the program to do. It's enough to use AOP to get you to write a lot less code. 2) It's for clearer logic that allows your business logic to focus on your own business without thinking about something else. These other things include: security, things, logs, and so on.
3. Those AOP terminologyAt first look at so many terms, all of a sudden is not good to accept, slowly, will soon understand.
- notification, enhanced processing (Advice) is the function you want, that is, security, things, days, and so on. You have to define it first, and then use it where you want to use it. A section of processing code that contains aspect
- Connection point (Joinpoint) This is a better explanation, that is, spring allows you to be the place of notification (Advice), that is really much, the basic of each method of money, after (both have a line), or throw the exception is can be a connection point, spring only supports method connection point. Others, such as ASPECTJ, can also allow you to inject a constructor or attribute, but that is not our concern, as long as you remember that all the way around the method is the connection point.
- Point of Entry (Pointcut) above the point of connection, to define the pointcut, you have a class, there are 15 methods, then there are more than 10 connection points, right, but you do not want to use all method attachments are used in the notification (using called weaving, the following), you just want to let a few, before calling these methods , after or throw an exception to do something, then use the pointcut to define these methods, let the pointcut to filter the connection point, select the few you want to the method.
- A facet (Aspect) facet is a combination of a notification and a pointcut. Now find out, no connection point what thing, link is to let you understand the pointcut to get out, understand this concept on the line. The notice explains what to do and when to do it (when you know by Befor,after,around in the method name), and the second entry point shows where to go (specifying exactly which method), which is a complete definition of the facet.
- Introduction (Introduction) allows us to add new method properties to an existing class. This is not to use the tangent (that is, the new method attribute: notification definition) in the target class?
- The target class mentioned in the target introduction, the object to be notified, the real business logic, can be woven into the plane without knowing it. Two self-focus on the logic of the business itself.
- Agent (proxy) How to implement the entire set of AOP mechanism, are through the proxy, this a moment to elaborate.
- Weaving (weaving) the process of applying facets to a target object to create a new proxy object. There are three ways that Spring uses the runtime, and why it is run, in the previous article "a discussion of spring AOP Development Ramble about the use of AOP and AspectJ" in the second label mentioned.
- Target Object – The original Java component of the project.
- AOP Proxy – Generates Java objects from the AOP framework.
- AOP Proxy method = advice + method of the target object.
The following diagram simplifies and graphically illustrates the AOP
On the image, AOP is programmed like a hamburger.
Raw bread-the method of the target object. Business components on the line.
Meat Pieces-Advice
Hamburg-Method of the AOP proxy.
Add meat to bread-introduced
The point is that the facets define which connection points are notified.
4. I understand the principle of AOPSpring uses proxy classes to wrap facets, they weave into spring-managed beans, which means that the proxy class is disguised as a target class, it intercepts calls to the methods in the target class, and the caller first becomes a camouflage class for the target class, and the Masquerade class first executes the slice. Then forward the call to the real target bean. Now 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 (implementation of the consent interface, we are brothers), also escaped the type check, to the Java runtime, the use of polymorphic late binding (so spring uses the runtime), The Disguise class (proxy class) becomes the real implementation of the interface, two he wrapped the real target class, the final realization of the specific function or target class, but disguised in the past to do something (log, security, things, etc.). This is like a person let you work, every time this time, your brother will come out, of course, he does not come out, thought is you, you this brother although can't do this thing, but she 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 to do Ah, So your brother came to see you again, the last thing to do it or yourself. But you do not know your brother has already received the gift, you just concentrate on this matter well. Along this thought, if itself this class does not implement an interface, you how to disguise me, I will not have the opportunity to let you get out of this twin brother, then use the 2nd proxy method, create a subclass of the target class, give birth to a son, let the son disguise me.  2) generates a subclass call. This time with the sub-class to do camouflage, of course, this can escape the JVM's strong type 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, implemented some other (write log, security check, Things, etc.). The contrast is that the son first from the father to learn all the skills, all looking for a son to work, but the son every time and dad the same thing before, all have 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 there is something to say, some skills is the father of the unique (final), the son will not learn, can not do this thing, can not do this thing, nature can not accept the gift of others. Former sibling mode, Spring uses the Java.lang.reflect.Proxy class of the JDK, which allows spring to dynamically generate a new class to implement the necessary interfaces, weave notifications, and take any of these interfacesAll calls are forwarded 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. By contrast, it's better to have a good sibling model, and she can do a lot more loose coupling, especially if today's shouting about interface-oriented programming, the parent-child mode can weave notifications only when no interface is implemented, and should be treated as an exception.
This article link address: AOP Those academic concepts-notification, enhanced processing connection point (Joinpoint) facets (Aspect)
Spring AOP Those academic concepts-notification, enhanced processing connection point (Joinpoint) facets (Aspect)