Spring框架学习笔记---->AOP

AOP概念

AOP:aspect oriented programing,面向切面编程。

将软件的各个模块,按照横向的角度进行观察,发现各个模块之间存在公共功能。

我们将公共功能抽取为增强代码,在运行的时候以代理方式的方式织入到目标对象中。

在上图的两个模块中,在执行内部的*.create方法的前后都出现相同的功能,我们可以将它们抽取出来。

然后将它们封装在一个类中,再通过织入的方式形成代理对象。

增强代码就是通过观察抽取出来的公共模块部分。

目标对象是抽取公共模块后的模块。

代理对象相当于没有抽取公共模块之前的目标对象。

生活中的例子

我们去买电脑,很多时候不是从生产电脑的工厂里去直接获得,而是从其他地方去获得。(比如说商厂、专卖店之类的地方)。

电脑工厂就相当于目标对象,主要用于你要去获得电脑。

专卖店就相当于代理对象,除了获取电脑,还要进行其他的一些事情,和专卖店里的人员进行一些其他的事情。

增强代码就是这里的其他事情,比如说喝喝咖啡?吹吹空调?

AOP思想演变

1、在不同的service层中可能出现相同放入代码:

问题:产生大量相同的代码。

2、为此我们可以把重复的部分封装为工具类,在需要的地方直接调用。

问题:在GoodsServiceImpl和UserServiceImpl中都存在着和TransactionUtil耦合的情况,不利于开发。

3、采用静态代理的方法解耦

问题:如果GoodsService中存在大量的方法时,代理对象将会对这些大量的方法进行重写。

4、动态代理

问题:需要编写一定量代码,有一定难度。

5、AOP

只需要通过编写配置文件和增强代码即可

AOP简单实例

结构图:

MyAspect:

package cn.sxt.spring.aop.aspect;public class MyAspect {public static void before() {System.out.println("开启事务");}public static void after() {System.out.println("提交事务");}
}

GoodsServiceImpl:

package cn.sxt.spring.aop.service.impl;import cn.sxt.spring.aop.service.GoodsService;public class GoodsServiceImpl implements GoodsService{@Overridepublic void create() {System.out.println("新增商品");}@Overridepublic void update() {System.out.println("更新商品");}}

GoodsService:

package cn.sxt.spring.aop.service;public interface GoodsService {public void create();public void update();
}

TestApp:

package cn.sxt.spring.aop.test;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.sxt.spring.aop.service.GoodsService;public class TestApp {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");GoodsService goodsService = (GoodsService) ac.getBean("goodsService");goodsService.create();}
}

applicationContext:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"     xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"><bean id="myAspect" class="cn.sxt.spring.aop.aspect.MyAspect"></bean><bean id="goodsService" class="cn.sxt.spring.aop.service.impl.GoodsServiceImpl"></bean><aop:config><aop:aspect ref="myAspect"><aop:pointcut expression="execution(* cn.sxt.spring.aop.service.impl.GoodsServiceImpl.*(..))" id="myPoint"/><aop:before method="before" pointcut-ref="myPoint"/><aop:after method="after" pointcut-ref="myPoint"/></aop:aspect></aop:config>
</beans>

在配置文件applicationContext中,需要配置三个东西:

  1、增强代码,也就是我们抽取出来公共的部分。

  2、目标对象。

  3、AOP:在AOP里我们可以对目标对象所需要的方法进行配置。

  <aop:aspect ref="myAspect"> 关联到关注点

  <aop:pointcut expression="execution(* cn.sxt.spring.aop.service.impl.GoodsServiceImpl.*(..))" id="myPoint"/>

  execution()执行一个方法,* 任意类型  cn.sxt.spring.aop.service.impl.GoodsServiceImpl 类名  * 任意方法名  (..) 任意参数  id 连接点名

  <aop:before method="before" pointcut-ref="myPoint"/>

  before在连接点之前 before 调用的方法名

AOP基本概念

目标对象

关注点:我们关心的公共功能了。横向观察时候,发现模块中存在公共功能

连接点:需要添加公共功能的地方

切入点:连接点集合

目标对象:包含连接点那些类

增强代码:

advice:关注点功能实现.TransactionUtil的before方法 ,after方法

aspect:关注点模块化,切面。TrasantionUtil实现before,after方法

aop proxy:动态代理实现

织入:将advice动态添加到连接点过程

转载于:https://www.cnblogs.com/huanhuan10/p/6736051.html

