Spring 运用 pointcut 和 advisor 对特定的方法进行切面编程
上一个例子演示了对特定的bean中的所有的方法进行面向切面编程,包括了 before , after , after throwing, around 几种形式:
如果想对一个bean中的特定方法进行切面编程,而不是所有的方法,就需要设置pointcut了,pointcut允许拦截一个方法通过 方法名 ,一个 pointcut必须和一个advisor想关联。
一般有以下配置组成:
1:advice 在方法执行前(before)后(after)做出相应的响应。通常是定义一些实现接口的类,然后实现相应的方法,比如:before 对应的实现MethodBeforeAdvice接口 , after对应的实现AfterReturningAdvice , around对应的实现MethodInterceptor接口 , after throwing 对应的实现:ThrowsAdvice 接口, 实现对应的接口的方法即可。
Pointcut 运用的例子:
- <!-- define a pointcut -->
- <bean id="bookPointcut" class="org.springframework.aop.support.NameMatchMethodPointcut">
- <property name="mappedName" value="printName" />
- </bean>
2:定义一个advice 相当于 对切点所做的操作, 根据advice的拦截位置需要实现相应的接口,比如: MethodInterceptor 是around对应的接口。
- <!-- around method -->
- bean id="aroundMethod" class="com.myapp.core.aop.advice.AroundMethod" />
对应的类:
- package com.myapp.core.aop.advice;
- import java.util.Arrays;
- import org.aopalliance.intercept.MethodInterceptor;
- import org.aopalliance.intercept.MethodInvocation;
- public class AroundMethod implements MethodInterceptor{
- @Override
- public Object invoke(MethodInvocation methodInvocation) throws Throwable {
- // TODO Auto-generated method stub
- System.out.println("method name:" + methodInvocation.getMethod().getName());
- System.out.println("method arguments" + Arrays.toString(methodInvocation.getArguments()));
- System.out.println("Around method : before ");
- try{
- Object result = methodInvocation.proceed();
- System.out.println("Around method : after ");
- return result;
- }catch(IllegalArgumentException e){
- System.out.println("Around method : throw an exception ");
- throw e;
- }
- }
- }
以上就是一个advice对应的类:
- <!-- define a advisor -->
- <bean id="bookAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
- <property name="pointcut" ref="bookPointcut"/>
- <property name="advice" ref="aroundMethod"></property>
- </bean>
其他部分不变:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- 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-3.0.xsd">
- <!-- more bean definitions for data access objects go here -->
- <bean id="book" class="com.myapp.core.aop.advice.Book">
- <property name="name" value="Effective java" />
- <property name="url" value="www.google.cn"/>
- <property name="pages" value="300" />
- </bean>
- <!-- around method -->
- <bean id="aroundMethod" class="com.myapp.core.aop.advice.AroundMethod" />
- <!-- define a pointcut -->
- <bean id="bookPointcut" class="org.springframework.aop.support.NameMatchMethodPointcut">
- <property name="mappedName" value="printName" />
- </bean>
- <!-- define a advisor -->
- <bean id="bookAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
- <property name="pointcut" ref="bookPointcut"/>
- <property name="advice" ref="aroundMethod"></property>
- </bean>
- <bean id="bookProxy" class="org.springframework.aop.framework.ProxyFactoryBean" >
- <property name="target" ref="book"/>
- <property name="interceptorNames">
- <list>
- <value>bookAdvisor</value>
- </list>
- </property>
- </bean>
- </beans>
对应的测试类:
- package com.myapp.core.aop.advice;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class MainTest {
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext("resource/aop.xml");
- Book book = (Book) context.getBean("bookProxy");
- System.out.println("---------------------");
- book.printName();
- System.out.println("---------------------");
- book.printUrl();
- System.out.println("----------------------");
- try{
- book.printThrowException();
- }catch(Exception e){
- // e.printStackTrace();
- }
- }
- }
测试结果:
- 三月 20, 2013 5:37:23 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
- INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@eb67e8: startup date [Wed Mar 20 17:37:23 CST 2013]; root of context hierarchy
- 三月 20, 2013 5:37:23 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
- INFO: Loading XML bean definitions from class path resource [resource/aop.xml]
- 三月 20, 2013 5:37:23 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
- INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@157985: defining beans [book,aroundMethod,bookPointcut,bookAdvisor,bookProxy]; root of factory hierarchy
- ---------------------
- method name:printName
- method arguments[]
- Around method : before
- Book name Effective java
- Around method : after
- ---------------------
- Book URL www.google.cn
- ----------------------
只有在printName方法上作用了around,其他方法没有调用,over。
Spring 运用 pointcut 和 advisor 对特定的方法进行切面编程的更多相关文章
- spring aop pointcut 切入点是类的公共方法(私有方法不行),还是接口的方法
spring aop pointcut 切入点是类的公共方法(私有方法不行),还是接口的方法 类的公共方法可以,但是私有方法不行 测试一下接口的方法是否能够捕捉到
- 不依赖Spring使用AspectJ达到AOP面向切面编程
网上大多数介绍AspectJ的文章都是和Spring容器混用的,但有时我们想自己写框架就需要抛开Spring造轮子,类似使用原生AspectJ达到面向切面编程.步骤很简单,只需要两步. 1.导入依赖 ...
- (精简)Spring框架的IoC(替代工厂类实现方法)和AOP(定义规则,约定大于配置)
Spring的核心框架主要包含两个技术,分别用来处理工厂类,以及事务处理和连接管理的. 两大核心概念 1) IoC:控制反转,在现在的开发中,如果想建立对象并设置属性,是需要先new对象,再通过se ...
- spring aop方式配置事务中的三个概念 pointcut advice advisor
AOP的3个关键概念 因为AOP的概念难于理解,所以在前面首先对Java动态代理机制进行了一下讲解,从而使读者能够循序渐进地来理解AOP的思想. 学习AOP,关键在于理解AOP的思想,能够使用AOP. ...
- Spring学习笔记:使用Pointcut 和Advisor实现AOP
基础知识 在 Spring AOP 中,有 3 个常用的概念,Advices . Pointcut . Advisor ,解释如下: Advices :表示一个 method 执行前或执行后的动作. ...
- Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法
Spring3系列10- Spring AOP——Pointcut,Advisor 上一篇的Spring AOP Advice例子中,Class(CustomerService)中的全部method都 ...
- 关于spring.net的面向切面编程 (Aspect Oriented Programming with Spring.NET)-切入点(pointcut)API
本文翻译自Spring.NET官方文档Version 1.3.2. 受限于个人知识水平,有些地方翻译可能不准确,但是我还是希望我的这些微薄的努力能为他人提供帮助. 侵删. 让我们看看 Spring.N ...
- 学习AOP之透过Spring的Ioc理解Advisor
花了几天时间来学习Spring,突然明白一个问题,就是看书不能让人理解Spring,一方面要结合使用场景,另一方面要阅读源代码,这种方式理解起来事半功倍.那看书有什么用呢?主要还是扩展视野,毕竟书是别 ...
- Spring面向切面编程(AOP)
1 spring容器中bean特性 Spring容器的javabean对象默认是单例的. 通过在xml文件中,配置可以使用某些对象为多列. Spring容器中的javabean对象默认是立即加载(立即 ...
随机推荐
- 利用Linq + Jquery + Ajax 异步分页的实现
在Web显示的时候我们经常会遇到分页显示,而网上的分页方法甚多,但都太过于消耗带宽,所以我想到了用Ajax来分页,利用返回的Json来处理返回的数据, 大大简化了带宽的压力. 先说下思路,无非就是异步 ...
- 删除提示 FOREIGN KEY 约束引用”
有时想删除某个表时,提示“无法删除对象 'Orders',因为该对象正由一个 FOREIGN KEY 约束引用”,原因很简单不要急躁,它被其它表的外键引用了,所以无法删除,在此只需先找到哪些表的外键引 ...
- HDU2295 Radar (DLX)
下面的代码99%参考了这个网站http://www.cnblogs.com/183zyz/archive/2011/08/07/2130193.html 人生的第一道DLX肯定是需要作一些参考的啦. ...
- C#中对象的输出
假设有个Costmer类如下: class Costmer { public string Id { get; set; } public string City { get; set; } publ ...
- 传说中的WCF(11):会话(Session)
在标题中我加了一个大家都很熟悉的单词——Session,熟吧?玩过Web开发的朋友肯定在梦中都会见到她. 在Web中为什么要会话呢?毕竟每个用户在一个Web应用中可能不止进行一次操作,比如,某二手飞机 ...
- (9)nehe教程3--添加颜色
添加颜色: 作为第二课的扩展,我将叫你如何使用颜色.你将理解两种着色模式,在左图中,三角形用的是光滑着色,四边形用的是平面着色. 上一课中我教给您三角形和四边形的绘制方法.这一课我将教您给三角形和四边 ...
- C# winform post请求数据
刚到公司混的时候,老板要求实现一个从C#的windows应用程序传参数到一个网页,然后这个网页不显示出来,但能把数据返回给应用程序的功能,问了好多人,找了好多资料,都搞不定,后来还是在老板的帮助下搞定 ...
- 经典SQL查询语句大全
一.基础1.说明:创建数据库CREATE DATABASE database-name2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备份数 ...
- Project Euler 108:Diophantine reciprocals I 丢番图倒数I
Diophantine reciprocals I In the following equation x, y, and n are positive integers. For n = 4 the ...
- php脚本的执行过程(编译与执行相分离)
php脚本的执行过程(编译与执行相分离) 深入理解PHP代码的执行的过程 PHP程序的执行流程 Apache + PHP 的并发访问