<?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. rt: Unknown command 'PATH='

  2. eclipse中 com.sun.image.codec.jpeg.JPEGCodec 无法编译通过问题

    在Eclipse中处理图片,需要引入两个包:import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEG ...

  3. PowerDesigner将PDM导出生成WORD文档--温习老知识

    转:http://www.cnblogs.com/wudiwushen/archive/2010/05/13/1734812.html 今天的温习老知识,是如何将一个PD设计的PDM来导出WORD文档 ...

  4. MYSQL删除以数字开头的字段

    例子: // 删除以0开头的字段 DELETE FROM `week_energy_copy` WHERE openid like '0%'; // 删除以数字开头的字段 DELETE FROM `w ...

  5. shell 执行jar 的命令

    #!/bin/sh ############## #判断是否程序已启动 jappname='Test' mainclasspath="com.company.commontest.test& ...

  6. 自定义UITableView的Seperator

    在默认配置中 ,UITableView的Cell之间的Seperator左边总是空出一块,即使在Storyboard中设置为0 ,也没有效果 需要在代码中进行配置,在ViewController中实现 ...

  7. iOS屏蔽高频点击技巧

    例如高频率点击一个按钮或者TableViewCell,会造成功能多次重复执行,在异步网络请求时候或者多线程时候,造成的问题尤其明显. 解决方法: 声明一个属性self.actionWorking ,标 ...

  8. JavaScript的Date 方法

    js中Date 方法 Date (对象) Date 对象能够使你获得相对于国际标准时间(格林威治标准时间,现在被称为 UTC-Universal Coordinated Time)或者是 Flash ...

  9. list,set,map,数组间的相互转换

    1.list转set Set set =  new  HashSet( new  ArrayList()); 2.set转list List list =  new  ArrayList( new   ...

  10. spark-streaming-kafka包源码分析

    转载请注明原创地址 http://www.cnblogs.com/dongxiao-yang/p/5443789.html 最近由于使用sparkstreaming的同学需要对接到部门内部的的kafk ...