<?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. bzoj1211

    prufer码水题(n-2)!/[(d1-1)!*(d2-1)!*…*(dn-1)!] ..] of longint; x,n,i,j,s:longint; ans:int64; begin read ...

  2. xml中1字节的UTF-8序列的字节1无效([字符编码]Invalid byte 1 of 1-byte UTF-8 sequence终极解决方案)

    今天在eclipse中编写pom.xml文件时,注释中的中文被eclipse识别到错误:Invalid byte 1 of 1-byte UTF-8 sequence,曾多次遇到该问题,问题的根源是: ...

  3. Kafka Topic Partition Replica Assignment实现原理及资源隔离方案

    本文共分为三个部分:   Kafka Topic创建方式 Kafka Topic Partitions Assignment实现原理 Kafka资源隔离方案   1. Kafka Topic创建方式 ...

  4. 程序员欢呼:微软Bing开始支持搜索源码、可直接运行!

    日常生活中,程序员们经常会遇见这样那样的问题,比如忘记了代码该怎么写,又或者需要实现一些特殊的算法和功能.这时候,你就可以去找微软 Bing 帮忙啦! 微软最近联合 HackerRank 一起研发了一 ...

  5. 如何优雅的输出PHP调试信息

    经常因为出现紧急bug而被老板骂的同事,为了更快的修复而直接利用线上的错误环境现场debug,并直接在页面上echo和dump.结果被老板发现了,又是一通臭骂.那么有没有什么办法更优雅的输出PHP调试 ...

  6. Eclipse的下载和安装

    下载 Android开发首选Eclipse for Android Developers版本,里面集成了ADT(Android Development Tools). 下载页面:http://www. ...

  7. php将SQL查询结果赋值给变量

    2012-03-25 12:12 a786013819 | 分类:数据库DB | 浏览1393次 $sql = "select field1 from pre_common_member_p ...

  8. Counting - SGU 117(快速幂)

    题目大意:求下面N个数里面有多少个数的M次方能整除K 代码如下: ======================================================== #include&l ...

  9. Parallel.Foreach的全部知识要点【转】

    简介 当需要为多核机器进行优化的时候,最好先检查下你的程序是否有处理能够分割开来进行并行处理.(例如,有一个巨大的数据集合,其中的元素需要一个一个进行彼此独立的耗时计算). .net framewor ...

  10. dispatch_get_current_queue 废弃

    由于iOS7以后 dispatch_get_current_queue 被废弃,所以需要寻找一个替代的方案. 发现 dispatch_get_current_queue 并没有字面上那么简单. 这个函 ...