文章更新时间:2020/04/06

一、一个例子

  在上面的例子中,包租婆的核心业务就是签合同,收房租,那么这就够了,灰色框起来的部分都是重复且边缘的事,交给中介商就好了,这就是 AOP 的一个思想:让关注点代码与业务代码分离!

  例子解析:

1.在 Package【service】下新建一个【LandlordService】类(核心业务)

/**
* 包租婆关心的业务~(模拟核心业务)
*/
@Component("landlord")
public class LandlordService {
public void service() {
// 仅仅只是实现了核心的业务功能
System.out.println("签合同");
System.out.println("收房租");
}
}

2.在 Package【aspect】下新建一个中介商【BrokerAspect】类(周边功能)

/**
* 包租婆不关心~
* 中介关心的业务~(模拟周边功能)
*/
@Component
@Aspect
class BrokerAspect { @Before("execution(* service.LandlordService.service())")
public void before() {
System.out.println("带租客看房");
System.out.println("谈价格");
} @After("execution(* service.LandlordService.service())")
public void after() {
System.out.println("交钥匙");
}
}

3.在 applicationContext.xml 中配置自动注入,并告诉 Spring IoC 容器去哪里扫描这两个 Bean

<?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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="aspect"/>
<context:component-scan base-package="service"/> <!--声明自动为spring容器中那些配置@Aspect切面的bean创建代理,织入切面。-->
<aop:aspectj-autoproxy/> </beans>

4.编写测试类

public class TestSpring {
@Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
LandlordService landlord = (LandlordService) context.getBean("landlord", LandlordService.class);
landlord.service();
}
}

5.查看执行结果

二、使用注解来开发 Spring AOP

第一步:选择连接点

  Spring 是方法级别的 AOP 框架,我们主要也是以某个类额某个方法作为连接点,另一种说法就是:选择哪一个类的哪一方法用以增强功能。

    ....
public void service() {
// 仅仅只是实现了核心的业务功能
System.out.println("签合同");
System.out.println("收房租");
}
....

  这里就选择上述 LandlordService 类中的 service() 方法作为连接点。

第二步:创建切面

  选择好了连接点就可以创建切面了,我们可以把切面理解为一个拦截器,当程序运行到连接点的时候,被拦截下来,在开头加入了初始化的方法,在结尾也加入了销毁的方法而已,在 Spring 中只要使用 @Aspect 注解一个类,那么 Spring IoC 容器就会认为这是一个切面了:

/**
* 包租婆不关心~
* 中介关心的业务~(模拟周边功能)
*/
@Component
@Aspect
class BrokerAspect { @Before("execution(* service.LandlordService.service())")
public void before() {
System.out.println("带租客看房");
System.out.println("谈价格");
} @After("execution(* service.LandlordService.service())")
public void after() {
System.out.println("交钥匙");
}
}

PS: 被定义为切面的类仍然是一个 Bean ,需要 @Component 注解标注

第三步:定义切点

  在上面的注解中定义了 execution 的正则表达式,Spring 通过这个正则表达式判断具体要拦截的是哪一个类的哪一个方法:

execution(* service.LandlordService.service())

  依次对这个表达式作出分析:

  • execution:代表执行方法的时候会触发
  • * :代表任意返回类型的方法
  • service.LandlordService:代表类的全限定名
  • service():被拦截的方法名称

第四步:测试 AOP

  我们来探讨一下环绕通知,这是 Spring AOP 中最强大的通知,因为它集成了前置通知和后置通知,它保留了连接点原有的方法的功能,所以它及强大又灵活,让我们来看看:

/**
* 包租婆不关心~
* 中介关心的业务~(模拟周边功能)
*/
@Component
@Aspect
class BrokerAspect { // 注释掉之前的 @Before 和 @After 注解以及对应的方法
// @Before("execution(* service.LandlordService.service())")
// public void before() {
// System.out.println("带租客看房");
// System.out.println("谈价格");
// }
//
// @After("execution(* service.LandlordService.service())")
// public void after() {
// System.out.println("交钥匙");
// } // 使用 @Around 注解来同时完成前置和后置通知
@Around("execution(* service.LandlordService.service())")
public void around(ProceedingJoinPoint joinPoint) {
System.out.println("带租客看房");
System.out.println("谈价格"); try {
joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
} System.out.println("交钥匙");
}
}

  运行测试代码,结果仍然正确:

三、使用 XML 配置开发 Spring AOP

  注解是很强大的东西,但基于 XML 的开发我们仍然需要了解,我们先来了解一下 AOP 中可以配置的元素:

  有了之前通过注解来编写的经验,并且有了上面的表,我们将上面的例子改写成 XML 配置很容易(去掉所有的注解):

<!-- 装配 Bean-->
<bean name="landlord" class="pojo.Landlord"/>
<bean id="broker" class="aspect.Broker"/> <!-- 配置AOP -->
<aop:config>
<!-- where:在哪些地方(包.类.方法)做增加 -->
<aop:pointcut id="landlordPoint"
expression="execution(* pojo.Landlord.service())"/>
<!-- what:做什么增强 -->
<aop:aspect id="logAspect" ref="broker">
<!-- when:在什么时机(方法前/后/前后) -->
<aop:around pointcut-ref="landlordPoint" method="around"/>
</aop:aspect>
</aop:config>

