Spring基础——AOP
AOP术语
- 1、连接点:类里面可以被增强的方法
- 2、切入点:实际被增强的方法
- 3、通知(增强):实际增强的逻辑部分。通知类型有多种:前置通知@Before、后置通知@AfterReturning、环绕通知@Around、异常通知@AfterThrowing、最终通知@After
- 4、切面:是动作,吧通知应用到接入点过程
1、Spring 框架一般都是基于 AspectJ 实现 AOP 操作
(1)AspectJ 不是 Spring 组成部分,独立 AOP 框架,一般把 AspectJ 和 Spirng 框架一起使用,进行 AOP 操作
2、基于 AspectJ 实现 AOP 操作
- (1)基于 xml 配置文件实现
- (2)基于注解方式实现
3、切入点表达式
- (1)切入点表达式作用:知道对哪个类里面的哪个方法进行增强
- (2)语法结构: execution([权限修饰符] [返回类型] [类全路径] [方法名称]([参数列表]) )返回类型可不写
- 例如:execution(* com.dao.BookDao.add(…)) *表示全部
4、执行顺序为:
①无错误情况
- @Around环绕之前
- @Before前置通知
- 被增强方法add
- @Around环绕之后
- @After最终通知
- @AfterReturning后置通知
②、有错误情况
- @Around环绕之前
- @Before前置通知
- @After最终通知
- @AfterThrowing异常通知
(1)、基于注解方式实现
①创建配置类
@Configuration
@ComponentScan(value = "com.springtest.aopanno")
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class SpringConfig {}
②创建被增强类
@Component
public class User {public void add() {System.out.println("add .......................");}
}
③创建代理类、并使用@Pointcut注解抽离相同的切入点
@Component
@Aspect //生成代理对象
public class UserProxy {// 抽取相同切入点@Pointcut(value = "execution(* com.springtest.aopanno.User.add())")public void pointdemo() {}// 前置通知
// @Before(value = "execution(* com.springtest.aopanno.User.add())")@Before(value = "pointdemo()")public void before() {System.out.println("before ..................");}// 最终通知
// @After(value = "execution(* com.springtest.aopanno.User.add())")@After(value = "pointdemo()")public void after() {System.out.println("after ..................");}// 后置通知 返回值之后执行
// @AfterReturning(value = "execution(* com.springtest.aopanno.User.add())")@AfterReturning(value = "pointdemo()")public void afterReturning() {System.out.println("afterReturning ..................");}// 异常通知
// @AfterThrowing(value = "execution(* com.springtest.aopanno.User.add())")@AfterThrowing(value = "pointdemo()")public void afterThrowing() {System.out.println("afterThrowing ..................");}// 环绕通知
// @Around(value = "execution(* com.springtest.aopanno.User.add())")@Around(value = "pointdemo()")public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {System.out.println("around环绕之前 ..................");
// 执行被增强的方法proceedingJoinPoint.proceed();System.out.println("around环绕之后 ..................");}
}
当一个方法被多个同种通知修饰时可使用 @Order(1)") 注解指定其优先级,数值越小优先级越高
@Before(value = “execution(* com.springtest.aopanno.User.add())”)
@Order(1)
public void before1() {
System.out.println(“before …”);
}
@Before(value = “execution(* com.springtest.aopanno.User.add())”)
@Order(2)
public void before2() {
System.out.println(“before …”);
}
(1)、基于xml方式实现
<!--创建对象-->
<bean id="book" class="com.springtest.aopxml.Book"></bean>
<bean id="bookProxy" class="com.springtest.aopxml.BookProxy"></bean>
<!--配置aop增强-->
<aop:config>
<!--切入点--><aop:pointcut id="p" expression="execution(* com.springtest.aopxml.Book.buy(..))"/>
<!--配置切面--><aop:aspect ref="bookProxy"><!--增强作用在具体的方法上--><aop:before method="beafter" pointcut-ref="p"/></aop:aspect>
</aop:config>
Spring基础——AOP相关推荐
- Spring基础专题——第六章(Aop编程)
前言:去年到现在一直没有很好的时间完成这个spring基础+源码的博客目标,去年一年比较懒吧,所以今年我希望我的知识可以分享给正在奋斗中的互联网开发人员,以及未来想往架构师上走的道友们我们一起进步,从 ...
- Spring基础专题——第五章(Aop代理)
前言:去年到现在一直没有很好的时间完成这个spring基础+源码的博客目标,去年一年比较懒吧,所以今年我希望我的知识可以分享给正在奋斗中的互联网开发人员,以及未来想往架构师上走的道友们我们一起进步,从 ...
- 1.3—Spring基础配置—3.AOP
1.点睛: AOP:面向切面编程,相对于OOP面向对象编程. Spring的AOP的存在目的是为了解耦.AOP可以让一组类共享相同的行为.在OOP中只能通过继承类和实现接口,来使代码的耦合度增强,AO ...
- [Spring 深度解析]第4章 Spring之AOP
第4章 ◄Spring之AOP► 在上一章节中,我们大致了解了Spring核心容器,了解了IOC思想在Spring中的具体应用Bean容器以及Bean的配置与使用,这一章我们将开始学习AOP在Spri ...
- [Spring 深度解析]第2章 Spring基础
第2章 ◄Spring基础► 在上一章节中,我们学习了Java的注解与反射,在这一章节我们将了解一下Spring框架,并学习Spring框架中重要的编程思想控制反转(IOC).面向切面编程(AOP ...
- Spring中AOP相关的API及源码解析,原来AOP是这样子的
前言 之所以写这么一篇文章主要是因为下篇文章将结束Spring启动整个流程的分析,从解析配置到创建对象再到属性注入最后再将创建好的对象初始化成为一个真正意义上的Bean.因为下篇文章会涉及到AOP,所 ...
- Spring【AOP模块】就是这么简单
2019独角兽企业重金招聘Python工程师标准>>> 前言 到目前为止,已经简单学习了Spring的Core模块.....于是我们就开启了Spring的AOP模块了...在讲解AO ...
- 4、spring核心AOP
Spring AOP 1. Spring AOP 概述 1-1 1.1. 何为AOP? 1-1 1.2. AOP要解决什么问题? 1-1 1.3. AOP实际项目应用场景? 1-2 1.4. AO ...
- Spring(四)——AOP、Spring实现AOP、Spring整合Mybatis、Spring中的事务管理
文章目录 1. 什么是AOP 2. 使用Spring实现AOP 2.1 使用Spring的API 接口实现 2.2 自定义实现 2.3 使用注解实现 3. 整合MyBatis 3.1 MyBatis- ...
最新文章
- [转]在cocos2d-x中让一个项目适配iphone、iphone retina、ipad、ipad retina四种分辨率
- 为什么我们总认为开源不挣钱?
- 两点(51nod 深搜)
- HDU 1827 Summer Holiday 图论scc
- WCF 实例 —— Android 短信助手 (WCF + Android)
- 2021高考倒计时HTML源码,2021高考倒计时
- DEDE源码分析与学习--index.php文件解读
- 【Leetcode】1512. Number of Good Pairs
- Colab运行沐神《动手学深度学习》:ImportError: cannot import name ‘_check_savefig_extra_args‘ from ‘matplotlib.back
- python3 英文字母单数转复数
- NOIP 考后欢乐赛 T2 中国象棋
- Weakly Supervised Deep Learning for Thoracic DiseaseClassification and Localization on Chest X-rays
- PS4怪物猎人世界服务器稳定吗,玩一把最近特火的《怪物猎人 世界》,差点把PS4主机给砸了!...
- 戏说计算机发展史,散文:戏说计算机二级考试
- 分开了,伤心了,后悔了
- java struts2 漏洞_Struts2漏洞利用
- 《笑着离开惠普》读书笔记之人性化管理的典范
- AE开发基础实验二(空间查询之按属性查询)
- 解决谷歌安装插件失败问题
- python给每个key添加数据_Python数据分析之逐块读取文本并累加key的次数