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的理解.特将相关内容进行整理. ...
随机推荐
- TensorFlow Keras API用法
TensorFlow Keras API用法 Keras 是与 TensorFlow 一起使用的更高级别的作为后端的 API.添加层就像添加一行代码一样简单.在模型架构之后,使用一行代码,可以编译和拟 ...
- HLS后端示例
HLS后端示例 TVM支持带有SDAccel的Xilinx FPGA板.这是有关如何将TVM部署到AWS F1 FPGA实例的文档. 此功能仍处于试验阶段.暂时无法使用SDAccel部署端到端神经网络 ...
- VB 老旧版本维护系列---迷之集合- ArrayList
迷之集合- ArrayList '定义一个字符串 Dim dataType_ImageStr As String = "2023,2091,2092,2096,2212" '将字符 ...
- 单点突破:MySQL之日志
前言 开发环境:MySQL5.7.31 日志是 mysql 数据库的重要组成部分,记录着数据库运行期间各种状态信息.若数据库发生故障,可通过不同日志记录恢复数据库的原来数据.因此实际上日志系统直接决定 ...
- UF_VEC 向量相关
Open C UF_VEC2_addUF_VEC2_affine_combUF_VEC2_ask_perpendicularUF_VEC2_componentsUF_VEC2_convex_combU ...
- Java显式锁
Java 显式锁. 一.显式锁 什么是显式锁? 由自己手动获取锁,然后手动释放的锁. 有了 synchronized(内置锁) 为什么还要 Lock(显示锁)? 使用 synchronized 关键字 ...
- 听说你还不知道Java代码是怎么运行的?
作为一名Java程序员,我们需要知道Java代码是怎么运行的.最近复习了深入理解Java虚拟机这本书,做了一下笔记,希望对大家有帮助,如果有不正确的地方,欢迎提出,感激不尽. java 代码运行主要流 ...
- 【题解】Luogu P2327 [SCOI2005]扫雷
Luogu P2327 [SCOI2005]扫雷 Description 相信大家都玩过扫雷的游戏.那是在一个n*m的矩阵里面有一些雷,要你根据一些信息找出雷来.万圣节到了,"余" ...
- 台达PLC开发笔记(一):台达PLC连接介绍,分别使用485、网口与台达PLC建立连接
前言 台达AS系列,型号为AS322P. 物理设备连接 使用WPL Editor连接PLC 使用RS485口当作RS232口连接PLC 注意: ...
- 为什么excel里面有的数据用CTRL+F,搜索搜不到?