Spring框架学习(二)相关推荐

  1. Java学习笔记-Day64 Spring 框架(二)

    Java学习笔记-Day64 Spring 框架(二) 一.控制反转IOC和依赖注入DI 1.控制反转IOC 2.依赖注入DI 3.Spring IOC容器 3.1.简介 3.2.实现容器 3.2.获 ...

  2. Spring框架学习笔记,超详细!!(4)

    Java小白开始学习Spring框架,一方面,跟着视频学习,并记录下学习笔记,方便以后复习回顾.另一方面,发布学习笔记来约束自己,学习路程还很遥远,继续加油坚持!!!希望能帮助到大家! 另外还有我的牛 ...

  3. spring security 学习二

    spring security 学习二 doc:https://docs.spring.io/spring-security/site/docs/ 基于表单的认证(个性化认证流程): 一.自定义登录页 ...

  4. PyTorch框架学习二十——模型微调(Finetune)

    PyTorch框架学习二十--模型微调(Finetune) 一.Transfer Learning:迁移学习 二.Model Finetune:模型的迁移学习 三.看个例子:用ResNet18预训练模 ...

  5. PyTorch框架学习二——基本数据结构(张量)

    PyTorch框架学习二--基本数据结构(张量) 一.什么是张量? 二.Tensor与Variable(PyTorch中) 1.Variable 2.Tensor 三.Tensor的创建 1.直接创建 ...

  6. Struts2框架学习(二) Action

    Struts2框架学习(二) Action Struts2框架中的Action类是一个单独的javabean对象.不像Struts1中还要去继承HttpServlet,耦合度减小了. 1,流程 拦截器 ...

  7. 《SpringBoot框架学习二之HTTP协议》

    <SpringBoot框架学习二之HTTP协议> 文章目录 <SpringBoot框架学习二之HTTP协议> 一.HTTP介绍 (1)概述 (2)HTTP版本协议 1.HTTP ...

  8. Spring框架学习笔记(三)(AOP,事务管理)

    Spring框架学习笔记(三) 九.AOP 9.1 AOP的注解配置 (1) 新建计算器核心功能(模拟:不能在改动核心代码) (2) 建立一个普通的Java类写增强代码(面向切面编程),使用Sprin ...

  9. 深入学习Spring框架(二)- 注解配置

    1.为什么要学习Spring的注解配置? 基于注解配置的方式也已经逐渐代替xml.所以我们必须要掌握使用注解的方式配置Spring. 关于实际的开发中到底使用xml还是注解,每家公司有着不同的使用习惯 ...

最新文章

  1. Xen之初体验:HA(额外附加)
  2. 菜鸟requireJS教程---1、初识requirejs
  3. scrapy框架对接seleniumpipeline数据持久化
  4. html.renderaction 控制器,Html.RenderAction简单用法
  5. iar stm32_树莓派玩转STM32开发(一)——介绍篇
  6. esp8266 阿里云 arduino_NUCLEO-G071RB通过WiFi与NB连接阿里云
  7. 二进制字符串转换到Ascll字符串
  8. jquery --- Poshy Tip jQuery Plugin
  9. 码匠编程:7 个令人兴奋的 JavaScript 新特性
  10. [你的灯亮着吗]读书笔记
  11. TensorFlow by Google过拟合优化 Machine Learning Foundations: Ep #7 - Image augmentation and overfitting
  12. json转map几种方法
  13. 游戏外挂篇:如何Dump内存获得游戏的辅助
  14. Ps学习(快速选择工具和魔棒的使用)
  15. oracle 本地连接不上,为什么Guardium S-TAP无法抓取Oracle本地连接(Bequeath)流量
  16. python绑定按键pageup键_键盘事件keydown、keypress、keyup随笔整理总结(摘抄)
  17. 当前时间、既定时间后的时间及时间比较大小(java实现)
  18. 要如何图片文字识别翻译?这些软件能帮你
  19. 麦当劳宣布20年来最大规模收购 提高服务智能化
  20. 正则表达式—HTML中的匹配

热门文章

  1. django中URL常用配置方法
  2. c4503文件服务器,理光C3503/C4503/C5503检查状态下各项目说明解释
  3. 标识符怎么读_音标怎么学?到底该学英式还是美式
  4. thinkphp3.2.3 找不到自定义模型_Orion HTC VIVE高性价比动作捕捉,虚拟直播 支持UE4.25 导入自定义模型...
  5. 如何完整卸载wxpython_TextCtrl的WXPython C++部分被删除
  6. 【Clickhouse】Clickhouse Cannot create table with column ‘Int256‘ because experimental bigint types
  7. 【Java】DNS缓存
  8. 【Java】Java Long映射到浏览器或者JavaScript后损失精度
  9. 【Flink】ValidationException: Comparison is only supported for numeric types and comparable types
  10. 【Spring】Spring boot 如何进行私有方法测试