Spring总结之AOP
一、Spring AOP简介(百度百科)
面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是 Spring
框架中的一个重要内容。利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度
降低,提高程序的可重用性,同时提高了开发的效率。
主要的功能是:日志记录,性能统计,安全控制,事务处理,异常处理等等。
二、Spring AOP实例
(1,前置通知;2,后置通知;3,环绕通知;4,返回通知;5,异常通知;)
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 业务逻辑bean -->
<bean id="userService" class="top.ruandb.service.impl.UserServiceImpl" ></bean>
<!-- 自定义的切面 -->
<bean id="userServiceAspect" class="top.ruandb.advice.UserServiceAspect"></bean>
<!-- AOP配置 -->
<aop:config>
<!-- 定义切面 -->
<aop:aspect id="userServiceAspect" ref="userServiceAspect">
<!-- 定义切点,表达式: -->
<aop:pointcut expression="execution(* top.ruandb.service.*.*(..))" id="businessService"/>
<!-- 前置通知,在方法执行之前通知 -->
<aop:before method="doBefore" pointcut-ref="businessService"/>
<!-- 后置通知:在方法执行之后通知 -->
<aop:after method="doAfter" pointcut-ref="businessService"/>
<!-- 环绕通知:在方法前后环绕 -->
<aop:around method="doAround" pointcut-ref="businessService"/>
<!-- 返回通知:方法返回后通知 -->
<aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>
<!-- 异常通知:出现异常后通知 -->
<aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>
</aop:aspect>
</aop:config> </beans>
//切面
public class UserServiceAspect { public void doBefore(JoinPoint jp) {
System.out.println(jp.getTarget().getClass().getName()+"前置通知:添加用户之前");
}
public void doAfter(JoinPoint jp) {
System.out.println(jp.getTarget().getClass().getName()+"后置通知:添加用户之后");
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println(pjp.getTarget().getClass().getName()+"环绕通知:环绕前");
Object obj = pjp.proceed();
System.out.println(pjp.getTarget().getClass().getName()+"环绕通知:环绕后");
return obj;
}
public void doAfterReturning(JoinPoint jp) {
System.out.println(jp.getTarget().getClass().getName()+"返回通知:返回通知");
}
public void doAfterThrowing(JoinPoint jp,Throwable ex) {
System.out.println(jp.getTarget().getClass().getName()+"异常通知:程序异常了"+ex.getMessage());
}
} public class SpringTest {
ApplicationContext ac ;
@Before
public void setUp() {
ac = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@After
public void tearDown() {
ac = null;
} @Test
public void test1() {
UserServiceI userService = (UserServiceI) ac.getBean("userService") ;
userService.addUser("rdb"); }
} 结果:
top.ruandb.service.impl.UserServiceImpl前置通知:添加用户之前
top.ruandb.service.impl.UserServiceImpl环绕通知:环绕前
添加用户rdb
top.ruandb.service.impl.UserServiceImpl返回通知:返回通知
top.ruandb.service.impl.UserServiceImpl环绕通知:环绕后
top.ruandb.service.impl.UserServiceImpl后置通知:添加用户之后 结果(异常):
top.ruandb.service.impl.UserServiceImpl前置通知:添加用户之前
top.ruandb.service.impl.UserServiceImpl环绕通知:环绕前
添加用户rdb
top.ruandb.service.impl.UserServiceImpl异常通知:程序异常了/ by zero
top.ruandb.service.impl.UserServiceImpl后置通知:添加用户之后
Spring总结之AOP的更多相关文章
- Spring 3.0 AOP (一)AOP 术语
关于AOP.之前我已写过一个系列的随笔: <自己实现简单的AOP>,它的关注点在于实现.实现语言是C#,实现方式为 自定义实现 RealProxy 抽象类.重写Invoke方法,以便进行方 ...
- Spring系列之AOP实现的两种方式
AOP常用的实现方式有两种,一种是采用声明的方式来实现(基于XML),一种是采用注解的方式来实现(基于AspectJ). 首先复习下AOP中一些比较重要的概念: Joinpoint(连接点):程序执行 ...
- springMVC+MyBatis+Spring 整合(4) ---解决Spring MVC 对AOP不起作用的问题
解决Spring MVC 对AOP不起作用的问题 分类: SpringMVC3x+Spring3x+MyBatis3x myibaits spring J2EE2013-11-21 11:22 640 ...
- 【转】spring - ioc和aop
[转]spring - ioc和aop 1.程序中为什么会用到spring的ioc和aop 2.什么是IOC,AOP,以及使用它们的好处,即详细回答了第一个问题 3.原理 关于1: a:我们平常使用对 ...
- Spring核心框架 - AOP的原理及源码解析
一.AOP的体系结构 如下图所示:(引自AOP联盟) 层次3语言和开发环境:基础是指待增加对象或者目标对象:切面通常包括对于基础的增加应用:配置是指AOP体系中提供的配置环境或者编织配置,通过该配置A ...
- Spring中的AOP
什么是AOP? (以下内容来自百度百科) 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种 ...
- Spring IOC及AOP学习总结
一.Spring IOC体系学习总结: Spring中有两个容器体系,一类是BeanFactory.还有一类是ApplicationContext.BeanFactory提供了基础的容器功能.Appl ...
- Spring自学教程-AOP学习(五)
Spring中的AOP 一.概述 (一)基本概念 1.什么是AOP? 面向方面编程.所谓方面即是指日志.权限.异常处理.事务处理等. 2.AOP的3个关键概念 (1)切入点(Pointc ...
- Spring 3.0 Aop 入门
关于Aop的原理,动态代理,反射,之类的底层java技术网上搜一堆一堆的..我就不多说了,主要说在spring上使用aop的方法. 首先不得不说一下的就是,spring aop的支持需要外部依赖包: ...
- J2EE进阶(十四)超详细的Java后台开发面试题之Spring IOC与AOP
J2EE进阶(十四)超详细的Java后台开发面试题之Spring IOC与AOP 前言 搜狐畅游笔试题中有一道问答题涉及到回答谈谈对Spring IOC与AOP的理解.特将相关内容进行整理. ...
随机推荐
- cuGraph-GPU图形分析
cuGraph-GPU图形分析 所述RAPIDS cuGraph库是GPU的集合加速图形算法,在GPU DataFrames中发现过程数据.cuGraph的愿景是使图分析无处不在,以至于用户只是根据分 ...
- Relay张量集成
Relay张量集成 Introduction NVIDIA TensorRT是一个用于优化深度学习推理的库.这种集成将尽可能多地减轻从中继到TensorRT的算子,在NVIDIA GPU上提供性能提升 ...
- Linux基础_vim命令
简介:Vim是从 vi 发展出来的一个文本编辑器.代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用. vi/vim 共分为三种模式,分别是命令模式(Command mode)也叫 ...
- NCF 如何导入Excel数据
简介 学了上一节的WebApi之后,我们会发现一片新天地 本节跟大家聊一聊,如何把本地的Excel数据导入到NCF中 仓库地址:https://github.com/NeuCharFramework/ ...
- 【NX二次开发】三点画圆,三角形外心,已知三点求圆心
已知P1.P2.P3,求点O 算法:三点不在一条直线上时,通过连接任意两点,作中垂线.任意两条中垂线的交点是圆心.
- 《四大点,搞懂Redis到底快在哪里?》
一.开发语言 二.纯内存访问 三.单线程 四.非阻塞多路I/O复用机制 前言 Redis是一种基于键值对(Key-Value)的NoSQL数据库 ,Redis的Value可以由String,hash, ...
- java并发编程实战之线程安全性(一)
1.1什么是线程安全性 要对线程安全性给出一个确切的定义是非常复杂的.最核心的概念就是正确性.正确性:某个类的行为与其规范完全一致.在良好的规范中通常会定义各种不变性条件来约束对象的状态,以及定义各种 ...
- 【模拟7.27】题(liu_runda的神题)(卡特兰数,组合数)
考场的SB经验不再分享 case 0: 一道组合计数的水题,具体不再讲可以看以前的相似题 case 1: 很明显的卡特兰计数,我们把长度为n的序列看成01串 关于卡特兰计数的详细的讲解 由此可知我们需 ...
- 【题解】[LuoguP3503]「BZOJ2086」[POI2010] Blocks
题目描述 给出N个正整数a[1..N],再给出一个正整数k,现在可以进行如下操作:每次选择一个大于k的正整数a[i],将a[i]减去1,选择a[i-1]或a[i+1]中的一个加上1.经过一定次数的操作 ...
- NOIP模拟测试10「大佬·辣鸡·模板」
大佬 显然假期望 我奇思妙想出了一个式子$f[i]=f[i-1]+\sum\limits_{j=1}^{j<=m} C_{k \times j}^{k}\times w[j]$ 然后一想不对得容 ...