http://blog.csdn.net/yerenyuan_pku/article/details/52880558

使用Spring配置文件实现AOP

前面我们已经学会了使用Spring的注解方式实现AOP,现在我们就要学习使用Spring配置文件实现AOP。本文是建立在使用Spring的注解方式实现AOP的细节的案例的基础之上的。 
我们首先将MyInterceptor类的代码修改为:

/*** 切面* @author li ayun**/
@Aspect
public class MyInterceptor { public void doAccessCheck() { System.out.println("前置通知"); } public void doAfterReturning() { System.out.println("后置通知"); } public void doAfter() { System.out.println("最终通知"); } public void doAfterThrowing() { System.out.println("异常通知"); } public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable { System.out.println("进入方法"); Object result = pjp.proceed(); System.out.println("退出方法"); return result; } }
  • 1
  • 2

从上可知MyInterceptor不过就是一个普通的JavaBean。现在若要使用Spring配置文件实现AOP,则须将Spring配置文件的内容修改为:

<?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:context="http://www.springframework.org/schema/context" 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/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <aop:aspectj-autoproxy /> <bean id="personService" class="cn.itcast.service.impl.PersonServiceImpl"></bean> <bean id="aspetbean" class="cn.itcast.service.MyInterceptor"></bean> <aop:config> <aop:aspect id="asp" ref="aspetbean"> <aop:pointcut expression="execution(* cn.itcast.service.impl.PersonServiceImpl.*(..))" id="mycut"/> <aop:before pointcut-ref="mycut" method="doAccessCheck"/> <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/> <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/> <aop:after pointcut-ref="mycut" method="doAfter"/> <aop:around pointcut-ref="mycut" method="doBasicProfiling"/> </aop:aspect> </aop:config> </beans>
  • 1

如果PersonServiceImpl类的代码为:

public class PersonServiceImpl implements PersonService { @Override public void save(String name) { // throw new RuntimeException("我是异常"); System.out.println("我是save()方法"); } @Override public void update(String name, Integer id) { System.out.println("我是update()方法"); } @Override public String getPersonName(Integer id) { System.out.println("我是getPersonName()方法"); return "xxx"; } }
  • 1

那么除了异常通知外,其他通知都将会执行。此时,测试SpringAOPTest类的interceptorTest()方法,会发现Eclipse控制台打印: 
 
如果PersonServiceImpl类的代码为:

public class PersonServiceImpl implements PersonService { @Override public void save(String name) { throw new RuntimeException("我是异常"); // System.out.println("我是save()方法"); } @Override public void update(String name, Integer id) { System.out.println("我是update()方法"); } @Override public String getPersonName(Integer id) { System.out.println("我是getPersonName()方法"); return "xxx"; } }
  • 1

那么,异常通知就会被执行。此时,测试SpringAOPTest类的interceptorTest()方法,会发现Eclipse控制台打印:
 
并且还抛出异常。 
基于Spring配置文件实现AOP,我们就学习到这里。如要查看源码,可点击使用Spring配置文件实现AOP进行下载。

aspectj的切入点语法定义细节

在使用Spring的注解方式实现AOP入门一文中,我们就初步了解了一下aspectj的切入点语法。我们可利用方法签名来编写aspectj的切入点表达式。最典型的切入点表达式是根据方法的签名来匹配各种方法:

  • execution (* cn.itcast.service.impl.PersonServiceImpl.*(..)):匹配PersonServiceImpl类中声明的所有方法。第一个*代表任意修饰符及任意返回值类型,第二个*代表任意方法,..匹配任意数量任意类型的参数,若目标类与该切面在同一个包中,可以省略包名。
  • execution public * cn.itcast.service.impl.PersonServiceImpl.*(..):匹配PersonServiceImpl类中的所有公有方法。
  • execution public double cn.itcast.service.impl.PersonServiceImpl.*(..):匹配PersonServiceImpl类中返回值类型为double类型的所有公有方法。
  • execution public double cn.itcast.service.impl.PersonServiceImpl.*(double, ..):匹配PersonServiceImpl类中第一个参数为double类型,后面不管有无参数的所有公有方法,并且该方法的返回值类型为double类型。
  • execution public double cn.itcast.service.impl.PersonServiceImpl.*(double, double):匹配PersonServiceImpl类中参数类型为double,double类型的,并且返回值类型也为double类型的所有公有方法。

现在若要求只拦截PersonServiceImpl类中返回值类型为String的方法,则aspectj的切入点表达式应该这样写:

execution(java.lang.String cn.itcast.service.impl.PersonServiceImpl.*(..))
  • 1
  • 1

若要求拦截PersonServiceImpl类中输入参数中的第一个参数类型为String,后面不管有没有参数的方法,则aspectj的切入点表达式应该这样写:

execution(* cn.itcast.service.impl.PersonServiceImpl.*(java.lang.String, ..))

若要求拦截PersonServiceImpl类中返回值类型不是void的所有方法,则aspectj的切入点表达式应该这样写:

execution(!void cn.itcast.service.impl.PersonServiceImpl.*(..))

若要求拦截cn.itcast.service包及其子包下的所有类的所有方法,则aspectj的切入点表达式应该这样写:

execution(* cn.itcast.service..*.*(..))

转载于:https://www.cnblogs.com/telwanggs/p/6913424.html

(转)使用Spring配置文件实现AOP相关推荐

