(转)使用Spring配置文件实现AOP
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的更多相关文章
- 8 -- 深入使用Spring -- 4...6 AOP代理:基于注解的XML配置文件的管理方式
8.4.6 基于XML配置文件的管理方式 Spring 2.x 提供一个新的aop:命名空间来定义切面.切入点和增强处理. XML配置方式优点: ⊙ 如果应用没有使用JDK 1.5 以上版本,那么应用 ...
- 你不知道的Spring配置文件
Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的"图纸".Java EE程序员必须学会并灵活应用这份"图纸&quo ...
- Spring系列之AOP实现的两种方式
AOP常用的实现方式有两种,一种是采用声明的方式来实现(基于XML),一种是采用注解的方式来实现(基于AspectJ). 首先复习下AOP中一些比较重要的概念: Joinpoint(连接点):程序执行 ...
- Spring系列之AOP
一.什么是AOPAOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引 ...
- Spring配置文件详解
转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常有用 spring配置文件是用于指导Sp ...
- Hibernate SQL方言 (hibernate.dialect) Spring配置文件applicationContext.xml
转自:http://www.cnblogs.com/wj-wangjun/archive/2009/10/21/1587624.html Hibernate SQL方言 (hibernate.dial ...
- spring配置文件详解--真的蛮详细
spring配置文件详解--真的蛮详细 转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常 ...
- Spring 配置文件详解 (以2.5为例)
转载自:http://blog.csdn.net/zzjjiandan/article/details/22922847 Spring配置文件是用于指导Spring工厂进行Bean生 ...
- Spring 配置文件applicationContext.xml
Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的"图纸". Spring配置文件是一个或多个标准的XML文档,applica ...
随机推荐
- [Selenium] 测试机器上安装了多个Firefox,如何指定运行哪一个?
可通过FirefoxBinary 来指定运行某个路径下的Firefox, 示例代码如下: public class testFirefoxBinary{ public static void main ...
- Healthy Holsteins
链接 分析:因为数据范围比较小,我们可以通过二进制枚举子集,然后找出所需饲料种数最小的并记录下来,同时记录一下路径,也就是字典序最小的 /* PROB:holstein ID:wanghan LANG ...
- 详细的Ajax使用
1. ajax对xml的接收和处理 xml主要作用: 主要保存和传输数据 1. xml文档结构 dom操作xml getElementsByTagName(); //根据标签名获取元素 childNo ...
- Allure生成测试报告
Allure 使用 安装 adapter 如果要在 pytest 中使用 Allure,需要使用一个 Adaptor Allure Pytest Adaptor 安装 pytest-allure-ad ...
- Sublime Text 常用的16 个 Sublime Text 快捷键
在我做了一次包含一些现场编码的演示后,一些观众问我是如何操作这么快.当然这里没有唯一的答案,答案是一堆简单的快捷键和大量的实践的组合.为了回应那些询问,我觉得有必要看看我每天想都不用想且使用的快捷键. ...
- J2ee的SSM和SSH的小结
1.介绍SSM框架: SSM是指由Spring.SpringMVC.Mybatis三个开源框架整合的开发框架. a).Spring是一个轻量级的容器框架,核心是控制反转(IoC)和面向切面(AOP). ...
- 041--Jquery
一.Jquery对象 jQuery 对象就是通过jQuery包装DOM对象后产生的对象.jQuery 对象是 jQuery 独有的. 如果一个对象是 jQuery 对象, 那么它就可以使用 jQuer ...
- error: expected ‘)’ before ‘PRId64’(转载)
转自:www.xuebuyuan.com/2077822.html error: expected ‘)’ before ‘PRId64’ 原来这个宏定义给c用的,C++要用它,就要定义一个__STD ...
- 一文教您如何通过 Docker 快速搭建各种测试环境(Mysql, Redis, Elasticsearch, MongoDB) | 建议收藏
欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 高级架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...
- C++ C# 默认对齐是8字节
C++ C# 默认对齐是8字节 以上,一直以为是4字节,尼玛