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..*.*(..))

(转)使用Spring配置文件实现AOP的更多相关文章

  1. 8 -- 深入使用Spring -- 4...6 AOP代理:基于注解的XML配置文件的管理方式

    8.4.6 基于XML配置文件的管理方式 Spring 2.x 提供一个新的aop:命名空间来定义切面.切入点和增强处理. XML配置方式优点: ⊙ 如果应用没有使用JDK 1.5 以上版本,那么应用 ...

  2. 你不知道的Spring配置文件

    Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的"图纸".Java EE程序员必须学会并灵活应用这份"图纸&quo ...

  3. Spring系列之AOP实现的两种方式

    AOP常用的实现方式有两种,一种是采用声明的方式来实现(基于XML),一种是采用注解的方式来实现(基于AspectJ). 首先复习下AOP中一些比较重要的概念: Joinpoint(连接点):程序执行 ...

  4. Spring系列之AOP

    一.什么是AOPAOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引 ...

  5. Spring配置文件详解

      转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常有用 spring配置文件是用于指导Sp ...

  6. Hibernate SQL方言 (hibernate.dialect) Spring配置文件applicationContext.xml

    转自:http://www.cnblogs.com/wj-wangjun/archive/2009/10/21/1587624.html Hibernate SQL方言 (hibernate.dial ...

  7. spring配置文件详解--真的蛮详细

    spring配置文件详解--真的蛮详细   转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常 ...

  8. Spring 配置文件详解 (以2.5为例)

    转载自:http://blog.csdn.net/zzjjiandan/article/details/22922847          Spring配置文件是用于指导Spring工厂进行Bean生 ...

  9. Spring 配置文件applicationContext.xml

    Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的"图纸". Spring配置文件是一个或多个标准的XML文档,applica ...

随机推荐

  1. Dijkstra堆优化

    Dijkstra是一个非常不错的最短路算法,它使用两层循环进行枚举,通过每次更新蓝白点的方式更新最短路,时间复杂度为O(n^2),优于floyd的O(n^3),不过只能用于计算单源最短路,而且无法处理 ...

  2. 洛谷 P3957 跳房子 —— 二分答案+单调队列优化DP

    题目:https://www.luogu.org/problemnew/show/P3957 先二分一个 g,然后判断: 由于转移的范围是一个区间,也就是滑动窗口,所以单调队列优化: 可以先令队尾为 ...

  3. bzoj3198

    容斥原理+哈希表 恰好k个,那么上容斥原理,我们先2^6枚举相同的位置,用哈希表判断有多少个对应位置相同的元素,然后用容斥原理计算,似乎这里的容斥没有那么简单,详见这里 http://www.cnbl ...

  4. 语言学习系列-Scala连接数据库示例

    Scala语法 预装数据库Mysql,登录用户名密码为:root:root,建立数据库test1,建立数据表emp: package com.ccb.day1   import java.sql.Dr ...

  5. 012--python字符编码和list列表和循环语句

    一.字符编码: ASCII码最多只能表示 256个符号,每一个字符占8位 为什么一个字节占8位?因为计算机在读一串二进制数111011001111101110的时候, 要按照规定的长度截取,才能分清一 ...

  6. E20180407-hm

    queue   n. (人或车辆) 行列,长队; 辫子;   vi. (人.车等) 排队等候;   vt. (使) 排队,列队等待; compatible  adj. 兼容的,相容的; 和谐的,协调的 ...

  7. 878. Nth Magical Number

    A positive integer is magical if it is divisible by either A or B. Return the N-th magical number.  ...

  8. 算法学习--Day1

    为了冲刺研究生初试,我准备在课余时间捡起往日的算法.多多练习算法题目,提前准备算法的机试. 今天是4月14日,距离算法考试还有两个月的时间吧,这两个月的所学所得我就都记录在这里了.不仅仅包括算法的准备 ...

  9. lightoj1169【DP】

    题意(来自大哥): 有两栋楼,左边一栋,右边一栋,层数从1-n,地面的标号为0,每一层有一个水果.有一只猴子在地面上,他现在要上到n层去,在第i层会吃掉水果花费一定时间. 猴子有两种方式从第i层到i+ ...

  10. bzoj 4407: 于神之怒加强版【莫比乌斯反演+线性筛】

    看着就像反演,所以先推式子(默认n<m): \[ \sum_{d=1}^{n}d^k\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)==d] \] \[ =\sum_{d=1} ...