<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<!--
1、引入AOP的命名空间
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
2、目标类
3、切面
4、拦截器 由spring内部实现
5、aop的配置
-->
<bean id="personDao" class="cn.test.aop.PersonDaoImpl"></bean>
<bean id="transaction" class="cn.test.aop.Transaction"></bean> <!-- aop配置 -->
<aop:config>
<!-- 切入点表达式
expression
确定哪个类可以生成代理对象
id 唯一标识 -->
<aop:pointcut expression="execution(* cn.test.aop.PersonDaoImpl.*(..))" id="perform"/>
<!-- 切面 -->
<aop:aspect ref="transaction">
<!-- 前置通知
* 在目标方法执行之前
-->
<aop:before method="beginTransaction" pointcut-ref="perform"/>
<!-- 后置通知
* 在目标方法执行之后
* 可以根据returning获取目标方法的返回值
* 如果目标方法遇到异常,该通知不执行
-->
<aop:after-returning method="commit" pointcut-ref="perform" returning="val"/>
<!-- 前置通知和后置通知只能在目标方法文中添加内容,但是控制不了目标方法的执行 -->
<!--
最终通知
* 在目标方法执行之后
* 无论目标方法是否遇到异常,都执行
* 经常做一些关闭资源
-->
<aop:after method="finallyMethod" pointcut-ref="perform"/>
<!--
异常通知
目的就是为了获取目标方法抛出的异常
-->
<aop:after-throwing method="exceptionMethod" throwing="ex" pointcut-ref="perform"/> </aop:aspect>
</aop:config> </beans>
 Transaction.java
