Spring AOP简介

问题提出

首先我们回顾一下OOP(Object Oriented Programming-面向对象编程),OOP引入了封装、继承、多态等概念建立了一种对象层次结构,用于模拟公共行为的一个集合。不过OOP允许开发者定义纵向的关系,并不适合定义横向的关系(例如日志功能)。 日志代码常常是横向的散布在所有对象层次中,这种散布在各处的重复的代码被称为横切(cross cutting)。如果仍然使用OOP设计,会导致大量的代码重复,不利于各模块的重用。因而我们引入AOP的编程思想。

面向切面编程

AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP的补充和完善。

在面向切面编程的思想里面,把功能分为核心业务功能和周边功能。

  • 核心业务: 比如登陆、CRUD等;
  • 周边功能: 比如性能统计、日志、事务管理等

上述的周边功能在Spring的AOP思想中,被定义为切面

在AOP思想里,核心业务功能和切面功能分别独立开发,随后把切面功能和核心业务“编织”在一起,这就叫AOP。

AOP 好处

AOP能够将那些和业务无关,却被业务模块共同调用的逻辑或责任(例如事务处理、日志管理、权限控制等)封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并且有利于未来的扩展和维护。

AOP 中的概念(理解即可)

  • 切入点(Pointcut)

    在哪些类、哪些方法上切入

  • 通知(Advice)

    在方法执行的什么时机(方法前?后?前和后?)增强什么功能

  • 切面(Aspect)

    切面 = 切入点 + 通知,即在什么时机,哪个地方,增强什么功能!

  • 织入(Weaving)

    把切面加入到对象中,并创建出代理对象的过程。(Spring实现)

AOP的一个案例

也可参照我的前一篇博客: Spring 代理模式

AOP 实现

问题背景说明:现有UserService接口(提供用户的CRUD),UserServiceImpl类(实现接口中方法)。如下:(均在service包下)

package service;public interface UserService {public void add();public void delete();public void update();public void select();
}
package service;public class UserServiceImpl implements UserService {public void add() {System.out.println("增加了一个用户");}public void delete() {System.out.println("删除了一个用户");}public void update() {System.out.println("更新信息");}public void select() {System.out.println("查询用户");}
}

需求: 对方法的执行实现追踪,即添加简易的日志功能!

注意: 使用aop织入,需要引入相应的依赖,代码如下:

<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.4</version>
</dependency>

方法一(使用原生Spring API接口)

首先创建log包,创建两个类:Log 、 AfterLog,分别实现相应的接口。代码如下:

Log:

package log;import org.springframework.aop.MethodBeforeAdvice;import java.lang.reflect.Method;public class Log implements MethodBeforeAdvice {//method:要执行的目标对象的方法//objects: 参数//target: 目标对象public void before(Method method, Object[] objects, Object target) throws Throwable {System.out.println(o.getClass().getName()+"的"+method.getName()+"被执行了!");}
}

AfterLog:

package log;import org.springframework.aop.AfterReturningAdvice;import java.lang.reflect.Method;public class AfterLog implements AfterReturningAdvice {public void afterReturning(Object returnValue, Method method, Object[] objects, Object target) throws Throwable {System.out.println("执行了"+method.getName()+",返回结果为:"+returnValue);}
}

applicationContext.xml中配置:(放在resources包下)

<?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd"><!--注册bean--><bean id="userService" class="com.demut.service.UserServiceImpl"/><bean id="log" class="com.demut.log.Log"/><bean id="afterLog" class="com.demut.log.AfterLog"/><!--方式一: 使用原生Spring API接口--><!--配置aop: 需要导入aop的约束--><aop:config><!--切入点 : expression: 表达式,execution(要执行的位置!* * * * *)--><aop:pointcut id="pointcut" expression="execution(* com.demut.service.UserServiceImpl.*(..))"/><!--执行环绕增加!--><aop:advisor advice-ref="log" pointcut-ref="pointcut"/><aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/></aop:config></beans>

