大数据平台网站建设,高端网站开发程,百度app下载安装,amh安装wordpress学习材料 
尚硅谷Spring零基础入门到进阶#xff0c;一套搞定spring6全套视频教程#xff08;源码级讲解#xff09; 
AOP AOP#xff08;Aspect Oriented Programming#xff09;是一种设计思想#xff0c;是软件设计领域中的面向切面编程#xff0c;它是面向对象编程的…学习材料 
尚硅谷Spring零基础入门到进阶一套搞定spring6全套视频教程源码级讲解 
AOP AOPAspect Oriented Programming是一种设计思想是软件设计领域中的面向切面编程它是面向对象编程的一种补充和完善它以通过预编译方式和运行期动态代理方式实现在不修改源代码的情况下给程序动态统一添加额外功能的一种技术。利用AOP可以对业务逻辑的各个部分进行隔离从而使得业务逻辑各部分之间的耦合度降低提高程序的可重用性同时提高了开发的效率。 需要了解相关的术语 
关注横切面分散在每个各个模块中解决同一样的问题如用户验证、日志管理、事务处理、数据缓存都属于横切关注点。通知or增强 切面 目标被代理的目标方法对象代理代理对象连接点 切入点 定位连接点的方式。 每个类的方法中都包含多个连接点所以连接点是类中客观存在的事物从逻辑上来说。 如果把连接点看作数据库中的记录那么切入点就是查询记录的 SQL 语句。 Spring 的 AOP 技术可以通过切入点定位到特定的连接点。通俗说要实际去增强的方法 切点通过 org.springframework.aop.Pointcut 接口进行描述它使用类和方法作为连接点的查询条件。 AOP的作用 简化代码把方法中固定位置的重复的代码抽取出来让被抽取的方法更专注于自己的核心功能提高内聚性。  代码增强把特定的功能封装到切面类中看哪里有需要就往上套被套用了切面逻辑的方法就被切面给增强了。  
基于注解实现AOP 
基于动态代理实现的而动态代理分为JDK和cglibjdk是在目标是实现类的情况下使用。 具体内容还是要认真看课件不是特别明白  
动手尝试 
引入依赖pom.xml !--spring aop依赖--dependencygroupIdorg.springframework/groupIdartifactIdspring-aop/artifactIdversion6.0.2/version/dependency!--spring aspects依赖--dependencygroupIdorg.springframework/groupIdartifactIdspring-aspects/artifactIdversion6.0.2/version/dependencybean.xml配置 !--基于注解的AOP的实现1、将目标对象和切面交给IOC容器管理注解扫描2、开启AspectJ的自动代理为目标对象自动生成代理3、将切面类通过注解Aspect标识--context:component-scan base-packagecom.atguigu.aop.annotation/context:component-scanaop:aspectj-autoproxy /建立的环境就是很简单一个Calculator接口并实现加减乘除。我需要在每个实现方法的前后进行日志的输出这样就需要解耦出来。 如何编写切面类最主要的是写切入点表达式 // Aspect表示这个类是一个切面类
Aspect
// Component注解保证这个切面类能够放入IOC容器
Component
public class LogAspect {Before(execution(public int com.atguigu.aop.annotation.CalculatorImpl.*(..)))public void beforeMethod(JoinPoint joinPoint){String methodName  joinPoint.getSignature().getName();String args  Arrays.toString(joinPoint.getArgs());System.out.println(Logger--前置通知方法名methodName参数args);}After(execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..)))public void afterMethod(JoinPoint joinPoint){String methodName  joinPoint.getSignature().getName();System.out.println(Logger--后置通知方法名methodName);}AfterReturning(value  execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..)), returning  result)public void afterReturningMethod(JoinPoint joinPoint, Object result){String methodName  joinPoint.getSignature().getName();System.out.println(Logger--返回通知方法名methodName结果result);}AfterThrowing(value  execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..)), throwing  ex)public void afterThrowingMethod(JoinPoint joinPoint, Throwable ex){String methodName  joinPoint.getSignature().getName();System.out.println(Logger--异常通知方法名methodName异常ex);}Around(execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..)))public Object aroundMethod(ProceedingJoinPoint joinPoint){String methodName  joinPoint.getSignature().getName();String args  Arrays.toString(joinPoint.getArgs());Object result  null;try {System.out.println(环绕通知--目标对象方法执行之前);//目标对象连接点方法的执行result  joinPoint.proceed();System.out.println(环绕通知--目标对象方法返回值之后);} catch (Throwable throwable) {throwable.printStackTrace();System.out.println(环绕通知--目标对象方法出现异常时);} finally {System.out.println(环绕通知--目标对象方法执行完毕);}return result;}}然后正常的调用想要调用的类就行。 
基于xml实现AOP 
aop:config!--配置切面类--aop:aspect refloggerAspectaop:pointcut idpointCut expressionexecution(* com.atguigu.aop.xml.CalculatorImpl.*(..))/aop:before methodbeforeMethod pointcut-refpointCut/aop:beforeaop:after methodafterMethod pointcut-refpointCut/aop:afteraop:after-returning methodafterReturningMethod returningresult pointcut-refpointCut/aop:after-returningaop:after-throwing methodafterThrowingMethod throwingex pointcut-refpointCut/aop:after-throwingaop:around methodaroundMethod pointcut-refpointCut/aop:around/aop:aspect
/aop:config