AOP

aspect oriented programming
是通过预编译方式和运行期动态代理实现程序和功能的统一维护的一种技术。
aop是oop的延续,是函数式编程的一种衍生。利用aop可以对业务逻辑的各个部分进行隔离,从而使得
业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发效率。

aop作用
在程序运行期间不用修改代码对已有方法进行增强
优势:减少使用重复代码、提高开发效率、维护方便

aop的实现方式 使用动态代理

spring aop 相关术语

JoinPoint 连接点,指哪些被拦截的点,是方法,或织入时机
PointCut 切入点,指对哪些JoinPoint进行拦截的定义,是被增强的方法
Advice 通知,指拦截到JointPoint之后要做的事情,有前置通知、后置通知、环绕通知
异常通知、最终通知
Introduction
Target 被代理对象(目标对象)
Weaving 加入事物支持的过程
Proxy 代理,一个类被AOP织入增强后,就产生了一个代理类
Aspect 切面,切入点和通知的结合

aop要求能提取出公共代码,并做成通知。

基于xml的aop

访问修饰符可以省略,
返回值可以使用*,
包名可以使用通配符
…表示当前包及其子包
类名和方法名可以通配
参数列表可以直接写数据类型
全通配写法*….*(…)

实际开发中的通常写法是业务层实现类下的所有方法·

 com.test3.AccountServiceImp.*(..)package com.test3;public interface IAccountService {void saveAccount();void updateAccount(int i);int deleteAccount();
}
package com.test3;public class AccountServiceImp implements IAccountService{public int deleteAccount() {// TODO Auto-generated method stubSystem.out.println("执行了删除");return 0;}public void saveAccount() {// TODO Auto-generated method stubSystem.out.println("执行了保存");}public void updateAccount(int i) {// TODO Auto-generated method stubSystem.out.println("执行了更新");}}
package com.test3;public class Logger {/*** 用于打印日志,在切入点方法执行之前执行**/public void printLog(){System.out.println("记录日志...");}
}
<?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" xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  <bean id="accountService" class="com.test3.AccountServiceImp"></bean>
<bean id="logger" class="com.test3.Logger"></bean>
<aop:config><!-- 配置切面 --><aop:aspect id="logAdvice" ref="logger"><!-- 配置通知的类型,且与建立通知方法和切入点方法的关联 --><aop:before method="printLog" pointcut="execution(public void com.test3.AccountServiceImp.saveAccount())"/></aop:aspect>
</aop:config>
</beans>
package com.test3;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");IAccountService accountService = (IAccountService)ac.getBean("accountService");accountService.saveAccount();accountService.updateAccount(1);accountService.deleteAccount();}
}

三月 27, 2020 11:42:21 上午 org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@12922f6: display name [org.springframework.context.support.ClassPathXmlApplicationContext@12922f6]; startup date [Fri Mar 27 11:42:21 CST 2020]; root of context hierarchy 三月 27, 2020 11:42:21 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [bean.xml] 三月 27, 2020 11:42:21 上午 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@12922f6]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1356f42 三月 27, 2020 11:42:21 上午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1356f42: defining beans [accountService,logger,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.aop.aspectj.AspectJPointcutAdvisor#0]; root of factory hierarchy

记录日志…
执行了保存

四种通知

配置切入点表达式

<?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" xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  <bean id="accountService" class="com.test3.AccountServiceImp"></bean>
<bean id="logger" class="com.test3.Logger"></bean>
<aop:config>
<!--可以在所有切面中使用,但必须放在切面声明之前--><aop:pointcut id="pt2" expression="execution(* com.test3.AccountServiceImp.*(..))"/><!-- 配置切面 --><aop:aspect id="logAdvice" ref="logger"><!-- 配置通知的类型,且与建立通知方法和切入点方法的关联 --><aop:before method="printLog" pointcut-ref="pt1"/><!--只能当前切面使用--><aop:pointcut id="pt1" expression="execution(* com.test3.AccountServiceImp.*(..))"/></aop:aspect>
</aop:config>
</beans>

基于注解的AOP

package com.test3;public interface IAccountService {void saveAccount();void updateAccount(int i);int deleteAccount();
}
package com.test3;import org.springframework.stereotype.Service;@Service("accountService")
public class AccountServiceImp implements IAccountService{public int deleteAccount() {// TODO Auto-generated method stubSystem.out.println("执行了删除");return 0;}public void saveAccount() {// TODO Auto-generated method stubSystem.out.println("执行了保存");}public void updateAccount(int i) {// TODO Auto-generated method stubSystem.out.println("执行了更新");}}
package com.test3;import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;@Component("logger")
@Aspect //表示当前类是一个切面
public class Logger {@Pointcut("execution(* com.test3.AccountServiceImp.*(..))")private void pt1(){}/*** 用于打印日志,在切入点方法执行之前执行**/@Before("pt1()")public void printLog(){System.out.println("记录日志...");}
}
<?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" xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  <context:component-scan base-package="com.test3"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
package com.test3;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");IAccountService accountService = (IAccountService)ac.getBean("accountService");accountService.saveAccount();accountService.updateAccount(1);accountService.deleteAccount();}
}

三月 27, 2020 12:35:47 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@12c9557: display name [org.springframework.context.support.ClassPathXmlApplicationContext@12c9557]; startup date [Fri Mar 27 12:35:47 CST 2020]; root of context hierarchy 三月 27, 2020 12:35:47 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [bean.xml] 三月 27, 2020 12:35:47 下午 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@12c9557]: org.springframework.beans.factory.support.DefaultListableBeanFactory@3a3001 三月 27, 2020 12:35:47 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@3a3001: defining beans [accountService,logger,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.aop.config.internalAutoProxyCreator]; root of factory hierarchy

记录日志…
执行了保存
记录日志…
执行了更新
记录日志…
执行了删除

spring5.0学习笔记10相关推荐

  1. mysql5.0镜像_Mysql5.0学习笔记(一)

    Mysql5.0学习笔记(一) -基本sql语句与支持字符集 1.登录 mysql -h localhost -u root 2.创建用户firstdb(密码firstdb)和数据库,并赋予权限于fi ...

  2. thinkphp学习笔记10—看不懂的路由规则

    原文:thinkphp学习笔记10-看不懂的路由规则 路由这部分貌似在实际工作中没有怎么设计过,只是在用默认的设置,在手册里面看到部分,艰涩难懂. 1.路由定义 要使用路由功能需要支持PATH_INF ...

  3. SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传

    SpringMVC:学习笔记(10)--整合Ckeditor且实现图片上传 配置CKEDITOR 精简文件 解压之后可以看到ckeditor/lang下面有很多语言的js,如果不需要那么多种语言的,可 ...

  4. Hadoop学习笔记—10.Shuffle过程那点事儿

    Hadoop学习笔记-10.Shuffle过程那点事儿 一.回顾Reduce阶段三大步骤 在第四篇博文<初识MapReduce>中,我们认识了MapReduce的八大步骤,其中在Reduc ...

  5. Linux学习笔记10

    Linux学习笔记10 Linux学习笔记10 正则表达式 源码包约定目录 Shell脚本约定目录 Shell脚本的创建与执行 date命令 同步时间 Shell脚本预设变量 与用户交互 数学计算 S ...

  6. HALCON 20.11:深度学习笔记(10)---分类

    HALCON 20.11:深度学习笔记(10)---分类 HALCON 20.11.0.0中,实现了深度学习方法. 本章解释了如何在训练和推理阶段使用基于深度学习的分类. 基于深度学习的分类是一种对一 ...

  7. 台大李宏毅Machine Learning 2017Fall学习笔记 (10)Tips for Deep Learning

    台大李宏毅Machine Learning 2017Fall学习笔记 (10)Tips for Deep Learning 注:本博客主要参照 http://blog.csdn.net/xzy_thu ...

  8. Zabbx6.0(学习笔记)

    Zabbx6.0(学习笔记) 目录导航 Zabbx6.0(学习笔记) 一.为什么 需要监控系统 二.如何选择监控 三.Zabbix概述 四.Zabbix安装哪个版本? Zabbix安装要求 1.硬件 ...

  9. Python学习笔记--10.Django框架快速入门之后台管理admin(书籍管理系统)

    Python学习笔记--10.Django框架快速入门之后台管理 一.Django框架介绍 二.创建第一个Django项目 三.应用的创建和使用 四.项目的数据库模型 ORM对象关系映射 sqlite ...

  10. flink1.12.0学习笔记第2篇-流批一体API

    flink1.12.0学习笔记第 2 篇-流批一体API flink1.12.0学习笔记第1篇-部署与入门 flink1.12.0学习笔记第2篇-流批一体API flink1.12.0学习笔记第3篇- ...

最新文章

  1. Python实现获得SQLServer数据库中的表插入到Oracle数据库
  2. 如何用excle制作黑人拉馆_家居DIY带你学习如何用塑料勺制作壁挂!
  3. C++ QT中的QSound使用方法
  4. Cisco Catalyst 2960系列交换机资料
  5. Ubuntu 18.0安装教程
  6. 由已打开的文件读取数据---read
  7. js解析json数据
  8. JAVA实现网页版斗地主_Java实现斗地主最简代码实例
  9. Java面试笔试题大全
  10. bpsk在瑞利信道matlab,请教BPSK在瑞利信道下的误码率仿真
  11. Acwing1183. 电力
  12. PyCharm输入法无法切换中英文
  13. JavaScript 教程,很好的!
  14. 原生实现freeCodeCamp上的Build a Personal Portfolio Webpage
  15. openlayers 绘制tin数据导文
  16. Cognos 11.0快速开发指南
  17. 疲劳检测-眼睛,嘴巴
  18. sw槽钢插件_Solidworks GB结构构件-GB3D型材库(几乎包含常用的各种型材)
  19. 将Vue项目部署在Nginx,解决前端路由、反向代理和静态资源问题
  20. 中国大城市政治地位综合实力排名

热门文章

  1. TurboFan-Sea of Nodes概念讲解
  2. python isinstance() type()
  3. Spring boot中使用Jackson ObjectMapper注入
  4. nginx allow 多个ip ipv4的网段表示方法解析
  5. Oracle Study之-- enq:SQ contention等待事件
  6. 那些初创互联网公司CEO最常说的的谎言
  7. sql查询慢原因及优化
  8. php foreach 传值还是传引用
  9. poj1847 最短路
  10. JSP,PHP,Python,Ruby,Perl概要及各自特点