测试类:

import com.demut.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//动态代理 代理的是接口!!!UserService userService = context.getBean("userService", UserService.class);userService.select();}
}

运行结果:

此方法中重点关注,applicationContext.xml中配置!

方法二(自定义类来实现)

创建diy包,在内部创建类:DiyPointCut,类中内容如下:

package com.demut.diy;public class DiyPointCut {public void before() {System.out.println("************方法执行前************");}public void after() {System.out.println("************方法执行后************");}
}

修改applicationContext.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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd"><!--注册bean--><bean id="userService" class="com.demut.service.UserServiceImpl"/><bean id="log" class="com.demut.log.Log"/><bean id="afterLog" class="com.demut.log.AfterLog"/><!--方式二:自定义类--><bean id="diy" class="com.demut.diy.DiyPointCut"/><aop:config><!--自定义切面ref要引用的类--><aop:aspect ref="diy"><!--切入点--><aop:pointcut id="point" expression="execution(* com.demut.service.UserServiceImpl.*(..))"/><!--通知--><aop:before method="before" pointcut-ref="point"/><aop:after method="after" pointcut-ref="point"/></aop:aspect></aop:config></beans>

测试结果:

方法三(使用注解实现)

使用到的注解:

  • @Aspect
  • @Before
  • @After
  • @Around

在diy包下新建类: AnnotationPointCut类:

package com.demut.diy;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;@Aspect //标注这个类是一个切面
public class AnnotationPointCut {@Before("execution(* com.demut.service.UserServiceImpl.*(..))")public void before() {System.out.println("==============方法执行前==============");}@After("execution(* com.demut.service.UserServiceImpl.*(..))")public void after() {System.out.println("==============方法执行后==============");}//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点@Around("execution(* com.demut.service.UserServiceImpl.*(..))")public void around(ProceedingJoinPoint jp) throws Throwable {System.out.println("环绕前");//获取签名~获取执行的是哪个类中的哪个方法Signature signature = jp.getSignature();System.out.println(signature);//执行方法Object proceed = jp.proceed();System.out.println("环绕后");}
}

在applicationContext.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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd"><!--注册bean--><bean id="userService" class="com.demut.service.UserServiceImpl"/><bean id="log" class="com.demut.log.Log"/><bean id="afterLog" class="com.demut.log.AfterLog"/><!--方式三:使用注解--><bean id="annotationPointCut" class="com.demut.diy.AnnotationPointCut"/><!--开启注解支持--><aop:aspectj-autoproxy/></beans>

测试结果:

【参考文章】

https://www.cnblogs.com/xrq730/p/4919025.html

https://www.jianshu.com/p/994027425b44

写在最后

瀑布的水逆流而上,

蒲公英种子从远处飘回,聚成伞的模样,

太阳从西边升起,落向东方。

子弹退回枪膛,

运动员回到起跑线上,

我交回录取通知书,忘了十年寒窗。

厨房里飘来饭菜的香,

你把我的卷子签好名字,

关掉电视,帮我把书包背上

你还在我身旁。

​ ——戴畅

