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. EWS 邮件提醒

    摘要 之前做的邮件提醒的项目,最近需要优化,由于使用了队列,但即时性不是特别好,有队列,就会出现先后的问题,最近调研了exchange 流通知的模式,所以想使用流通知模式和原先的拉取邮件的方法结合,在 ...

  2. SpringBoot,Vue前后端分离开发首秀

    需求:读取数据库的数据展现到前端页面 技术栈:后端有主要有SpringBoot,lombok,SpringData JPA,Swagger,跨域,前端有Vue和axios 不了解这些技术的可以去入门一 ...

  3. LINQ to Objects系列(3)深入理解Lambda表达式

    Lambda表达式是学好LINQ很重要的一个知识点,后面的LINQ查询中会大量地使用到Lambda表达式.这篇文章从以下几点进行总结. 1,Lambda表达式的前世今生 2,Lambda表达式的实际运 ...

  4. JavaOne 2016主旨演讲畅谈Java近期及远期规划

    在 JavaOne 2016 主题演讲开场,来自 Oracle 的 Java 产品管理负责人 Sharat Chander 指出 Java 盛行于个人和工作的日常生活各个领域,无论是大数据.物联网甚至 ...

  5. HTML自定义标签与标签自定义属性

    大部分浏览器支持自定义HTML标签和为标准标签自定义属性,而且很多浏览器对这两种自定义行为的支持都很直接了当. 自定义HTML标签 在firefox.chrome这种现代浏览器里,自定义标签很简单,就 ...

  6. forever 启动nodejs

    forever可以看做是一个nodejs的守护进程,能够启动,停止,重启我们的app应用. 1.全局安装 forever // 记得加-g,forever要求安装到全局环境下 sudo npm ins ...

  7. element-ui Tag、Dialog组件源码分析整理笔记(五)

    Tag 标签组件 <script> export default { name: 'ElTag', props: { text: String, closable: Boolean, // ...

  8. 【代码笔记】iOS-JSONKit的使用

    一,工程图. 二,代码. #import "RootViewController.h" //为JSONKit添加头文件 #import "JSONKit.h" ...

  9. List中Add()与AddAll()的区别

    我们在开发过程中经常会使用到List<Object> list=new ArrrayList<>(); 这个集合,Object 也可以是String.Integer等. 当我们 ...

  10. 移动端开发时默认样式reset

    /* http://meyerweb.com/eric/tools/css/reset/ v2.0 | 20110126 License: none (public domain) */ html, ...