AOP summary post of spring learning the first AOP program of spring learning

AOP (aspect oriented programming), also known as aspect oriented programming, is a programming paradigm, which provides another perspective to consider the program structure, so as to improve object-oriented programming (OOP).

During OOP development, They are all based on developing components (such as classes) and then combining components. The biggest problem of OOP is that it is unable to decouple components for development. For example, in the above example, AOP is to overcome this problem and separate this coupling. AOP provides developers with a crosscutting concern (such as log concerns) separation and weaving mechanism, which separates crosscutting concerns, and then weaves them into the system through some technology, so as to complete our function without coupling.

1 AOP overview

AOP (aspect oriented programming): it is a new methodology and a supplement to the traditional OOP (object-oriented programming). The main programming object of AOP is aspect, which modularizes crosscutting concerns. When applying AOP programming, you still need to define common functions, but you can clearly define where and how this function is applied, and there is no need to modify the affected classes In this way, crosscutting concerns are modularized into special objects (Facets). AOP more concepts.

AOP benefits:

2 Spring AOP

In spring 2 In versions above 0, AOP based on AspectJ annotation or XML configuration can be used. Aspectjava is the most complete and process AOP framework in the Java community.

2.1 using aspectjava annotation support in spring

To use AspectJ annotations in spring applications, you must include the AspectJ class library under the classpath: aopalliance jar、aspectj. weaver. Jar and spring - aspects jar。 Then add the AOP schema to the < beans > root element. Enable AspectJ annotation support in the spring IOC container. Just define an empty XML element < AOP: AspectJ AutoProxy > in the bean configuration file. When the spring IOC container detects the < AOP: AspectJ AutoProxy > element in the bean configuration file, it will automatically create a proxy for the beans matching the AspectJ aspect.

In AspectJ annotation, aspect is just a Java class with @ aspect annotation. Notification is a simple Java method marked with some annotation. AspectJ supports the following five types of notification annotations:

2.2 writing AspectJ pointcut expressions using method signatures

The most typical pointcut expression matches various methods according to their signatures:

In AspectJ, pointcut expressions can use the operators & &, |! Combine.

2.3 using the AOP program example, all five notifications are used

First, define a hello interface. The code is as follows:

Then define a hello implementation class helloimpl, and the code is as follows:

Then define a hello aspect class, helloaspect, as shown below. Note: the Hello interface, helloimpl class and helloaspect class are all in com Under the AOP package.

Configure aopcontext XML file, as follows:

Test class aoptest class:

The final output is:

Now the question is, what is the order of various notices? In fact, it can also be seen from the output results. The execution order of various notifications in the same class is:

Notification before around > = before notification > = target method execution > = notification after around > = after notification > = afterreturn notification

2.4 priority of specified sections

When more than one slice is applied to the same connection point, their priority is uncertain unless explicitly specified. The priority of the aspect can be specified by implementing the ordered interface or using the @ order annotation. Implement the ordered interface. The smaller the return value of the getorder () method, the higher the priority If the @ order annotation is used, the sequence number appears in the annotation.

2.5 reuse pointcut definitions

When writing AspectJ facets, you can write pointcut expressions directly in the notification annotation However, the same pointcut expression may be repeated in multiple notifications. In AspectJ aspect, a pointcut can be declared as a simple method through @ pointcut annotation The method body of the pointcut is usually empty because it is unreasonable to mix the pointcut definition with the application logic. The access control character of the pointcut method also controls the visibility of the pointcut If pointcuts are to be shared in multiple aspects, it is best to centralize them in a common class In this case, they must be declared public When you introduce this pointcut, you must include the class name If the class is not placed in the same package as this aspect, it must also contain the package name. Other notifications can introduce the pointcut through the method name.

3 aspect of XML based configuration declaration

In addition to using AspectJ annotations to declare aspects, spring also supports declaring aspects in bean configuration files This declaration is done through the XML elements in the AOP schema. Under normal circumstances, annotation - based declarations take precedence over XML - based declarations Through AspectJ annotation, aspects can be compatible with AspectJ, while XML based configuration is spring specific Because AspectJ is supported by more and more AOP frameworks, facets written in annotation style will have more opportunities for reuse.

3.1 declared section

When declaring aspects in XML, you need to import AOP schema into the < beans > root element. In the bean configuration file, all spring AOP configurations must be defined inside the < AOP: config > element. For each aspect, you need to create a < AOP: aspect > element to reference the back-end bean instance for the specific aspect implementation.

3.2 declaration of entry points and notifications

Pointcuts are declared using < AOP: pointcut > elements. Pointcuts must be defined under < AOP: sapect > elements or directly under < AOP: config > elements.

The notification element uses < pointcut ref > to reference the pointcut, or directly embeds < pointcut > into the pointcut expression. The method attribute specifies the name of the notification method in the aspect class.

reference material:

1. The first AOP program for spring learning

  2、Spring4. 0 video tutorial from getting started to becoming proficient

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>