Spring 面向切面编程(AOP) D5相关推荐

  1. Spring→面向切面编程AOP、相关概念、通知Advice类型、配置切面切入点通知、AOP相关API、AOP代理类ProxyFactoryBean、AOP注解@AspectJ

    面向切面编程AOP CGLib AOP相关概念 Advice类型 Spring实现AOP Spring配置切面aspect 配置切入点pointcut 配置通知advice 配置通知参数 调用新的父类 ...

  2. Spring——面向切面编程(AOP)

    1 AOP概述   AOP 并不是 Spring 框架的专属名称,它的全称是 Aspect Oriented Programming ,意为:面向切面编程.   在程序运行某个方法的时候,不修改原始执 ...

  3. Spring面向切面编程-AOP详解

    文章目录 前言 介绍AOP 一.实现AOP 1.1.全注解形式实现AOP 前提准备(引入jar包) 实现AOP(五种通知) 二.认识JoinPont与ProceedingJoinPoint 2.1.初 ...

  4. Spring in Action 入门之面向切面编程AOP

    注明:这篇文章一是当成学习笔记,二是给大家提供另一个快速理解学习Spring的参考.欢迎留言讨论,持续更新中~ (该部分是Spring的面向切面编程AOP) 第四章 通知Bean 在软件编程中,散布于 ...

  5. Spring(四):面向切面编程AOP

    2019独角兽企业重金招聘Python工程师标准>>> 横切关注点:分布于应用中多处的功能 面向切面编程AOP:将横切关注点与业务逻辑相分离 在使用面向切面编程时,仍在一个地方定义通 ...

  6. Spring之面向切面编程AOP(八)

    介绍&步骤 视频教程: https://www.bilibili.com/video/BV1WZ4y1P7Bp?p=121 官方笔记链接:https://pan.baidu.com/s/1dn ...

  7. Spring面向切面编程(AOP)详解

    Spring面向切面编程(AOP)详解 面向切面编程(AOP)是Spring框架的另外一个重要的核心内容. 而在讲AOP之前,先来了解一下动态代理这个概念,因为AOP基于动态代理. 动态代理概念:在程 ...

  8. 服务端第三次课程:面向切面编程AOP

    3:面向切面编程AOP 1:回顾 bean的组装方式 规划的装配 component autowired sacn是在configuration底下的 Java config 使用configurat ...

  9. Spring-学习笔记08【面向切面编程AOP】

    Java后端 学习路线 笔记汇总表[黑马程序员] Spring-学习笔记01[Spring框架简介][day01] Spring-学习笔记02[程序间耦合] Spring-学习笔记03[Spring的 ...

最新文章

  1. 独热编码(one-hot)是什么?什么数据类型需要进行独热编码?pandas如何进行独热编码(one-hot)?
  2. 组合,多态,封装, @property
  3. java随机产生密码_用Java生成随机密码的方法
  4. 已安装的sql怎么添加功能_微信群管理工具有哪些功能?怎么在社群中添加微信小助手?...
  5. html/css题库,DIV+CSS题库
  6. python技巧 计算字符串中字母出现的次数并取出最大
  7. raspberry pi_如何保持您的Raspberry Pi更新
  8. 计算机模拟与生态工程,2018年环境生态工程专业分析及就业前景
  9. BST-V51开发板用c语言,小代码 向原文学习 BST 简单的C语言版本
  10. 用angular Material 做统计表格
  11. metro风格的特点
  12. 什么是实例?什么是引用?
  13. day4-数字类型和列表基础
  14. 曾用心并深度参与的一款游戏今天发布了关服通知,回顾一下我最初的工作日报
  15. 教你炒股票25:每日解盘
  16. 不可重复读和幻读有什么区别?
  17. 关于下载表格数据乱码的解决方案
  18. MySQL-基础练习题1
  19. Metasploit 渗透测试之制作隐藏后门
  20. P2P之UDP穿透NAT的原理与实现--增强篇(附源代码)

热门文章

  1. 怎么申请企业邮箱,企业邮箱快速登录入口
  2. 去哪里下载SSL证书?
  3. java基础--名词解释汇总
  4. 【从零开始学深度学习编译器】十四,MLIR Toy Tutorials学习笔记之部分Lowering
  5. 两轮电自2.0时代开启 小牛电动以独立主见创造新物种
  6. 2021年美容师(初级)考试总结及美容师(初级)模拟试题
  7. 小写数字转大写金额php,php 金额小写数字转大写汉字
  8. 刚刚!腾讯荣升Linux基金会白金会员
  9. 使用关键字搜索公众号文章,
  10. 直播源代码中关于手机直播平台开发的登陆注册介绍