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示例代码的更多相关文章

  1. 基于注解的Spring AOP示例

    基于注解的Spring AOP示例 目录 在XML配置文件中开启 @AspectJ 支持 声明切面及切入点 声明通知 测试 结语 在XML配置文件中开启 @AspectJ 支持 要使用Spring的A ...

  2. Spring温故而知新 – Spring AOP

    AOP的相关专业术语 通知(Advice):定义在连接点做什么 Spring中通知类型:前置通知,后置通知,返回通知,异常通知,环绕通知 连接点(JoinPoint):程序执行过程中拦截的点,Spin ...

  3. Spring AOP 知识整理

    通过一个多月的 Spring AOP 的学习,掌握了 Spring AOP 的基本概念.AOP 是面向切面的编程(Aspect-Oriented Programming),是基于 OOP(面向对象的编 ...

  4. Spring AOP学习笔记01:AOP概述

    1. AOP概述 软件开发一直在寻求更加高效.更易维护甚至更易扩展的方式.为了提高开发效率,我们对开发使用的语言进行抽象,走过了从汇编时代到现在各种高级语言繁盛之时期:为了便于维护和扩展,我们对某些相 ...

  5. Spring Aop 应用实例与设计浅析

    0.代码概述 代码说明:第一章中的代码为了突出模块化拆分的必要性,所以db采用了真实操作.下面代码中dao层使用了打印日志模拟插入db的方法,方便所有人运行demo. 1.项目代码地址:https:/ ...

  6. Spring 学习——Spring AOP——AOP配置篇Advice(有参数传递)

    声明通知Advice 配置方式(以前置通知为例子) 方式一 <aop:config> <aop:aspect id="ikAspectAop" ref=" ...

  7. Spring AOP介绍及源码分析

    转自:http://www.uml.org.cn/j2ee/201301102.asp 软件开发经历了从汇编语言到高级语言和从过程化编程到面向对象编程:前者是为了提高开发效率,而后者则使用了归纳法,把 ...

  8. Spring AOP和事务的相关陷阱

    1.前言 2.嵌套方法拦截失效 2.1 问题场景 2.2 解决方案 2.3 原因分析 2.3.1 原理 2.3.2 源代码分析 3.Spring事务在多线程环境下失效 3.1 问题场景 3.2 解决方 ...

  9. 简单理解Spring之IOC和AOP及代码示例

    Spring是一个开源框架,主要实现两件事,IOC(控制反转)和AOP(面向切面编程). IOC 控制反转,也可以称为依赖倒置. 所谓依赖,从程序的角度看,就是比如A要调用B的方法,那么A就依赖于B, ...

随机推荐

  1. ikanalyzer分词,计算信息熵排序分词结果

    因需求,现需分词接口,故记录之. 1.需要依赖: <!-- https://mvnrepository.com/artifact/com.janeluo/ikanalyzer --> &l ...

  2. CSS学习笔记03 CSS层叠性、继承性、特殊性

    层叠性 所谓层叠性是指多种CSS样式的叠加,也就是说后面设置的样式会层叠(覆盖)之前的样式,层叠性的前提是CSS的选择器的优先级相同,例如,当使用内嵌式CSS样式表定义<p>标记字号大小为 ...

  3. Mock session,cookie,querystring in ASB.NET MVC

    写测试用例的时候经常发现,所写的功能需要Http上下文的支持(session,cookie)这类的. 以下介绍2种应用场景. 用于控制器内Requet获取参数 控制器内的Requet其实是控制器内的属 ...

  4. MRO + super面试题(详解)

    class Init(object): def __init__(self, v): print("init") self.val = v class Add2(Init): de ...

  5. Django工程的建立以及小网站的编写

    这篇博文会详细的介绍如何创建django工程,介绍我如何做了第一个网站.本文基于windows7安装了python2.7.12,Django1.8.18(LTS)版.采用的IDE为pycharm.建议 ...

  6. AngularJS学习之 登录表单 清爽验证(边学边更新)

    注册过程的确好多需要验证的,但是注册成功之后的登录就简单多了, 只要用户 输入 用户名和密码, ajax向后台提交登录请求, 根据返回的结果确定用户名或者密码是否正确即可登录. 所以这个登录表单的验证 ...

  7. TensorFlow数据读取方式:Dataset API

    英文详细版参考:https://www.cnblogs.com/jins-note/p/10243716.html Dataset API是TensorFlow 1.3版本中引入的一个新的模块,主要服 ...

  8. Android 机器人项目Pepper_Android

    github地址:https://github.com/ohwada/Pepper_Android

  9. Expo大作战(二十三)--expo中expo kit 高级属性(没干货)

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...

  10. 【前端】NodeJs包管理工具NPM

    NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS部署上的很多问题. 测试是否安装成功,出现版本提示表示安装成功. npm -v NPM常用命令 官方文档:https://www.npm ...