The first AOP program for spring learning

IOC and AOP are the two cornerstones of spring. AOP (aspect oriented programming), also known as aspect oriented programming, is a programming paradigm that 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. Basic concepts of AOP

What notification types does spring have?

In AOP, select the connection point of the target object through the pointcut, and then weave the notification at the corresponding connection point of the target object. The pointcut and notification are facets (crosscutting concerns), and the implementation of applying facets at the connection point of the target object is through the AOP proxy object, as shown in the figure.

2. HelloWorld program of AOP

AOP proxy is an object created by the AOP framework through proxy mode. Spring uses JDK dynamic proxy or cglib proxy to implement it. Spring uses JDK dynamic proxy by default, so that any interface can be proxy. If the proxy object implementation is not an interface, cglib proxy will be used by default, but cglib proxy can also be applied to the interface. The purpose of AOP proxy is to weave the slice into the target object.

Create a new project and import the corresponding package in spring. The last introduced jar package is as follows (some packages are not available in spring lib and need to be downloaded separately):

(1) Define target interface

(2) Define the target interface implementation class

(3) Define facet support classes

(4) Configure in XML

Pointcuts are configured using < AOP: pointcut > under the < AOP: config > tab. The expression attribute is used to define the pointcut mode. The default is AspectJ syntax, "execution (* CN. Javass.. *. * (..)" Indicates matching CN Any method execution under javass package and sub package. For how to configure the expression property, click: expression configuration

The facet is configured using the < AOP: aspect > tag under the < AOP: config > tag, where "ref" is used to reference the methods of the facet support class. The front notification is defined by the
tag under the

tag, the pointcut-ref attribute is used to reference the pointcut Bean, and the method is used to refer to the method of the section notification to implement the class. This method is the notification implementation, that is, the method that is called before the execution of the target class method. The final notification is defined by the < AOP: after > tag under the < AOP: aspect > tag. In addition to using the pointcut ref attribute to refer to the existing pointcut, the pointcut attribute can also be used to define the pointcut, such as pointcut = "execution (* CN. Javass.. *. * (..)", The method attribute is also the specified notification implementation, that is, the method that is called after the execution of the target class method.

(5) Test run

(6) Output results

reference material

1. Learn from me about spring MVC catalog summary post, pdf download and source code download

2. The first hello world program for spring learning

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
分享
二维码
< <上一篇
下一篇>>