怎么建设彩票网站,html网站的规划与建设,武陟做网站,wordpress 耗时本篇内容包括#xff1a;Spring AOP 概述#xff08;AOP 简介、AOP 为什么叫面向切面编程、AOP 主要用来解决的问题 和 AOP 的相关术语#xff09;、Spring AOP Demo#xff08;xml 方式、注解方式#xff09;以及相关知识点#xff08;JDK 动态代理和 CGLIB 代理、Sprin… 本篇内容包括Spring AOP 概述AOP 简介、AOP 为什么叫面向切面编程、AOP 主要用来解决的问题 和 AOP 的相关术语、Spring AOP Demoxml 方式、注解方式以及相关知识点JDK 动态代理和 CGLIB 代理、Spring AOP 和 AspectJ AOP、Aspect、Pointcut、Around 注解等内容 一、Spring AOP 概述
1、AOP 简介
AOPAspect oriented programming即面向切面编程它是一个编程范式是 OOP面向对象编程的一种延续目的就是提高代码的模块性。
Spring AOP 基于动态代理的方式实现如果是实现了接口的话就会使用 JDK 动态代理反之则使用 CGLIB 代理Spring中 AOP 的应用主要体现在 事务、日志、异常处理等方面通过在代码的前后做一些增强处理可以实现对业务逻辑的隔离提高代码的模块化能力同时也是解耦。Spring主要提供了 Aspect 切面、JoinPoint 连接点、PointCut 切入点、Advice 增强等实现方式。
2、AOP 为什么叫面向切面编程
切 指的是横切逻辑原有业务逻辑代码不动只能操作横切逻辑代码所以面向横切逻辑
面 横切逻辑代码往往要影响的是很多个方法每个方法如同一个点多个点构成一个面。这里有一个面的概念
3、AOP 主要用来解决的问题
例如现有三个类Horse、Pig、Dog这三个类中都有 eat 和 run 两个方法。
通过 OOP 思想中的继承我们可以提取出一个 Animal 的父类然后将 eat 和 run 方法放入父类中Horse、Pig、Dog通过继承Animal类即可自动获得 eat() 和 run() 方法。这样将会少些很多重复的代码。
OOP 编程思想可以解决大部分的代码重复问题。但是有一些问题是处理不了的。比如在父类 Animal 中的多个方法的相同位置出现了重复的代码OOP 就解决不了。这部分重复的代码一般统称为横切逻辑代码。
横切逻辑代码存在的问题
代码重复问题横切逻辑代码和业务代码混杂在一起代码臃肿不变维护
AOP 就是用来解决这些问题的AOP 另辟蹊径提出横向抽取机制将横切逻辑代码和业务逻辑代码分离代码拆分比较容易难的是如何在不改变原有业务逻辑的情况下悄无声息的将横向逻辑代码应用到原有的业务逻辑中达到和原来一样的效果
AOP 主要用来解决在不改变原有业务逻辑的情况下增强横切逻辑代码根本上解耦合避免横切逻辑代码重复。
4、AOP 的相关术语
连接点Joinpoint所谓连接点是指那些被拦截到的点。在spring中这些点指的是方法因为spring只支持方法类型的连接点。切入点Pointcut切入点是指我们要对哪些连接点Joinpoint进行拦截通知/增强Advice所谓通知是指拦截到Joinpoint之后要做的事情通知分为前置通知后置通知异常通知最终通知环绕通知切面要完成的功能。织入Weaving是指把增强应用到目标对象来创建新的代理对象的过程。spring采用动态代理织入而AspectJ采用编译期织入和类装载期织入。切面Aspect是切入点和通知引介的结合代理Proxy一个类被AOP织入增强后就产生了一个结果代理类目标对象Target代理的目标对象 二、Spring AOP Demo
1、xml配置方式
# 引入依赖 !-- 模块构建在 spring-core 和 spring-Beans 之上。它继承了 Bean 模块的特性并添加了对国际化、事件传播、资源加载透明化的支持 --dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.3.22/version/dependency# 配置 xml 信息
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.2.xsdbean idaopTank classdesignpattern.aop.v1.AopTank/bean idaopMethod classdesignpattern.aop.v1.AopMethod/aop:configaop:aspect idtime refaopMethodaop:pointcut idonmove expressionexecution(public void com.liziheng.demo.api.aop.demo.AopDog.*(..))/aop:before methodbefore pointcut-refonmove/aop:after methodafter pointcut-refonmove//aop:aspect/aop:config/beans# 切入时添加方法
public class AopMethod {public void before() {System.out.println(before...);}public void after() {System.out.println(after...);}
}# 被切入的类
public class AopDog {public void eat() {System.out.println(The dog is eating...);}public void drink() {System.out.println(The dog is drinking water...);}}# 测试
public class Main {public static void main(String[] args) {ApplicationContext context new ClassPathXmlApplicationContext(app.xml);AopDog dog (AopDog) context.getBean(AopDog);tank.move();tank.voice();}
}2、注解方式
# 引入依赖 dependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion1.9.4/version/dependency# xml配置同上
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.2.xsdaop:aspectj-autoproxy/bean idaopTank classdesignpattern.aop.v1.AopTank/bean idaopMethod classdesignpattern.aop.v1.AopMethod/
/beans# 切入时添加方法
public class AopMethod {Before(execution(public void com.liziheng.demo.api.aop.demo.AopDog.*(..)))public void before() {System.out.println(before...);}After(execution(public void com.liziheng.demo.api.aop.demo.AopDog.*(..)))public void after() {System.out.println(after...);}
}# 被切入的类 同xml方式
# 测试 同xml方式 三、相关知识点
1、JDK 动态代理和 CGLIB 代理
JDK 动态代理主要是针对类实现了某个接口AOP 则会使用 JDK 动态代理。他基于反射的机制实现生成一个实现同样接口的一个代理类然后通过重写方法的方式实现对代码的增强
而如果某个类没有实现接口AOP 则会使用 CGLIB 代理。他的底层原理是基于 asm 第三方框架通过修改字节码生成成成一个子类然后重写父类的方法实现对代码的增强。
2、Spring AOP 和 AspectJ AOP
Spring AOP 基于动态代理实现属于运行时增强。
AspectJ 则属于编译时增强主要有3种方式
编译时织入指的是增强的代码和源代码我们都有直接使用 AspectJ 编译器编译就行了编译之后生成一个新的类他也会作为一个正常的 Java 类装载到 JVM编译后织入指的是代码已经被编译成 class 文件或者已经打成 jar 包这时候要增强的话就是编译后织入比如你依赖了第三方的类库又想对他增强的话就可以通过这种方式加载时织入指的是在 JVM 加载类的时候进行织入。
总结下来的话就是 Spring AOP 只能在运行时织入不需要单独编译性能相比 AspectJ 编译织入的方式慢而 AspectJ 只支持编译前后和类加载时织入性能更好功能更加强大。
3、Aspect、Pointcut、Around 注解
Pointcut表示一个切入点value表示切入点的作用范围Aspect 表示申明一个切面 Around环绕增强方法正常之前的前后调用Before前置增强方法执行前调用After后置 final 增强不管方法正常退出还是一场都会执行AfterReturning后置增强方法正常退出时执行AfterThowing异常抛出增强方法抛异常时执行