2021年7月15日星期四

十分钟全面理解Spring AOP

什么是AOP

AOP(Aspect-Oriented Programming,面向切面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善。OOP引入封装、继承和多态性等概念来建立一种对象层次结构,用以模拟公共行为的一个集合。当我们需要为分散的对象引入公共行为的时候,OOP则显得无能为力。也就是说,OOP允许你定义从上到下的关系,但并不适合定义从左到右的关系。例如日志功能。日志代码往往水平地散布在所有对象层次中,而与它所散布到的对象的核心功能毫无关系。对于其他类型的代码,如安全性、异常处理和透明的持续性也是如此。这种散布在各处的无关的代码被称为横切(cross-cutting)代码,在OOP设计中,它导致了大量代码的重复,而不利于各个模块的重用。

而AOP技术则恰恰相反,它利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其名为"Aspect",即切面。所谓"切面",简单地说,就是将那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块间的耦合度,并有利于未来的可操作性和可维护性。AOP代表的是一个横向的关系,如果说"对象"是一个空心的圆柱体,其中封装的是对象的属性和行为;那么面向方面编程的方法,就仿佛一把利刃,将这些空心圆柱体剖开,以获得其内部的消息。而剖开的切面,也就是所谓的"方面"了。然后它又以巧夺天功的妙手将这些剖开的切面复原,不留痕迹。

使用"横切"技术,AOP把软件系统分为两个部分:核心关注点和横切关注点。业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点。横切关注点的一个特点是,他们经常发生在核心关注点的多处,而各处都基本相似。比如权限认证、日志、事务处理。AOP的作用在于分离系统中的各种关注点,将核心关注点和横切关注点分离开来。正如Avanade公司的高级方案构架师Adam Magee所说,AOP的核心思想就是"将应用程序中的商业逻辑同对其提供支持的通用服务进行分离。"

实现AOP的技术,主要分为两大类:一是采用动态代理技术,利用拦截方法的方式,对该方法进行装饰,以取代原有对象行为的执行;二是采用静态织入的方式,引入特定的语法创建"切面",从而使得编译器可以在编译期间织入有关"切面"的代码。

AOP相关概念

为了更好的理解AOP,我们有必要先了解AOP的相关术语。

切面(Aspect)

横切关注点的模块化(跨越应用程序多个模块的功能,比如 日志功能),这个关注点实现可能另外横切多个对象。

连接点(Join point)

连接点是在应用执行过程中能够插入切面的一个点。这个点可以是类的某个方法调用前、调用后、方法抛出异常后等。切面代码可以利用这些点插入到应用的正常流程之中,并添加行为。

通知(Advice)

在特定的连接点,AOP框架执行的动作。

Spring AOP 提供了5种类型的通知:

  • 前置通知(Before):在目标方法被调用之前调用通知功能。
  • 后置通知(After):在目标方法完成之后调用通知,无论该方法是否发生异常。
  • 后置返回通知(After-returning):在目标方法成功执行之后调用通知。
  • 后置异常通知(After-throwing):在目标方法抛出异常后调用通知。
  • 环绕通知(Around):通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为。

切点(Pointcut)

指定一个通知将被引发的一系列连接点的集合。AOP 通过切点定位到特定的连接点。切点和连接点不是一对一的关系,一个切点匹配多个连接点,切点通过 org.springframework.aop.Pointcut 接口进行描述,它使用类和方法作为连接点的查询条件。每个类都拥有多个连接点,例如 ArithmethicCalculator类的所有方法实际上都是连接点。

引入(Introduction)

添加方法或字段到被通知的类。 Spring允许引入新的接口到任何被通知的对象。例如,你可以使用一个引入使任何对象实现 IsModified接口,来简化缓存。Spring中要使用Introduction, 可有通过DelegatingIntroductionInterceptor来实现通知,通过DefaultIntroductionAdvisor来配置Advice和代理类要实现的接口

目标对象(Target Object)

包含连接点的对象。也被称作被通知或被代理对象。

AOP代理(AOP Proxy)

AOP框架创建的对象,包含通知。 在Spring中,AOP代理可以是JDK动态代理或者CGLIB代理。

织入(Weaving)

织入描述的是把切面应用到目标对象来创建新的代理对象的过程。 Spring AOP 的切面是在运行时被织入,原理是使用了动态代理技术。Spring支持两种方式生成代理对象:JDK动态代理和CGLib,默认的策略是如果目标类是接口,则使用JDK动态代理技术,否则使用Cglib来生成代理。

Spring AOP使用

可以通过

1、基于

1、maven依赖


<properties> <spring.version>4.3.6.RELEASE</spring.version> <aspectj.version>1.8.8</aspectj.version> <java.version>1.7</java.version> <junit.version>4.12</junit.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies> <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-core</artifactId>  <version>${spring.version}</version> </dependency> <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-context</artifactId>  <version>${spring.version}</version> </dependency> <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-beans</artifactId>  <version>${spring.version}</version> </dependency> <!-- AOP --> <dependency>  <groupId>org.springframework</groupId>  <artifactId>spring-aop</artifactId>  <version>${spring.version}</version> </dependency> <dependency>  <groupId>org.aspectj</groupId>  <artifactId>aspectjrt</artifactId>  <version>${aspectj.version}</version> </dependency> <dependency>  <groupId>org.aspectj</groupId>  <artifactId>aspectjweaver</artifactId>  <version>${aspectj.version}</version>  <scope>runtime</scope> </dependency></dependencies>

2、定义目标对象

package com.bytebeats.spring4.aop..service;public interface UserService { String login(String username, String passowrd);  String register(String username, String passowrd);}

UserServiceImpl
package com.bytebeats.spring4.aop..service;import org.springframework.stereotype.Service;@Service("userService")public class UserServiceImpl implements UserService { @Override public String login(String username, String passowrd) {  System.out.println("login username:"+username+",passowrd:"+passowrd);  if(username==null){   throw new NullPointerException("username is null");  }  return "OK"; } @Override public String register(String username, String passowrd) {  System.out.println("register username:"+username+",passowrd:"+passowrd);  return "OK"; }}

3、定义切面

package com.bytebeats.spring4.aop.;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import java.util.Arrays;import java.util.List;public class LogInterceptor { /**  * 前置通知:在方法执行前执行的代码  * @param joinPoint  */ public void beforeExecute(JoinPoint joinPoint){    String methodName = joinPoint.getSignature().getName();  List<Object> args = Arrays.asList(joinPoint.getArgs());  System.out.println(this.getClass().getSimpleName()+" before execute"); }  /**  * 后置通知:在方法执行后执行的代码(无论该方法是否发生异常),注意后置通知拿不到执行的结果  * @param joinPoint  */ public void afterExecute(JoinPoint joinPoint){    String methodName = joinPoint.getSignature().getName();  System.out.println(this.getClass().getSimpleName()+" after execute"); }  /**  * 后置返回通知:在方法正常执行后执行的代码,可以获取到方法的返回值  * @param joinPoint  */ public void afterReturning(JoinPoint joinPoint, Object result){  String methodName = joinPoint.getSignature().getName();  System.out.println(this.getClass().getSimpleName()+" afterReturning execute, result:"+result); }  /**  * 后置异常通知:在方法抛出异常之后执行,可以访问到异常信息,且可以指定出现特定异常信息时执行代码  * @param joinPoint  */ public void afterThrowing(JoinPoint joinPoint, Exception /**NullPointerException*/ exception){  String methodName = joinPoint.getSignature().getName();  System.out.println(this.getClass().getSimpleName()+" afterThrowing execute, exception:"+exception); }  /**  * 环绕通知, 围绕着方法执行  */ public Object around(ProceedingJoinPoint joinPoint){    String methodName = joinPoint.getSignature().getName();  System.out.println(this.getClass().getSimpleName()+" around execute start");    Object result = null;  try {      //执行目标方法   result = joinPoint.proceed();  } catch (Throwable e) {   e.printStackTrace();  }  return result; }}

4、

spring-aop-

<?<beans ="http://www.springframework.org/schema/beans" ="http://www.w3.org/2001/" ="http://www.springframework.org/schema/context" ="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans    >"> <!-- 自动扫描的包 --> <context:component-scan base-package="com.bytebeats.spring4.aop."> </context:component-scan> <!-- 启用AspectJ自动代理 --> <aop:aspectj-autoproxy /> <!-- 配置 bean --> <bean id="userService"   class="com.bytebeats.spring4.aop."></bean> <!-- 配置切面 --> <bean id="logInterceptor"  class="com.bytebeats.spring4.aop."></bean> <!-- aop配置 --> <aop:config>  <!-- 配置切点表达式 -->  <aop:pointcut expression="execution(* com.bytebeats.spring4.aop."   id="pointcut"/>  <!-- 配置切面及通知 -->  <aop:aspect ref="logInterceptor" order="1">   <aop:before method="beforeExecute" pointcut-ref="pointcut"/>   <aop:after method="afterExecute" pointcut-ref="pointcut"/>   <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="exception"/>   <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/>   <aop:around method="around" pointcut-ref="pointcut"/>  </aop:aspect> </aop:config></beans>

 到此,基于

package com.bytebeats.spring4.aop.;import com.bytebeats.spring4.aop..service.UserService;import org.springframework.context.support.ClassPath;public class SpringAop{ public static void main(String[] args) {    ClassPath= new ClassPath("classpath:spring-aop-);  UserService userService = (UserService) ctx.getBean("userService");  //userService.login("ricky", "123");  System.out.println("***********************");  userService.login(null, "123456");  ctx.close(); }}

2、注解方式

1、定义目标对象

package com.bytebeats.spring4.aop.annotation.service;/** * ${DESCRIPTION} * * @author Ricky Fung * @create 2017-04-03 11:11 */public interface BankService { boolean transfer(String from, String to, int amount);}

BankServiceImpl
package com.bytebeats.spring4.aop.annotation.service;import org.springframework.stereotype.Service;/** * ${DESCRIPTION} * * @author Ricky Fung * @create 2017-04-03 11:12 */@Servicepublic class BankServiceImpl implements BankService { @Override public boolean transfer(String from, String to, int amount) {  if(amount<1){   throw new IllegalArgumentException("transfer amount must be a positive number");  }  System.out.println("["+from+"]向["+to+ "]转账金额"+amount);  return false; }}

 

2、定义切面

package com.bytebeats.spring4.aop.annotation;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;import java.util.Arrays;import java.util.List;@Order(1)@Aspect@Componentpublic class TransferLogAdvice { @Pointcut("execution(* com.bytebeats.spring4.aop.annotation.service.BankServiceImpl.*(..))") public void pointcut1() { } @Pointcut("execution(* com.bytebeats.spring4.aop.annotation.service.*ServiceImpl.*(..))") public void myPointcut() { } /**  * 前置通知:在方法执行前执行的代码  * @param joinPoint  */ @Before(value = "pointcut1() || myPointcut()") //@Before("execution(* com.bytebeats.spring4.aop.annotation.service.BankServiceImpl.*(..))") public void beforeExecute(JoinPoint joinPoint){    String methodName = joinPoint.getSignature().getName();  List<Object> args = Arrays.asList(joinPoint.getArgs());    System.out.println(this.getClass().getSimpleName()+ " before execute:"+methodName+ " begin with "+args); }  /**  * 后置通知:在方法执行后执行的代码(无论该方法是否发生异常),注意后置通知拿不到执行的结果  * @param joinPoint  */ @After(value = "pointcut1()") public void afterExecute(JoinPoint joinPoint){    String methodName = joinPoint.getSignature().getName();  System.out.println(this.getClass().getSimpleName()+ " after execute:"+methodName+" end!"); }  /**  * 后置返回通知:在方法正常执行后执行的代码,可以获取到方法的返回值  * @param joinPoint  */ @AfterReturning(value = "pointcut1()",   returning="result") public void afterReturning(JoinPoint joinPoint, Object result){    String methodName = joinPoint.getSignature().getName();  System.out.println(this.getClass().getSimpleName()+ " afterReturning execute:"+methodName+" end with result:"+result); }  /**  * 后置异常通知:在方法抛出异常之后执行,可以访问到异常信息,且可以指定出现特定异常信息时执行代码  * @param joinPoint  */ @AfterThrowing(value = "pointcut1()",   throwing="exception") public void afterThrowing(JoinPoint joinPoint, Exception /**NullPointerException*/ exception){    String methodName = joinPoint.getSignature().getName();  System.out.println(this.getClass().getSimpleName()+ " afterThrowing execute:"+methodName+" occurs exception:"+exception); }  /**  * 环绕通知, 围绕着方法执行  */ @Around(value = "pointcut1()") public Object around(ProceedingJoinPoint joinPoint){    String methodName = joinPoint.getSignature().getName();  System.out.println(this.getClass().getSimpleName()+ " around:"+methodName+" execute start");    Object result = null;  try {   result = joinPoint.proceed();  } catch (Throwable e) {   e.printStackTrace();  }  System.out.println(this.getClass().getSimpleName()+ " around:......

原文转载:http://www.shaoqun.com/a/879499.html

跨境电商:https://www.ikjzd.com/

文化衫事件:https://www.ikjzd.com/w/1932

急速:https://www.ikjzd.com/w/1861

转运中国:https://www.ikjzd.com/w/1549


什么是AOPAOP(Aspect-OrientedProgramming,面向切面编程),可以说是OOP(Object-OrientedPrograming,面向对象编程)的补充和完善。OOP引入封装、继承和多态性等概念来建立一种对象层次结构,用以模拟公共行为的一个集合。当我们需要为分散的对象引入公共行为的时候,OOP则显得无能为力。也就是说,OOP允许你定义从上到下的关系,但并不适合定义从左到右
虚拟信用卡:https://www.ikjzd.com/w/1055
2021开斋节是哪天,开斋节放假安排:http://www.30bags.com/a/425770.html
2021肯尼亚动物大迁徙时间,2021肯尼亚动物大迁徙是什么时候:http://www.30bags.com/a/425806.html
2021跨年东部华侨城杂技表演?东部华侨城跨年魔术秀?:http://www.30bags.com/a/432022.html
2021跨年好去处!在桂林漓江边倒数,到未开发的天坑探索神秘地心!【寻路记】:http://www.30bags.com/a/225406.html
口述:小姨子与姐夫之间那些不要脸的事儿(中)(5/5):http://lady.shaoqun.com/a/82893.html
男人撕开奶罩揉吮奶头 我都说疼了他还在继续:http://www.30bags.com/m/a/249804.html
老师慢慢张开腿坐上去 轻轻的顶开老师的两瓣:http://www.30bags.com/m/a/249729.html
动漫里被"单身"诅咒的剩下的女老师们!请赶快去救他们!:http://lady.shaoqun.com/a/423153.html
深圳欢乐田园毕业生有优惠吗:http://www.30bags.com/a/504643.html
年轻人经常做"这件事",可能会给自己带来四大危害:http://lady.shaoqun.com/a/423154.html
这名美国女教师与青少年拘留中心的囚犯发生性关系,被判处7年监禁:http://lady.shaoqun.com/a/423155.html