运行测试程序,看到正确结果:

Spring学习(八)AOP详解的更多相关文章

  1. Spring学习之AOP详解

    aop使用方式 @Aspect注解 wildcards通配符: * 匹配任意数量的字符 + 匹配指定类及其子类 .. 一般用于匹配任意数的子包或参数 operators运算符 && 与 ...

  2. [Spring学习笔记 5 ] Spring AOP 详解1

    知识点回顾:一.IOC容器---DI依赖注入:setter注入(属性注入)/构造子注入/字段注入(注解 )/接口注入 out Spring IOC容器的使用: A.完全使用XML文件来配置容器所要管理 ...

  3. Spring AOP详解及简单应用

    Spring AOP详解   一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址: ...

  4. 转:Spring AOP详解

    转:Spring AOP详解 一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址: ...

  5. 【转载】Spring AOP详解 、 JDK动态代理、CGLib动态代理

    Spring AOP详解 . JDK动态代理.CGLib动态代理  原文地址:https://www.cnblogs.com/kukudelaomao/p/5897893.html AOP是Aspec ...

  6. Spring AOP详解(转载)所需要的包

    上一篇文章中,<Spring Aop详解(转载)>里的代码都可以运行,只是包比较多,中间缺少了几个相应的包,根据报错,几经百度搜索,终于补全了所有包. 截图如下: 在主测试类里面,有人怀疑 ...

  7. Spring全家桶——SpringBoot之AOP详解

    Spring全家桶--SpringBoot之AOP详解 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关键单元是类,而在AOP中,模块化单元是方 ...

  8. Spring Aop 详解二

    这是Spring Aop的第二篇,案例代码很详解,可以查看https://gitee.com/haimama/java-study/tree/master/spring-aop-demo. 阅读前,建 ...

  9. spring cache 学习——@CachePut 使用详解

    1. 功能说明 当需要在不影响方法执行的情况下更新缓存时,可以使用 @CachePut,也就是说,被 @CachePut 注解的缓存方法总是会执行,而且会尝试将结果放入缓存(当然,是否真的会缓存还跟一 ...

  10. Spring4 AOP详解

    Spring4 AOP详解 第一章Spring 快速入门并没有对Spring4 的 AOP 做太多的描述,是因为AOP切面编程概念不好理解.所以这章主要从三个方面详解AOP:AOP简介(了解),基于注 ...

随机推荐

  1. 自我介绍网页填写表格PHP,JavaScript,html,css代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. .NET 设计模式 思维导图

    关于.NET 设计模式 思维导图 背景说明 以前都在匆匆忙忙写代码,在无穷无尽的需求中间左冲右突,最近终于有一些闲暇的时间,来总结和思考编程中的一些核心思想,磨刀不误砍柴的功夫,期望通过总结和学习,能 ...

  3. 操作系统-PV习题

    (1)阅览室问题:加入阅览室入口有一本登记册,每个人都必须按顺序签名进去. 想法:登记册可以用结构数组A[]表示,包含name和number.此外,还需要信号量seatcount表示剩余座位数. 使用 ...

  4. 算法-搜索(5)m路搜索树

    动态m路搜索树即系统运行时可以动态调整保持较高搜索效率的最多m路的搜索树.以3路搜索树为例说明其关键码排序关系:   const int MaxValue=; template <class T ...

  5. 产品经理培训教程视频大全与模板Axure rp9与8视频教程元件库模板

    注意:请仔细阅读购买,一旦发货百度网盘链接不能退~ 自动发邮件到买家留言处的邮箱,或注册淘宝时的邮箱自动通过旺旺给您发货还可以访问网页提取链接自助提取(复制到浏览器): http://4k5.cn/V ...

  6. 模拟画图题P1185 绘制二叉树

      题目链接P1185 绘制二叉树 题意概述   根据规则绘制一棵被删去部分节点的满二叉树.节点用 \(o\) 表示,树枝用/\表示.每一层树枝长度会变化,以满足叶子结点有如下特定: 相邻叶子节点是兄 ...

  7. 区块链入门到实战(30)之Solidity – 基础语法

    一个 Solidity 源文件可以包含任意数量的合约定义.import指令和pragma指令. 让我们从一个简单的 Solidity 源程序开始.下面是一个 Solidity 源文件的例子: prag ...

  8. 给Django的Admin添加自定义Action 并移除需要选择对象的限制

    不得不说,Django的Admin真的给开发带来很多便利,这不,我又来折腾了,这次是添加自定义的action 这个自定义的Action可以看看官方文档的介绍,很详细,不再赘述. https://doc ...

  9. SpringBoot中关于Excel的导入和导出

    前言   由于在最近的项目中使用Excel导入和导出较为频繁,以此篇博客作为记录,方便日后查阅.本文前台页面将使用layui,来演示对Excel文件导入和导出的效果.本文代码已上传至我的gitHub, ...

  10. unimrcp plugin 分析

    摘要: unimrcp 访问媒体资源是通过插件实现,社区的代码给出了demo plugin,但是距离一个生产插件还是有一段的距离.这边文章介绍插件的整个逻辑过程,以及如何实现我们自己的插件.