什么是aop

AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

aop在spring的作用

提供声明式事务;允许用户自定义切面

  • 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 ....

  • 切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。

  • 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。

  • 目标(Target):被通知对象。

  • 代理(Proxy):向目标对象应用通知之后创建的对象。

  • 切入点(PointCut):切面通知 执行的 “地点”的定义。

  • 连接点(JointPoint):与切入点匹配的执行点。

即 Aop 在 不改变原有代码的情况下 , 去增加新的功能 .

使用spring实现aop

第一种方式:通过api实现接口实现

1.首先创建业务接口和实现类(service和impl)

2.然后去写我们的增强类 , 我们编写两个 , 一个前置增强 一个后置增强

public class BeforeLog implements MethodBeforeAdvice {

   //method : 要执行的目标对象的方法
   //objects : 被调用的方法的参数
   //Object : 目标对象
   @Override
   public void before(Method method, Object[] objects, Object o) throws Throwable {
       System.out.println( o.getClass().getName() + "的" + method.getName() + "方法被执行了");
  }
}
public class AfterLog implements AfterReturningAdvice {
   //returnValue 返回值
   //method被调用的方法
   //args 被调用的方法的对象的参数
   //target 被调用的目标对象
   @Override
   public void afterReturning(Object returnValue, Method method, Object[] args,Object target) throws Throwable {
       System.out.println("执行了" + target.getClass().getName()
       +"的"+method.getName()+"方法,"
       +"返回值:"+returnValue);
  }
}

3.去spring的xml文件中注测,并实现aop切入
   <!--注册bean-->
   <bean id="userService" class="com.kuang.service.UserServiceImpl"/>
   <bean id="log" class="com.kuang.log.Log"/>
   <bean id="afterLog" class="com.kuang.log.AfterLog"/>

   <!--aop的配置-->
   <aop:config>
       <!--切入点 expression:表达式匹配要执行的方法-->
       <aop:pointcut id="pointcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
       <!--执行环绕; advice-ref执行方法 . pointcut-ref切入点-->
       <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
       <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
   </aop:config>

理解:Spring的Aop就是将公共的业务 (日志 , 安全等) 和领域业务结合起来 , 当执行领域业务时 , 将会把公共业务加进来 . 实现公共业务的重复利用 . 领域业务更纯粹 , 程序猿专注领域业务 , 其本质还是动态代理 .
第二种方式:自定义类来实现aop(常用)
1.写service和impl
2.写一个自定义的切入类
public class DiyPointcut {

   public void before(){
       System.out.println("---------方法执行前---------");
  }
   public void after(){
       System.out.println("---------方法执行后---------");
  }
   
}
2.去spring中配置
<!--第二种方式自定义实现-->
<!--注册bean-->
<bean id="diy" class="com.kuang.config.DiyPointcut"/>

<!--aop的配置-->
<aop:config>
   <!--第二种方式:使用AOP的标签实现-->
   <aop:aspect ref="diy">
       <aop:pointcut id="diyPonitcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
       <aop:before pointcut-ref="diyPonitcut" method="before"/>
       <aop:after pointcut-ref="diyPonitcut" method="after"/>
   </aop:aspect>
</aop:config>

理解:一个aop切面代表一个类,而类里的方法等于通知

第三种方式:使用注解实现

1.编写一个注解实现的增强类
@Aspect
public class AnnotationPointcut {
   @Before("execution(* com.kuang.service.UserServiceImpl.*(..))")
   public void before(){
       System.out.println("---------方法执行前---------");
  }

   @After("execution(* com.kuang.service.UserServiceImpl.*(..))")
   public void after(){
       System.out.println("---------方法执行后---------");
  }

   @Around("execution(* com.kuang.service.UserServiceImpl.*(..))")
   public void around(ProceedingJoinPoint jp) throws Throwable {
       System.out.println("环绕前");
       System.out.println("签名:"+jp.getSignature());
       //执行目标方法proceed
       Object proceed = jp.proceed();
       System.out.println("环绕后");
       System.out.println(proceed);
  }
}
 
2.在spring中注册bean
<!--第三种方式:注解实现-->
<bean id="annotationPointcut" class="com.kuang.config.AnnotationPointcut"/>
<aop:aspectj-autoproxy/>

了解
通过aop命名空间的<aop:aspectj-autoproxy />声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。
当然,spring 在内部依旧采用AnnotationAwareAspectJAutoProxyCreator进行自动代理的创建工作,但具体实现的细节已经被<aop:aspectj-autoproxy />隐藏起来了

