Spring AOP示例代码
public interface CustomerDao {
public void save();
public void update();
}
public class CustomerDaoImpl implements CustomerDao {
public void save() {
// 模拟异常
// int a = 10/0;
System.out.println("保存客户...");
}
public void update() {
System.out.println("修改客户...");
}
}
import org.aspectj.lang.ProceedingJoinPoint; /**
* 切面类:切入点 + 通知
* @author Administrator
*/
public class MyAspectXml { /**
* 通知(具体的增强)
*/
public void log(){
System.out.println("记录日志...");
} /**
* 最终通知:方法执行成功或者出现异常,都会执行
*/
public void before(){
System.out.println("before通知...");
}
/**
* 最终通知:方法执行成功或者出现异常,都会执行
*/
public void after(){
System.out.println("after通知...");
} /**
* 方法执行之后,执行后置通知。程序出现了异常,后置通知不会执行的。
*/
public void afterReturn(){
System.out.println("后置通知...");
} /**
* 环绕通知:方法执行之前和方法执行之后进行通知,默认的情况下,目标对象的方法不能执行的。需要手动让目标对象的方法执行
*/
public void around(ProceedingJoinPoint joinPoint){
System.out.println("环绕通知1...");
try {
// 手动让目标对象的方法去执行
joinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("环绕通知2...");
} }
<?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" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --> <!-- 配置客户的dao -->
<bean id="customerDao" class="com.itheima.demo3.CustomerDaoImpl"/> <!-- 编写切面类配置好 -->
<bean id="myAspectXml" class="com.itheima.demo3.MyAspectXml"/> <!-- 配置AOP -->
<aop:config>
<aop:aspect ref="myAspectXml">
<!-- <aop:before method="log" pointcut="execution(public void com.itheima.demo3.CustomerDaoImpl.save())"/> --> <!-- 配置最终通知
<aop:after method="after" pointcut="execution(public void com.itheima.demo3.CustomerDaoImpl.save())"/>
-->
<aop:before method="before" pointcut="execution(public void com.itheima.demo3.CustomerDaoImpl.save())"/>
<aop:after method="after" pointcut="execution(public void com.itheima.demo3.CustomerDaoImpl.save())"/>
<!-- <aop:after-returning method="afterReturn" pointcut="execution(public void com.itheima.demo3.CustomerDaoImpl.save())"/> --> <!-- 环绕通知
<aop:around method="around" pointcut="execution(public void com.itheima.demo3.CustomerDaoImpl.save())"/> -->
</aop:aspect>
</aop:config> </beans>
Spring AOP示例代码的更多相关文章
- 基于注解的Spring AOP示例
基于注解的Spring AOP示例 目录 在XML配置文件中开启 @AspectJ 支持 声明切面及切入点 声明通知 测试 结语 在XML配置文件中开启 @AspectJ 支持 要使用Spring的A ...
- Spring温故而知新 – Spring AOP
AOP的相关专业术语 通知(Advice):定义在连接点做什么 Spring中通知类型:前置通知,后置通知,返回通知,异常通知,环绕通知 连接点(JoinPoint):程序执行过程中拦截的点,Spin ...
- Spring AOP 知识整理
通过一个多月的 Spring AOP 的学习,掌握了 Spring AOP 的基本概念.AOP 是面向切面的编程(Aspect-Oriented Programming),是基于 OOP(面向对象的编 ...
- Spring AOP学习笔记01:AOP概述
1. AOP概述 软件开发一直在寻求更加高效.更易维护甚至更易扩展的方式.为了提高开发效率,我们对开发使用的语言进行抽象,走过了从汇编时代到现在各种高级语言繁盛之时期:为了便于维护和扩展,我们对某些相 ...
- Spring Aop 应用实例与设计浅析
0.代码概述 代码说明:第一章中的代码为了突出模块化拆分的必要性,所以db采用了真实操作.下面代码中dao层使用了打印日志模拟插入db的方法,方便所有人运行demo. 1.项目代码地址:https:/ ...
- Spring 学习——Spring AOP——AOP配置篇Advice(有参数传递)
声明通知Advice 配置方式(以前置通知为例子) 方式一 <aop:config> <aop:aspect id="ikAspectAop" ref=" ...
- Spring AOP介绍及源码分析
转自:http://www.uml.org.cn/j2ee/201301102.asp 软件开发经历了从汇编语言到高级语言和从过程化编程到面向对象编程:前者是为了提高开发效率,而后者则使用了归纳法,把 ...
- Spring AOP和事务的相关陷阱
1.前言 2.嵌套方法拦截失效 2.1 问题场景 2.2 解决方案 2.3 原因分析 2.3.1 原理 2.3.2 源代码分析 3.Spring事务在多线程环境下失效 3.1 问题场景 3.2 解决方 ...
- 简单理解Spring之IOC和AOP及代码示例
Spring是一个开源框架,主要实现两件事,IOC(控制反转)和AOP(面向切面编程). IOC 控制反转,也可以称为依赖倒置. 所谓依赖,从程序的角度看,就是比如A要调用B的方法,那么A就依赖于B, ...
随机推荐
- Jquery停止动画
stop方法 第一个参数:是否清除动画队列 true | false 第二个参数:是否跳转到动画最终效果 true | false 使用stop()方法的技巧 当下拉菜单和手风琴产生动画队列的问题 ...
- 无责任Windows Azure SDK .NET开发入门(二):使用Azure AD 进行身份验证
<編者按>本篇为系列文章,带领读者轻松进入Windows Azure SDK .NET开发平台.本文为第二篇,将教导读者使用Azure AD进行身分验证.也推荐读者阅读无责任Windows ...
- 如何给oracle账户解锁
在创建数据库时,已经为SYS等4个账户设定了口令,其中SYS与SYSTEM具有管理员权限,在SQL*Plus工具中使用SYSTEM账户登录Oracle数据库. 1.通过数据字典dba_users,查看 ...
- order by与索引(转载)
order by与索引 ORDER BY 通常会有两种实现方法,一个是利用有序索引自动实现,也就是说利用有序索引的有序性就不再另做排序操作了.另一个是把结果选好之后再排序. 用有序索引这种,当然是 ...
- 简单的SpringMVC经典案例
主题:构建一个基于SpringMVC的HelloWord Web 项目 目的:快速体验什么是SpringMVC 方案: 1.创建工程,命名:SpringMVC 2.导包 3.在SRC下添加spring ...
- zookeeper 知识点汇总
目录 Zookeeper 是什么 Zookeeper 树状模型 Zookeeper 集群结构 如何使用 ZooKeeper 运行 Zookeeper 步骤1 修改 ZooKeeper 配置文件 步骤 ...
- IntelliJ idea 备份与恢复
为了防止突然断电或者电脑突然关机导致idea恢复出厂设置,需要定期备份配置. 一.备份 File---Export Settings 将settings.jar 文件导入到C:\Users\xutin ...
- Difference between nn.softmax & softmax_cross_entropy_with_logits & softmax_cross_entropy_with_logits_v2
nn.softmax 和 softmax_cross_entropy_with_logits 和 softmax_cross_entropy_with_logits_v2 的区别 You have ...
- JSP内置对象——session对象
举个购物流程的例子: 这整个购物过程,它是属于一次回话.那么这个session是保存在服务器内存当中,并且它保存着不同用户对应的session,一个用户对应一个session.看下面这幅图: 从图中可 ...
- Fiddler抓包使用教程-会话图标
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/72933882 本文出自[赵彦军的博客] 使用fiddler抓包时,会看到左侧按照顺 ...