Detailed introduction of Java AOP knowledge
- 2020-05-30 20:18:38
- OfStack
Java AOP
AOP knowledge organization
AOP(Aspect-Oriented Programming) : aspect oriented programming. OOP(Object-Oriented Programming) object-oriented programming. For OOP, we are familiar with it. For AOP, we may think it is a new feature. In fact, AOP is a supplement to OOP, OOP is oriented to vertical programming, while AOP is oriented to horizontal programming.
Aspect oriented programming (AOP) compensates for object-oriented programming (OOP) by providing another way to think about program structure. The key unit of modularity in OOP is the class (classes), while the unit of modularity in AOP is the aspect. Facets modularize concerns, such as transaction management that crosscuts multiple types and objects.
The AOP framework is an important part of spring. However, the Spring IoC container does not rely on AOP, which means you have the option of using AOP as a complement to the Spring IoC container, making it a powerful middleware solution.
The role of AOP in Spring Framework
1. AOP concept:
To learn AOP, of course, you must first understand its many conceptual terms:
Notification type:
Surround notification is the most common type of notification. Like AspectJ1, Spring provides all types of notifications, and we recommend that you use the simplest notification type possible to achieve the desired functionality. For example, if you just need the return value of one method to update the cache, it's best to use a post notification instead of a wrap notification, although a wrap notification can do the same thing. The most appropriate notification type can simplify the programming model and avoid many potential errors. For example, you don't need to be on JoinPoint
Call the proceed() method used to wrap around the notification, and there is no call problem.
In this case, I won't write AOP based on @AspectJ, because I prefer Spring to create AOP agents using ProxyFactoryBean.
2. Create AOP agent using ProxyFactoryBean:
Create a AOP in Spring agent. The basic method is to use org springframework. aop. framework. ProxyFactoryBean. This class provides complete control over the pointcuts and advice to be applied, including the order in which they are applied. Like other FactoryBean implementations 1, ProxyFactoryBean introduces an indirection layer. If you define an ProxyFactoryBean named foo, the object that references foo will see not the ProxyFactoryBean instance itself, but the object created by the getObject() method in the ProxyFactoryBean implementation. This method creates an AOP proxy that wraps a target object.
The ProxyFactoryBean class itself is also an JavaBean, and its properties are mainly used for the following purposes:
1 some main properties from org. springframework. aop. framework. ProxyConfig inherited in (this class is the parent of all AOP Spring agent factory). These main attributes include:
Other properties to be explained in ProxyFactoryBean include:
The name here is the name of bean in the current factory, including the name of bean in the parent factory. You can't use the bean reference here because it will cause ProxyFactoryBean to ignore the singleton Settings for notifications.
You can suffix the name of an interceptor with an asterisk (*). This will result in all notifiers in the application whose names start before the asterisk being applied.
Singleton: whether the factory should return the same object, no matter how often the method getObject() is called. Several FactoryBean implementations provide this method. The default value is true. If you want to use stateful notifications -- for example, stateful mixin-- you can use stereotype notifications by setting the value of the singleton property to false.
3. Agents based on JDK and CGLIB:
A proxy based on CGLIB will be created if a class that requires the target object to be proxied (we will simply call it the target class later) does not implement any interface. This is the simplest scenario, because the JDK proxy is interfact-based, and no interface means that there is no possibility of proxy using JDK.
If the proxyTargetClass property of ProxyFactoryBean is set to true, then a proxy based on CGLIB will be created. Such a rule makes sense, following the rule of least surprise (which guarantees a set of 1 uniqueness). Even when ProxyFactoryBean's proxyInterfaces property is set to one or more fully qualified interface names, and proxyTargetClass's true property is set to true, the CGLIB-based proxy will actually be used.
If the ProxyFactoryBean proxyInterfaces property is set to one or more fully qualified interface names, a proxy based on JDK will be created. The proxy created will implement all the interfaces described in the proxyInterfaces property; If the target class implements all of the interfaces described in the proxyInterfaces property as well as some additional interfaces, the returned proxy will implement only the specified interfaces and not those additional interfaces.
If ProxyFactoryBean's proxyInterfaces property is not set, but the target class implements one (or more) interfaces, then ProxyFactoryBean will automatically detect that the target class has implemented at least one interface, and a proxy based on JDK will be created. The interfaces that are actually proxied will be all the interfaces that the target class implements; In fact, this is similar to listing each interface implemented by the target class in the proxyInterfaces property. However, this significantly reduces the amount of work and the likelihood of typos.
Thank you for reading, I hope to help you, thank you for your support of this site!