Hello, today we are going to share Spring AOP with you. Take out your notebook and write it down

AOP is introduced

Aspect oriented programming (AOP) is a programming pattern similar to object oriented programming (OOP). Spring AOP is a framework based on AOP programming pattern. Its use effectively reduces the duplication of code between systems and achieves the purpose of loose coupling between modules. It isolates all parts of the business logic so that developers can focus on the core business while writing the business logic, thus improving development efficiency. AOP adopts horizontal extraction mechanism to replace the repetitive code of traditional vertical inheritance system, and its application is mainly reflected in transaction processing, log management, authority control, exception handling and so on.

Two of the most popular AOP frameworks are Spring AOP and AspectJ. Since Spring 2.0, Spring AOP has introduced support for AspectJ.

Technical Terms

  • Advice: Enhanced processing in an AOP framework. The advice describes when and how the enhanced processing is performed by the aspect.
  • Join point: A join point is a point at which a section can be inserted during application execution. This point can be a method call or an exception thrown. In Spring AOP, join points are always method calls.
  • Pointcuts: Join points where enhanced processing can be inserted.
  • Aspect: An Aspect is a combination of advice and pointcuts.
  • Introduction: Introduction allows you to add new methods or attributes to an existing class.
  • Weaving: Weaving is the process of adding enhancement processing to the target object and creating an enhanced object.

Features of Spring AOP

AOP in Spring is implemented through dynamic proxies. Different AOP frameworks also support different join points. For example, AspectJ and JBoss support join points for fields and constructors in addition to method pointcuts. While Spring AOP cannot intercept changes to object fields and does not support constructor join points, we cannot apply notifications at Bean creation time.

AOP classification

  • Static AOP implementations, where the AOP framework makes changes to the program’s source code at compile time to generate static AOP proxy classes (generated *.class files have been changed to require the use of a specific compiler) such as AspectJ.
  • Dynamic AOP implementations, where an AOP framework dynamically generates proxy objects (JDK dynamic proxies in memory, or CGlib dynamically generates AOP proxy classes) at runtime, such as SpringAOP.

Comparison of various AOP implementations

Dynamic proxies are covered in design patterns, but not in this case.

Spring AOP use

There are two ways to use it: One is using the Spring in the org. Springframework. Aop) framework. ProxyFactoryBean class, the class corresponding to the starting point and notification provides a complete control ability, and can generate the specified content, but rarely used this way in the development. The second approach is typically used in development, where Spring uses AspectJ to develop AOP. This approach is primarily implemented based on XML and annotations, and is mainly described below.

Spring uses AspectJ to develop AOP

Typically developed using annotations, AspectJ allows annotations to define aspects, pointcuts, and enhanced processing, while the Spring framework recognizes and generates AOP proxies based on these annotations.

The implementation process is as follows: Introduce the Spring JAR package — “create the facet class” — “create the Spring configuration file (not necessary if using annotations)

Notification type:

Pointcut expression

Pointcut expressions are mainly used to configure which methods of which classes to intercept, and are often used below by @pointcut.

Grammar:

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)

Parameter explanation:

  • modifiers-pattern? 【 modify type, can not write 】
  • Ret-type-pattern [method return value type, required]
  • declaring-type-pattern? Method declaration type, can not write
  • Name-pattern (param-pattern)
  • throws-pattern? The exception type thrown by the method can be omitted.

Symbol explanation:

  • ? The sign stands for 0 or 1, so you don’t have to write it
  • The * symbol represents any type, 0 or more
  • The method parameters are… Represents a variable parameter

Official case explanation:

Annotation way

You first need to turn on the AOP annotation approach in your Spring configuration file. XML Enabled

Notes introduction:

  1. @aspect: Specifies a class as the Aspect class
  2. @pointcut: Specifies the Pointcut expression that tells you which methods to enhance last. The method in which the annotation is made must be private
  3. @before: Pre-notification: executed Before the target method
  4. @after: post notification: execute After target method (always execute)
  5. @afterreturning: Notification after return: Execution before completion of execution method (exception not executed)
  6. @afterthrowing: Exception notification: Executed when an exception occurs
  7. @around: circular notification: execute Around target method (common)

Use cases: mainly provides the code case of the cutting class, which class to intercept, the test code is not provided.

XML implementation

Xml-based declarative means defining aspects, pointcuts, and declarations of advice in the form of a Spring configuration file, and all aspects and advice must be defined in an AOP :config element. Here’s an example:

The < beans XMLNS = “www.springframework.org/schema/bean…”

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <! - the target class - > < bean id = "customerDao" class = "com. Mengma. Dao. CustomerDaoImpl" / > <! - cut class - > < bean id = "myAspect" class = "com. Mengma. Aspectj. XML. MyAspect" > < / bean > <! --> < AOP :config> < AOP :aspect ref="myAspect"> < AOP :pointcut expression="execution (* com.mengma.dao.*.* (..)) )" id="myPointCut" /> <! <aop:before method="myBefore" pointeut-ref="myPointCut" /> <! Postnotification, executed after the method returns, Aop: returning returning method="myAfterReturning" pointcut-ref="myPointCut" returning="returnVal" / > <! Aop :around method="myAround" pointcut-ref="myPointCut" /> <aop:around method="myAround" pointcut-ref="myPointCut" /> <! Throw notification: used to handle exceptions that can be received from the current method --> <! -- * Note: Enhancement will not be performed if the program has no exceptions --> <! -- * throwing property: Aop :after-throwing method="myAfterThrowing" pointcut-ref="myPointCut" throwing="e" /> <aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointCut" throwing="e" /> <! <aop:after method="myAfter" pointcut-ref="myPointCut" /> </aop:aspect> </aop:config>Copy the code

Well, this is the end of today’s article, I hope to help you confused in front of the screen