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的理解.特将相关内容进行整理. ...
随机推荐
- 使用Keil语言的嵌入式C编程教程(上)
使用Keil语言的嵌入式C编程教程(上) Embedded C Programming Tutorial with Keil Language Embedded System 嵌入式系统是指以单片机为 ...
- halcon——缺陷检测常用方法总结(光度立体)
引言 机器视觉中缺陷检测分为一下几种: blob+特征(官方示例surface_scratch.hdev) blob+差分+特征(官方示例pcb_inspection.hdev) 光度立体 特征训练 ...
- 用Redis实现签到功能
一.场景 在很多时候我们会遇到用户签到的场景,每天用户进入应用时,需要获取用户当天的签到状态,如果没签到,用户可以进行签到,并且得到相关的奖励.我们可能需要每天的签到情况,必要的时候可能还需要统计一下 ...
- springboot2.x整合tkmapper
springboot整合tkmapper 1.导入pom依赖 1.1 导入springboot的parent依赖 <parent> <artifactId>spring-boo ...
- P2365 任务安排
题目描述 n 个任务排成一个序列在一台机器上等待完成(顺序不得改变),这 n 个任务被分成若干批,每批包含相邻的若干任务. 从零时刻开始,这些任务被分批加工,第 i 个任务单独完成所需的时间为 ti ...
- 如果你这么去理解HashMap就会发现它真的很简单
Java中的HashMap相信大家都不陌生,也是大家编程时最常用的数据结构之一,各种面试题更是恨不得掘地三尺的去问HashMap.HashTable.ConcurrentHashMap,无论面试题多么 ...
- Redundant Paths 分离的路径
Redundant Paths 分离的路径 题目描述 为了从F(1≤F≤5000)个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了被迫走某一条路,所以她 ...
- LCD1602液晶显示模块的单片机驱动深入详解之硬件篇
(本文以HD44780主控芯片的LCD1602为蓝本进行描述,其中的截图也来自HD44780数据手册,用户可自行搜索其datasheet,有部分整理网上的,但绝对要比你看到的要深入得多) 一.接口 L ...
- B站英文教学视频的字幕获取 学习必看!
前言 最近在B站看一些纯英文的课程,视频课程有的是纯中文字幕的,有的是纯英文字幕的.由于英文的重要性,一份字幕的文档在我们观看后,留着日后粗略再读是很有益处的.但是为了得到这个英文字幕走了许多弯路.最 ...
- 为什么switch里的case没有break不行
前言 一个小姐姐拿着一个switch的选择题来问我. 之所以这么笃定地回答这个问题,并不是我知道其中原理,而是之前在一个群里,有人问了同类型的问题,我瞥了一眼记住了答案,所以才依葫芦画瓢. 小姐姐接着 ...