Implementation Mechanism
Spring AOP is to achieve a dynamic proxy. Spring when running, will generate for a moving target Bean Agent Bean, and when the caller really is not directly used to access the target Bean, but the use of proxy Bean
important point
- Agent Bean produced using ProxyFactory
when it is programmed to use or Xml configuration, the focus of a class to be used, it is crucial to achieve SpringAOP of class. We can see from the name out of it is a proxy factory class. - Internal use a different proxy to achieve under the circumstances
- Agent Bean injected into the caller for use
This is Spring's style
Two kinds of agents to achieve
Agents have at least two in Java, JDK agent one is, one is an open source implementation of the package agent
fancy FIG, JDK proxy agent is required to achieve a target interfaces, CGLIB-agent does not need any interfaces target Bean . Therefore, if the target Bean implements the interface, the above two methods are feasible; if the target Bean does not implement any interface, it can only consider the use of CGLIB.
If you consider the performance from the words, JDK CGLIB higher than the general performance of some.
Type of connection points
The connection point is used to indicate where in the notification, Spring which currently supports only one connection point type - the method call. That notify us in writing only method call on such a scene can be used for other applications not on the scene.
However, under certain scenarios, the connection point is not a method call, then you can integrate more powerful AspectJ, which provides support for more types of connection points.
Notification type
Fancy, listing the various types of notice, there are methods before calling the Before there after calling the method After back and forth, the method can be used to run the Around . After divided into two types: After Returning , After Throwing .
Method to realize
The following are supported by Spring AOP implementations:
- Use ProxyFactory
using pure Java code to use programmatic writing, the core of a class is ProxyFactory - ProxyFactoryBean configuration
using Xml to configure a ProxyFactoryBean - Use AOP namespace
currently using more popular - Use of AspectJ annotation