  1. [5] Spring中的AOP操作(使用xml 配置文件的方式)

    AOP (Aspect Oriented Programing) 面向切面编程 AOP 采取横向抽取机制,取代了传统的纵向继承体系重复性代码(性能监视.事务管理.安全检查.缓存) Spring AOP ...

  2. (转)使用Spring配置文件实现事务管理

    http://blog.csdn.net/yerenyuan_pku/article/details/52886207 前面我们讲解了使用Spring注解方式来管理事务,现在我们就来学习使用Sprin ...

  3. XML配置文件的命名空间与Spring配置文件中的头

    一直以来,写Spring配置文件,都是把其他配置文件的头拷贝过来,最多改改版本号,也不清楚哪些是需要的,到底是干嘛的.今天整理一下,拒绝再无脑copy. 一.Spring配置文件常见的配置头 < ...

  4. Spring 框架的AOP之注解的方式

    1. 环境搭建 1.1 导入 jar 包 Spring 框架的基本开发包(6个); Spring 的传统AOP的开发包 spring-aop-4.3.10.RELEASE org.aopallianc ...

  5. Spring中的AOP在Advice方法中获取目标方法的参

    参考:http://my.oschina.net/itblog/blog/211693 http://christang.iteye.com/blog/2037919 http://blog.csdn ...

  6. spring配置文件中分别使用多个properties文件

    spring配置文件中分别使用多个properties文件 在使用spring时,有时候需要为了模块配置方便有时候需要针对不同的模块建立不同的applicationContext的配置文件,然后在对应 ...

  7. Spring配置文件详解三:Spring声明式事务管理

    1.声明式事务管理 Spring提供了声明式事务管理,这是通过Spring AOP实现的. 原理:Spring中进行事务管理的通常方式是利用AOP(面向切片编程)的方式,为普通java类封装事务控制, ...

  8. spring中关于aop拦截功能的记录

    java写了个接口并写了个实现类: package myspring.calculator; public interface IArithmeticCalculator { public doubl ...

  9. JAVA spring配置文件总结

    首先来看一个标准的Spring配置文件 applicationContext.xml <?xml version="1.0" encoding="UTF-8&quo ...

最新文章

  1. wxpython多个面板_wxpython:隐藏其中一个拆分窗口面板
  2. MySQL 5.7 for Windows 解压缩版配置安装
  3. 为什么我的文章没有被推荐?
  4. java 获取类加载器_java-如何从类加载器获取类路径?
  5. w8服务器dns修改,Win8.1系统的DNS地址如何修改?修改win8.1系统DNS地址图文教程
  6. 使用ssh远程连接时的一些注意问题
  7. 将军令:数据安全平台建设实践 1
  8. html列表按时间排序代码_把 Linux 上的文件列表和排序玩出花来 | Linux 中国
  9. 在cmd里面运行adb命令的时候提示:adb server is out of date. killing...
  10. 【CS229】多变量线性回归
  11. python进行数据可视化时,中文是方块显示
  12. 面向视频领域的边缘计算白皮书
  13. 进销存erp系统价格
  14. Django下进行urlencode编码,可以编码中文参数的代码
  15. 优秀java程序员必须具备的技术技能
  16. 竞品分析—微博PK微信
  17. 用python求圆的表面积_【用python写一组类(class)对应各种几何体(正方体,长方体,球,圆柱)的表面积和体积的编码】作业帮...
  18. python圣斗士(十七):令人欲罢不能的正则
  19. 利用Python(pyserial、minimalmodbus、modbus_tk)进行单片机通信
  20. 九价疫苗三针一共多少钱 2022

热门文章

  1. (92)多人投票器(七人投票器)
  2. (01)VHDL介绍
  3. (47)System Verilog 类中变量随机激励inside运算符
  4. (8)UART发送verilog与Systemverilog编码
  5. android xpath解析xml,Android 中处理 XML 的四种方式-DOM
  6. 水经注叠加cad_如何下载等高线并在CAD中与卫星影像叠加
  7. 微型计算机由5大部分,微机原理答案 (5)
  8. php框架 swoop_PHP Form Validation
  9. mybatis级联查询list_MyBatis手把手跟我做系列(四) ---级联查询与懒加载
  10. matlab中符号对象的数据类型是,符号对象(Symbolic Object)的使用