个人网站上传有啥要求,山东网站建设平台,榜单优化,建设和住房保障部 网站目录 什么是AOP#xff1f;AOP组成Spring AOP 实现步骤Spring AOP实现原理JDK Proxy VS CGLIB 什么是AOP#xff1f; AOP#xff08;Aspect Oriented Programming#xff09;#xff1a;⾯向切⾯编程#xff0c;它是⼀种思想#xff0c;它是对某⼀类事情的集中处理。⽐如… 目录 什么是AOPAOP组成Spring AOP 实现步骤Spring AOP实现原理JDK Proxy VS CGLIB  什么是AOP AOPAspect Oriented Programming⾯向切⾯编程它是⼀种思想它是对某⼀类事情的集中处理。⽐如⽤户登录权限的效验没学 AOP 之前我们所有需要判断⽤户登录的⻚⾯中的⽅法都要各⾃实现或调⽤⽤户验证的⽅法然⽽有了 AOP 之后我们只需要在某⼀处配置⼀下所有需要判断⽤户登录⻚⾯中的⽅法就全部可以实现⽤户登录验证了不再需要每个⽅法中都写相同的⽤户登录验证了。 AOP组成 
切面(Aspect)定义的是事件(AOP是啥的)。ex用户登录校验切点(Pointcut)定义具体规则。ex定义用户登录拦截规则哪些接口判断用户登录权限哪些不判断。通知(Advice)AOP执行的具体方法。ex获取用户登录信息如果获取到说明已经登录否则未登录。 前置通知 后置通知 环绕通知 返回通知连接点(Join Point)有可能触发切点的所有点。ex所有接口 
Spring AOP 实现步骤 
1.添加Spring AOP依赖 
!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-bo
ot-starter-aop --
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId
/dependency2.定义切面。 
package com.example.demo.common;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;Aspect // 定义切面
Component
public class UserAspect {  
} 
3.定义切点。 
package com.example.demo.common;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;Aspect // 定义切面
Component
public class UserAspect {// 切点Pointcut(execution(* com.example.demo.controller.UserController.*(..)))public void pointcut() {}
} 
4.执行通知。 
package com.example.demo.common;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;Aspect // 定义切面
Component
public class UserAspect {// 切点Pointcut(execution(* com.example.demo.controller.UserController.*(..)))public void pointcut() {}// 通知Before(pointcut())public void doBefore() {System.out.println(执行了前置通知);}
} 
package com.example.demo.common;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;Aspect // 定义切面
Component
public class UserAspect {// 切点Pointcut(execution(* com.example.demo.controller.UserController.*(..)))public void pointcut() {}// 通知Before(pointcut())public void doBefore() {System.out.println(执行了前置通知);}// 后置通知After(pointcut())public void doAfter() {System.out.println(执行了后置方法);}// 环绕通知Around(pointcut())public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println(环绕通知执行之前);// 执行目标方法Object result  joinPoint.proceed();System.out.println(环绕通知执行之后);return result;}
} Spring AOP实现原理 
Spring AOP 是构建在动态代理基础上因此 Spring 对 AOP 的⽀持局限于⽅法级别的拦截。 Spring 动态代理组成 1.JDK Proxy 代理对象必须实现接口才能使用JDK Proxy 。 2.CGLIB 通过实现代理类的子类来实现动态代理。 
JDK Proxy VS CGLIB 
1.出生不同。 2.实现不同JDK Proxy要求代理类实现接口才能实现代理 CGLIB 是通过实现子类完成动态代理。 3.性能不同JDK 7 JDK Proxy性能是略高于CGLIB JDK 7之前 CGLIB 性能远远高于JDK Proxy。