1 public class Transaction {
//joinPoint 连接点信息
public void beginTransaction(JoinPoint joinPoint){
joinPoint.getArgs();//获取方法的参数
String methodName = joinPoint.getSignature().getName();
System.out.println(methodName);
System.err.println("begin trans action");
}
public void commit(JoinPoint joinPoint,Object val){
System.err.println("commit");
} public void finallyMethod(){
System.err.println("finally method");
} public void exceptionMethod(Throwable ex){
System.err.println(ex.getMessage());
} /**
* 环绕通知
* 能控制目标方法的执行
*/ public void aroundMethod(ProceedingJoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
if("addPerson".equals(methodName)){ }
}

测试:

 /**
* 原理
* * 加载配置文件,启动spring容器
* * spring容器为bean创建对象
* * 解析aop的配置,会解析切入点表达式
* * 看纳入spring管理的那个类和切入点表达式匹配,如果匹配则会为该类创建代理对象
* * 代理对象的方法体的形成就是目标方法+通知
* * 客户端在context.getBean时,如果该bean有代理对象,则返回代理对象,如果没有代理对象则返回原来的对象
* 说明:
* 如果目标类实现了接口,则spring容器会采用jdkproxy,如果目标类没有实现接口,则spring容器会采用
* cglibproxy
*
* */
@Test
public void doSome(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("cn/test/aop/applicationContext.xml");
PersonDao personDao= (PersonDao) applicationContext.getBean("personDao"); personDao.addPerson(); }

Spring 中各种通知的更多相关文章

  1. Spring中的通知(Advice)和顾问(Advisor)

    在Spring中,目前我学习了几种增强的方式,和大家分享一下 之前的话: 1.AOP  (Aspect  Oriented Programming  面向切面编程) 在软件业,AOP为Aspect O ...

  2. spring中订阅redis键值过期消息通知

    1.首先启用redis通知功能(ubuntu下操作):编辑/etc/redis/redis.conf文件,添加或启用以下内容(过期通知): notify-keyspace-events Ex 或者登陆 ...

  3. Spring中关于AOP的实践之AspectJ方式实现通知

    (本文中如有不当之处,恳请批评指正) AspectJ方式的简化了通知的出现复杂度.但是对配置文件的操作复杂度有了一定的提升 一. 配置通知 package com.xkx.adviceDemo; im ...

  4. spring中的AOP 以及各种通知 配置

    理解了前面动态代理对象的原理之后,其实还是有很多不足之处,因为如果在项目中有20多个类,每个类有100多个方法都需要判断是不是要开事务,那么方法调用那里会相当麻烦. spring中的AOP很好地解决了 ...

  5. spring aop 环绕通知around和其他通知的区别

    前言: spring 的环绕通知和前置通知,后置通知有着很大的区别,主要有两个重要的区别: 1) 目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,而前置和后置通知   是不能决定的,他们只 ...

  6. Spring中文文档

    前一段时间翻译了Jetty的一部分文档,感觉对阅读英文没有大的提高(*^-^*),毕竟Jetty的受众面还是比较小的,而且翻译过程中发现Jetty的文档写的不是很好,所以呢翻译的兴趣慢慢就不大了,只能 ...

  7. Spring AOP 四大通知

    Spring AOP 四大通知 Spring 3.X 以前 1.前置通知,实现  MethodBeforeAdvice 接口,重写 public  void  before(Method  metho ...

  8. Spring 中的 JDBC 事务

    Spring 对 JDBC 的支持 JdbcTemplate 简介 •为了使 JDBC 更加易于使用, Spring 在 JDBC API 上定义了一个抽象层, 以此建立一个 JDBC 存取框架. • ...

  9. [转载] 【Shiro】Apache Shiro架构之实际运用(整合到Spring中)

    写在前面:前面陆陆续续对Shiro的使用做了一些总结,如题,这篇博文主要是总结一下如何将Shiro运用到实际项目中,也就是将Shiro整到Spring中进行开发.后来想想既然要整,就索性把Spring ...

随机推荐

  1. BZOJ1614: [Usaco2007 Jan]Telephone Lines架设电话线

    1614: [Usaco2007 Jan]Telephone Lines架设电话线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 892  Solved: ...

  2. Node.js权威指南 (10) - Node.js中的错误处理与断言处理

    10.1 使用domain模块处理错误 / 272 10.1.1 domain模块概述 / 272 10.1.2 创建并使用Domain对象 / 274 10.1.3 隐式绑定与显式绑定 / 276 ...

  3. (转载)MySQL关键字ORDER BY的使用

    例子: mysql), d_id ), name ), age ), sex ), homeaddr )); // 可以看到首先按照d_id进行升序排列,排列好了之后, // 若d_id字段中有相同的 ...

  4. 导入 from pdfminer.pdfinterp import process_pdf 错误

    >>> from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter>>> from ...

  5. excel小技巧-用于测试用例的编号栏:“获取当前单元格的上一格的值+1”=INDIRECT(ADDRESS(ROW()-1,COLUMN()))+1

    编写用例的时候使用,经常修改用例的时候会需要增加.删除.修改条目,如果用下拉更新数值的方式会很麻烦. 1.使用ctrl下拉,增删移动用例的时候,需要每次都去拉,万一列表比较长,会很麻烦 2.使用ROW ...

  6. Linux学习笔记1——Linux的目录结构

    / 是根目录 ~是主目录 bin 存放二进制可执行文件(Is,cat,mkdir等) boot 存放用于系统引导时使用的各种文件 dev 用于存放设备文件 etc 存放系统配置文件 home 存放所有 ...

  7. stringstream 与空格 (大家讨论一下代码结果的原因)

    #include <iostream> // std::cout, std::endl #include <iomanip> // std::setw #include < ...

  8. NGU-学习笔记(1)-动态添加删除图集

    现在 正在做unity的方向 不得不说我选的是UI方向 Unity中很有名的就是NGUI插件了.今天做了个ngui的简单背包系统.非常简陋..初学着 自己mark下 (1)预览 主要就是个 simpl ...

  9. mysqldump的常用语句及各参数详解

    mysqldump的常用语句及各参数详解 分类: MySQL 2011-01-11 17:55 1368人阅读 评论(0) 收藏 举报 数据库mysql服务器tableinsertdatabase m ...

  10. leetcode72. Edit Distance

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...