Do you know the AOP that you must ask in interviews? How can it be achieved through Spring?

Aspect Oriented Programing Aspect Oriented Programming, compared with oop object-oriented programming, Aop is no longer concerned with a certain class or some methods in the program code, while aop considers more of a face-to-face cut, namely A kind of cutting between layers, so it is called a cut surface. Think of everyone’s burger (with meat in the middle). So how does aop intercept the entire surface? Considering the configuration of servlet urlpattern /* learned, it is actually the realization of aop.

How Spring Aop is implemented

  • Annotation method
  • XML way

Case practice

Annotation method

Introducing jar package coordinates

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.9</version>
</dependency>

beans.xml configuration

Add namespace

xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop.xsd

Configure Aop proxy

<aop:aspectj-autoproxy/>

Write aop implementation class

/**
* 声明切面组件
*/
@Component
@Aspect
public class LogCut {
    
    
    /**
    * 定义切入点 匹配方法规则定义
    * 匹配规则表达式含义 拦截 com.xxx.service 包下 以及子包下 所有类的所有方法
    */
	@Pointcut("execution (* com.xxx.service..*.*(..))")
	public void cut(){
    
    }
    /**
    * 声明前置通知 并将通知应用到定义的切入点上
    * 目标类方法执行前 执行该通知
    */
    @Before(value="cut()")
    public void before(){
    
    
   	 	System.out.println("前置通知.....");
    }
    /**
    * 声明返回通知 并将通知应用到切入点上
    * 目标类方法执行完毕执行该通知
    */
    @AfterReturning(value="cut()")
    public void afterReturning(){
    
    
    	System.out.println("返回通知....");
    }
    /**
    * 声明最终通知 并将通知应用到切入点上
    * 目标类方法执行过程中是否发生异常 均会执行该通知 相当于异常中的 finally 
    */
    @After(value="cut()")
    public void after(){
    
    
    	System.out.println("最终通知....");
    }
    /**
    * 声明异常通知 并将通知应用到切入点上
    * 目标类方法执行时发生异常 执行该通知
    */
    @AfterThrowing(value="cut()",throwing="e")
    public void afterThrowing(Exception e){
    
    
    	System.out.println("异常通知....方法执行异常时执行:"+e);
    }
    /**
    * 声明环绕通知 并将通知应用到切入点上
    * 方法执行前后 通过环绕通知定义相应处理
    */
    @Around(value="cut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
    
    
        System.out.println("环绕前置...");
        System.out.println("环绕通知");
        System.out.println(pjp.getTarget()+"--"+pjp.getSignature());
        Object result=pjp.proceed();//执行目标对象方法
        System.out.println("环绕后置...");
        return result;
    } 
}

Aop matching method regular expression language (a brief understanding)

Introduction to Aop Pointcut Expressions

Execute any public method:

execution(public *(..))

Execute any set method

execution(* set*(..))

Execute any method of any class under the com.xxx.service package

execution(* com.xxx.service.*.*(..))

Execute any method of any class under the com.xxx.service package and sub-packages

execution(* com.xxx.service..*.*(..))

xml way

Configuration aspects, entry points, notifications

<!-- aop 相关配置 -->
<aop:config>
    <!-- aop 切面配置 -->
    <aop:aspect ref="logCut">
        <!-- 定义 aop 切入点 -->
        <aop:pointcut expression="execution (* com.xxx.service..*.*(..))" 
        id="cut"/>
        <!-- 配置前置通知 指定前置通知方法名 并引用切入点定义 -->
        <aop:before method="before" pointcut-ref="cut"/>
        <!-- 配置返回通知 指定返回通知方法名 并引用切入点定义 -->
        <aop:after-returning method="afterReturning" pointcut-ref="cut"/>
        <!-- 配置异常通知 指定异常通知方法名 并引用切入点定义 -->
        <aop:after-throwing method="afterThrowing" throwing="e" pointcut-ref="cut"/>
        <!-- 配置最终通知 指定最终通知方法名 并引用切入点定义 -->
        <aop:after method="after" pointcut-ref="cut"/>
        <!-- 配置环绕通知 指定环绕通知方法名 并引用切入点定义 -->
        <aop:around method="around" pointcut-ref="cut"/>
	</aop:aspect>
</aop:config>

Define bean

/**
* 声明切面组件
*/
@Component
public class LogCut {
    
    
    public void before(){
    
    
    	System.out.println("前置通知.....");
    }
    public void afterReturning(){
    
    
    	System.out.println("返回通知....");
    } 
    public void after(){
    
    
    	System.out.println("最终通知....");
    }
    public void afterThrowing(Exception e){
    
    
   	 System.out.println("异常通知....方法执行异常时执行:" + e);
    }
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
    
    
        System.out.println("环绕前置...");
        System.out.println("环绕通知");
        System.out.println(pjp.getTarget()+"--"+pjp.getSignature());
        Object result=pjp.proceed();
        System.out.println("环绕后置...");
        return result;
    }
}

Expand

Basic concepts of AOP

JoinPoint (connection point)【dynamic】

Every point that is intercepted, spring middle refers to every method that is intercepted, and a connection point of spring aop represents the execution of a method.

Pointcut (entry point) [static]

For the definition of interception of connection points (the matching rule definition specifies which methods are intercepted and which methods are processed), spring has a special expression language definition.

Advice {emphasis}

Intercept every connection point (each method) before and after the operation

  • Pre-notification (pre-enhancement) –before() notification before execution method
  • Return notification (return enhancement)-the notification after the afterReturning method ends normally and returns
  • Exception throw notification (Enhanced exception throw) –afetrThrow()
  • Final notification--after the notification will be executed regardless of whether the method is abnormal or not
  • Around advice-around a join point (join point) advice, such as method calls. This is the most powerful type of notification. Surround notifications can complete custom behaviors before and after method calls. It will also choose whether to continue executing the connection points or directly return their own return value or throw an exception to end the execution

Aspect

The combination of entry point and notification determines the definition of the aspect. The entry point defines which methods of which classes are to be intercepted. The notification defines what to do after the interception method. The aspect is an abstraction of cross-cutting concerns, similar to classes. Class is the abstraction of object characteristics, while the aspect is the abstraction of cross-cutting concerns.

Target

Target audience

Weave (woven into)

The process of applying the aspect to the target object and generating the proxy object is called weaving (process).

Introduction

Without modifying the original application code, the process of dynamically adding methods or fields to the class during the program runtime is called introduction.

Guess you like

Origin blog.csdn.net/dirft_lez/article/details/109031812