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

Provide declarative enterprise services, especially as an alternative to EJB declarative services. The most important service is declarative transaction management (which I think is the most used part of AOP). Allows the user to implement custom facets to improve the use of OOP with AOP.

1. AOP concept:

To learn AOP, of course, you must first understand its many conceptual terms:

Aspect (Aspect) : modularity of a concern that may crosscut multiple objects. Transaction management is a good example of a crosscutting concern in an J2EE application. In Spring AOP, cut surfaces can be implemented using either pattern-based) or @Aspect annotation. Join point (Joinpoint) : a specific point in the execution of a program, such as when a method is called or when an exception is handled. In Spring AOP, a join point always represents the execution of a method. Notification (Advice) : an action performed at a particular join point on the cut surface. This includes different types of notifications such as "around," "before," and "after" (the types of notifications are discussed later). Many of the AOP frameworks (including Spring) use the interceptor as a notification model and maintain a chain of interceptors centered around the join point. Pointcut (Pointcut) : an assertion that matches a join point. The advice is associated with a pointcut expression and runs at a join point that satisfies that pointcut (for example, when a method of a particular name is executed). How pointcut expressions match join points is at the heart of AOP: Spring USES AspectJ pointcut syntax by default. Introduction (Introduction) : used to declare an additional method or property to a type (also known as a join type declaration (inter-type declaration). Spring allows the introduction of a new interface (and a corresponding implementation) to any proscribed object. For example, you can use the introduction to make 1 bean implement the IsModified interface to simplify the caching mechanism. Target object (Target Object) : an object notified by one or more facets. Also known as the notified object (advised). Since Spring AOP is implemented through a runtime proxy, this object is always a proxied (proxied) object. AOP broker (AOP Proxy) : object created by the AOP framework to implement aspect contracts (for example, notification method execution, and so on). In Spring, the AOP agent can be either an JDK dynamic agent or an CGLIB agent. Weaving (Weaving) : connect the cut surface to another application type or object and create one notified object. This can be done at compile time (for example, using the AspectJ compiler), class loading time, and runtime. Spring and other pure Java AOP frame 1 like, finished weaving at run time.

Notification type:

Pre-notification (Before advice) : a notification that is executed before a join point, but this notification cannot prevent the execution of the flow before the join point (unless it throws an exception). Postnotification (After returning advice) : a notification that is executed after a join point has completed normally: for example, a method returns normally without throwing any exceptions. Exception notification (After throwing advice) : notification that is executed when a method throws an exception and exits. Final notification (After (finally) advice) : a notification that is executed when a join point exits (either normal return or abnormal exit). Surround notification (Around Advice) : a notification that surrounds a join point, such as a method call. This is the most powerful type of notification. Surround notifications can complete custom behavior before and after method invocations. It also chooses whether to continue with the join point or simply return its own return value or throw an exception to end the execution.

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:

Specify the target object you want to represent Specify whether to use CGLIB.

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:

proxyTargetClass: when this property is true, the target class itself is proxied rather than the interface of the target class. If the property value is set to true, the CGLIB proxy will be created. optimize: used to control whether agents created through CGLIB use aggressive optimization policies. Users are not recommended to use this setting unless they fully understand how the AOP agent handles optimizations. Currently this property is only used for the CGLIB proxy; Invalid for JDK dynamic proxy (default proxy). frozen: if a broker configuration is frozen, modifications to that configuration are not allowed. This is useful for simple optimizations and when you don't want callers to operate on the proxy (via the Advised interface) after the proxy is created. The default value is false, which allows you to do things like add additional notifications. exposeProxy: determines whether the current agent is exposed to 1 ThreadLocal to be accessed by the target object. If the target object needs to get a proxy and the exposeProxy property is set to true, the target object can use the AopContext.currentProxy () method. aopProxyFactory: implementation using AopProxyFactory. This provides a way to customize whether to use dynamic proxies, CGLIB, or other proxy policies. The default implementation will select the dynamic proxy or CGLIB as the case may be. 1 normally there should be no need to use this property; It was designed to add a new proxy type to Spring 1.1.

Other properties to be explained in ProxyFactoryBean include:

proxyInterfaces: an array of strings that require the interface name of the proxy. If not, an CGLIB proxy is used for the target class. interceptorNames: an array of strings of Advisor that can contain the name of the interceptor or other notification. The order is important, and those first in line will be prioritized. That means that the first interceptor in the list will be able to intercept the first call.

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!

Related articles: