AOP

  • 描述(摘抄百度百科)
  • AOP开发中的相关操作术语
  • 实例(xml方式)
    • 通知的类型
    • 切入点表达式的写法:
  • 实例(注解方式)

描述(摘抄百度百科)

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

AOP开发中的相关操作术语

Joinpoint(连接点):所谓连接点是指那些被拦截到的点。其实就是类里面可以被增强的方法,这些方法称为连接点。
  Pointcut(切入点):所谓切入点是指我们要对哪些 Joinpoint 进行拦截的定义。比如实际开发中只对saveAccount()方法增强,该方法就是切入点。
  Advice(通知/增强):增强的逻辑,称为增强或通知
  Aspect(切面):把增强应用到切入点上面的过程称为切面。
  Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction 可以在运行期为类动态地添加一些方法或 Field。
  Target(目标对象):代理的目标对象。
  Proxy(代理):代理对象
  Weaving(织入):将通知应用到目标的过程。例如:将日志记录应用到saveAccount()方法上面。
下图更直观:

实例(xml方式)

创建一个meavn工程,pom文件中添加如下依赖:

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>

  

AccountService 接口:

public interface AccountService {

    /***
* 保存账户
*/
void saveAccount(); /***
* 更新账户
*/
void updateAccount(Integer i); /***
* 删除账户
*/
void deleteAccount(Integer i);
}

  

AccountServiceImpl实现类:

public class AccountServiceImpl implements AccountService {
public void saveAccount() {
System.out.println("保存用户成功.....");
} public void updateAccount(Integer i) {
System.out.println("更新用户成功.....");
} public void deleteAccount(Integer i) {
System.out.println("删除用户成功.....");
}
}

  

再编写一个增强的类Logger:

public class Logger {
/***
* @Description: 用于打印日志
* @Date: 2021/2/9
*/
public void Printlog(){
System.out.println("输出打印日志.....");
}
}

  

bean.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--配置service-->
<bean id="accountService" class="com.zy.aop.service.AccountServiceImpl"></bean> <!--配置切面logger类-->
<bean id="logger" class="com.zy.aop.logger.Logger"></bean> <!--配置Aop-->
<aop:config>
<!--配置切入点表达式-->
<aop:pointcut id="log" expression="execution(public void com.zy.aop.service.AccountServiceImpl.saveAccount())"/>
<!--配置切面-->
<aop:aspect id="" ref="logger">
<!--前置通知:在切入点方法执行前执行-->
<aop:before method="Printlog" pointcut-ref="log"></aop:before>
</aop:aspect>
</aop:config>
</beans>

  

测试代码:

public class TestAop {
@Test
public void TestSaveAccount(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
AccountService accountService = (AccountService) context.getBean("accountService");
accountService.saveAccount();
accountService.updateAccount(1);
Integer i = 10;
}
}

  

运行结果:

标签说明:

标签 说明
aop:config 该标签声明aop配置开始
aop:pointcut 用于配置切入点表达式。就是指定对哪些类的哪些方法进行增强。属性:expression:用于定义切入点表达式。id:用于给切入点表达式提供一个唯一标识
aop:aspect 用于配置切面。属性:id:给切面提供一个唯一标识。ref:引用配置好的通知类 bean 的 id

通知的类型

通知类型 标签
前置通知 <aop:before method="" pointcut-ref=""></aop:before>
后置通知 <aop:after-returning method="" pointcut-ref=""></aop:after-returning>
异常通知 <aop:after-throwing method=""></aop:after-throwing>
最终通知 <aop:after method="" pointcut-ref=""></aop:after>
环绕通知 <aop:around method="" pointcut-ref=""></aop:around>

每一个标签中都有这三个属性method:指定通知中方法的名称。pointct:定义切入点表达式pointcut-ref:指定切入点表达式的引用。
前置通知:

<aop:config>
<aop:pointcut id="log" expression="execution(* com.zy.aop.service.AccountServiceImpl.saveAccount())"/>
<aop:aspect id="" ref="logger">
<!--前置通知:在切入点方法执行前执行-->
<aop:before method="Printlog" pointcut-ref="log"></aop:before>
</aop:aspect>
</aop:config>

  

后置通知:

<aop:config>
<aop:pointcut id="log" expression="execution(* com.zy.aop.service.AccountServiceImpl.saveAccount())"/>
<aop:aspect id="" ref="logger">
<!--后置通知:在切入点方法执行后执行-->
<aop:after-returning method="Printlog" pointcut-ref="log"></aop:after-returning>
</aop:aspect>
</aop:config>

  

异常通知:

<aop:config>
<aop:pointcut id="log" expression="execution(* com.zy.aop.service.AccountServiceImpl.saveAccount())"/>
<aop:aspect id="" ref="logger">
<!--异常通知:程序出现异常的时候,进行的操作-->
<aop:after-throwing method="Printlog" pointcut-ref="log"></aop:after-throwing>
</aop:aspect>
</aop:config>

  

最终通知:

<aop:config>
<aop:pointcut id="log" expression="execution(* com.zy.aop.service.AccountServiceImpl.saveAccount())"/>
<aop:aspect id="" ref="logger">
<!--最终通知:在切入点方法执行后执行,无论方法是否正常执行都会在后面执行-->
<aop:after method="Printlog" pointcut-ref="log"></aop:after>
</aop:aspect>
</aop:config>

  

环绕通知:

<aop:config>
<aop:pointcut id="log" expression="execution(* com.zy.aop.service.AccountServiceImpl.saveAccount())"/>
<aop:aspect id="" ref="logger">
<!--环绕通知-->
<aop:around method="Printlog2" pointcut-ref="log"></aop:around>
</aop:aspect>
</aop:config>

  

我们要修改一下增强的类Logger中的方法:

public class Logger {
/***
* @Description: 用于打印日志
* @Date: 2021/2/9
*/
public void Printlog() {
System.out.println("输出打印日志.....");
}
public Object Printlog2(ProceedingJoinPoint pro) {
Object proceed = null;
Object[] args = pro.getArgs();//得到方法实行所需参数
try {
//在这之前是前置通知
proceed = pro.proceed(args);//调用业务层方法
//在这之后是后置通知
return proceed;
} catch (Throwable t) {
//在这是异常通知
} finally {
//在这是最终通知
}
return proceed;
}
}

  

切入点表达式的写法:

表达式语法:execution([修饰符] 返回值类型 包名.类名.方法名(参数))
写法说明:
全匹配方式:public void com.zy.aop.service.AccountServiceImpl.saveAccount(参数类型)
访问修饰符可以省略: void com.zy.aop.service.AccountServiceImpl.saveAccount(参数类型)
返回值可以使用号,表示任意返回值: com.zy.aop.service.AccountServiceImpl.saveAccount(参数类型),具体可以百度在这我就不写了。
通常情况下,我们都是对业务层的方法进行增强,所以切入点表达式都是切到业务层实现类。
execution(* com.zy.aop.service..(…))

Spring中AOP学习笔记的更多相关文章

  1. spring 中bean学习笔记

    spring 中bean 一.bean的定义和应用 1. bean 形象上类似于getXX()和setXX()的一种. 2. 由于java是面向对象的,类的方法和属性在使用中需要实例化. 3. 规律: ...

  2. Spring中AOP原理,源码学习笔记

    一.AOP(面向切面编程):通过预编译和运行期动态代理的方式在不改变代码的情况下给程序动态的添加一些功能.利用AOP可以对应用程序的各个部分进行隔离,在Spring中AOP主要用来分离业务逻辑和系统级 ...

  3. Spring入门IOC和AOP学习笔记

    Spring入门IOC和AOP学习笔记 概述 Spring框架的核心有两个: Spring容器作为超级大工厂,负责管理.创建所有的Java对象,这些Java对象被称为Bean. Spring容器管理容 ...

  4. Spring 源码学习笔记10——Spring AOP

    Spring 源码学习笔记10--Spring AOP 参考书籍<Spring技术内幕>Spring AOP的实现章节 书有点老,但是里面一些概念还是总结比较到位 源码基于Spring-a ...

  5. 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)

    一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...

  6. Spring 源码学习笔记11——Spring事务

    Spring 源码学习笔记11--Spring事务 Spring事务是基于Spring Aop的扩展 AOP的知识参见<Spring 源码学习笔记10--Spring AOP> 图片参考了 ...

  7. Spring源码学习笔记12——总结篇,IOC,Bean的生命周期,三大扩展点

    Spring源码学习笔记12--总结篇,IOC,Bean的生命周期,三大扩展点 参考了Spring 官网文档 https://docs.spring.io/spring-framework/docs/ ...

  8. Spring中AOP简介与切面编程的使用

    Spring中AOP简介与使用 什么是AOP? Aspect Oriented Programming(AOP),多译作 "面向切面编程",也就是说,对一段程序,从侧面插入,进行操 ...

  9. spring cloud(学习笔记)高可用注册中心(Eureka)的实现(二)

    绪论 前几天我用一种方式实现了spring cloud的高可用,达到两个注册中心,详情见spring cloud(学习笔记)高可用注册中心(Eureka)的实现(一),今天我意外发现,注册中心可以无限 ...

随机推荐

  1. 全球城市ZoneId和UTC时间偏移量的最全对照表

    前言 你好,我是A哥(YourBatman). 如你所知,现行的世界标准时间是UTC世界协调时,时区已不直接参与时间计算.但是呢,城市名称or时区是人们所能记忆和容易沟通的名词,因此我们迫切需要一个对 ...

  2. 聊聊.net应用程序的Docker镜像

    ​要在容器中运行.net应用程序,你需要在容器镜像中安装.net Framework或.net Core 运行时.这不是你需要自己管理的东西,因为微软提供的Docker镜像已经安装了运行时,你可以使用 ...

  3. 大促密集,CDN如何保障电商体验如丝般顺滑?

    简介: 前不久,阿里云技术天团空降CSDN在线峰会,对核心技术竞争力进行解读.其中,阿里云高级技术专家曾福华分享了<双11: CDN如何保障电商大促如丝般顺滑>的议题.俗话说:养兵千日,用 ...

  4. error Unexpected use of comma operator no-sequences解决过程

    error Unexpected use of comma operator no-sequences解决过程 报错内容: ERROR in ./pages/course/_id.vue friend ...

  5. In Search of an Understandable Consensus Algorithm" (https://raft.github.io/raft.pdf) by Diego Ongaro and John Ousterhout.

    In Search of an Understandable Consensus Algorithm" (https://raft.github.io/raft.pdf) by Diego ...

  6. LeetCode上并发题目无Go版本:台湾同胞试水 — 交替打印FooBar

    https://mp.weixin.qq.com/s/I5va3PI1oGIj8R_n3Nw2yw

  7. https://www.cs.cmu.edu/~dga/papers/cuckoo-conext2014.pdf 检验hash冲突

    https://github.com/google/cityhash We like to test hash functions with SMHasher, among other things. ...

  8. Jenkins部署springboot项目

    记录jenkins如何部署springboot项目(jar类型的) 一.首先需要先配置好jenkins的基本配置(jdk.maven--),可在系统管理-->>全局工具配置中进行配置. 配 ...

  9. C++ Primer Plus读书笔记(三)复合类型

    这节主要是介绍数组.结构体.类等概念,以及new delete内存管理. 1.数组 首先普及一下C++中字符串的概念,只有在结尾有 \0 的才叫字符串, cout 输出字符串也以空字符为标记进行结束输 ...

  10. Solon rpc 1.3.1 发布,推出Cloud接口与配置规范

    Solon 是一个微型的Java RPC开发框架.项目从2018年启动以来,参考过大量前人作品:历时两年,3500多次的commit:内核保持0.1m的身材,超高的跑分,良好的使用体验.支持:Rpc. ...