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. [android] fragment的动态创建

    在一个商业软件中,会有很多的界面,如果没一个界面对应一个activity,那么activity会非常的多,清单文件也会非常的乱,谷歌在android3.0以后引入了新的概念叫fragment frag ...

  2. 导航栏pop拦截

    一.新建一个分类 二.导入分类头文件 三.需要拦截的地方实现方法   - (BOOL)navigationShouldPopTwo  即可 .h #import <UIKit/UIKit.h&g ...

  3. REST风格的应用程序实现

    莫笑我老土,因为我确实是最近才听说REST风格的,以前就是觉得 /category/product/pid 这样的地址非常的漂亮,但是那只是表象罢了,了解深入以后,发现必须有一个客户端的Ajax En ...

  4. java自动生成代码

    看到这个标题,如果你以为真的可以完全自动生成,那你就太Naive啦 我这里想介绍的是,利用模板生成我们需要的代码 一.模板 首先来说说模板,常见的java模板有:FreeMarker.Velocity ...

  5. Spring Boot使用layui的字体图标时无法正常显示 解决办法

    在html文件使用字体图标并且预览时正常,但是启动工程后显示不正常,浏览器调试界面显示字体文件无法decode: Failed to decode downloaded font: xxxxx 如图所 ...

  6. Vue: ES6常用语法

    ES6 模板字符串:  ~ ${变量名}~ <div id="app"></div> <script> /* 找到对应id */ let ite ...

  7. layui的checkbox示例

    1.html页面: var isSkipcheckbox = ''; if (appOptions.isSkip != "0") { isSkipcheckbox = 'check ...

  8. CSS中文乱码解决方法

    原文链接:http://caibaojian.com/css-unicode.html 我的CSS里面有一个content用到了中文,作用主要是在前端日报文章中显示出“网页链接”这四个字,然而打开百度 ...

  9. JS中判断数据类型的几种方法

    1⃣️首先我们来了解一下js中的数据类型 1.基本数据类型:Undefined.Null.Boolean.Number.String(值类型) 2.复杂数据类型:Object(引用类型) (值类型和引 ...

  10. 基于react的标准form+table页面的请求流程图

    componentDidMount直接走handleSearch而不走loadData,是为了当form有默认初始值的时候也能适用! --------------------2018.2.7新增--- ...