<aop:aspectj-autoproxy />有一个proxy-target-class属性,默认为false,表示使用jdk动态代理织入增强,当配为<aop:aspectj-autoproxy  poxy-target-class="true"/>时,
表示使用CGLib动态代理技术织入增强。不过即使proxy-target-class设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。




 
 

Aop的学习与使用的更多相关文章

  1. spring AOP的学习

    1.Spring常用的概念 Joinpoint(连接点): 所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点. Pointcut(切入点): ...

  2. Spring AOP体系学习总结:

    二.Spring AOP体系学习总结: 要理解AOP整体的逻辑需要理解一下Advice,Pointcut,Advisor的概念以及他们的关系. Advice是为Spring Bean提供增强逻辑的接口 ...

  3. Spring Annotation注解进行aop的学习

    使用Maven管理项目,pom文件为: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...

  4. Spring框架-AOP详细学习[转载]

    参考博客:https://blog.csdn.net/qq_22583741/article/details/79589910#4-%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85% ...

  5. Spring AOP体系学习总结

    要理解AOP整体的逻辑需要理解一下Advice,Pointcut,Advisor的概念以及他们的关系.  Advice是为Spring Bean提供增强逻辑的接口,提供了多种方法增强的方式,比如前置, ...

  6. 笔记-spring aop 原理学习2

    InstantiationAwareBeanPostProcessor AnnotationAwareAspectJAutoProxyCreator https://blog.csdn.net/qq_ ...

  7. spring aop 原理学习

    @EnableAspectJAutoProxy: @Import(AspectJAutoProxyRegistrar.class) 实际是创建了一个以org.springframework.aop.c ...

  8. Spring中的AOP(学习笔记)

    是什么AOP及实现方式 AOP的基本概念 Schema-base AOP Spring AOP API AspectJ

  9. 关于spring AOP的学习

    比较好的帖子http://www.cnblogs.com/xing901022/p/4265544.html

随机推荐

  1. ondyari / FaceForensics配置指南

    https://github.com/ondyari/FaceForensics 安装配置方法: $ git clone https://github.com/ondyari/FaceForensic ...

  2. Java多线程_ThreadLocal

    用法:ThreadLocal用于保存某个线程共享变量:对于同一个static ThreadLocal,不同线程只能从中get,set,remove自己的变量,而不会影响其他线程的变量. ThreadL ...

  3. source insight编辑matlab,不可

    先说结论,我用的source insight版本是4.0x,matlab的语言包用的sourceinght官网提供的matlab.clf.链接如下:https://www.sourceinsight. ...

  4. js对象数组新增、修改时的验证是否重复的逻辑

    JS代码: // 定义数据集合 const persons = [ { id: 1, name: '张三' }, { id: 2, name: '李四' } ] console.log('') con ...

  5. /usr/bin/ld: cannot find -lcrypto

    当我们使用openssl里边的函数的时候,需要链接crypto的库 如果找不到,加一个软链接,如下: ln -s /usr/lib64/libcrypto.so.1.1 /usr/lib64/libc ...

  6. python练习 数字不同数之和+人名最多数统计

    数字不同数之和 描述 获得用户输入的一个整数N,输出N中所出现不同数字的和.‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬ ...

  7. 23种设计模式(C++)

    每一种都有对应理解的相关代码示例 → Git原码 一. GOF-23 模式分类 从目的来看 • 创建型(Creational)模式:将对象的部分创建工作延迟到子类或者其他对象,从而应对需求变化为对象创 ...

  8. e3mall商城的归纳总结2之认识dubbo、zookeeper

    由于本项目用的是soa架构,因此必须需要两个系统之间进行通信,目前的解决办法有三种(本人认为) Webservice:效率不高基于soap协议.项目中不推荐使用. 使用restful形式的服务:htt ...

  9. PostgreSQL在不同的表空间移动数据文件

    一.背景 在工作中,可能会遇到将表从一个表空间移动另一个表空间.例如 * 对数据进行冷处理 * 表空间所在的磁盘空间不足 * 建表时分配错了表空间 以上等等,可能需要你将一个表移动表空间. 二.表空间 ...

  10. 使用 Visual Studio 2019 批量添加代码文件头

    应用场景介绍 在我们使用一些开源项目时,基本上都会在每个源代码文件的头部看到一段版权声明.一个项目或解决方案中源代码文件的个数少则几十,多则几千甚至更多,那么怎么才能给这么多文件方便地批量添加或者修改 ...