上一个例子演示了对特定的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 运用的例子:

一:能够通过以下两种方式匹配相应的方法:
   1:通过name(名称)匹配。
   2:通过正则表达式匹配。
 
二:通过名称匹配:
1:通过pointcut 和 advisor 拦截printName方法:创建一个org.springframework.aop.support.NameMatchMethodPointcut的切点bean  ----->   bookPointcut   即切点(pointcut) 其属性 mappedName  定义了 需要拦截的方法,就是切点。如下:
  1. <!-- define  a  pointcut -->
  2. <bean id="bookPointcut" class="org.springframework.aop.support.NameMatchMethodPointcut">
  3. <property name="mappedName" value="printName" />
  4. </bean>

2:定义一个advice 相当于 对切点所做的操作, 根据advice的拦截位置需要实现相应的接口,比如: MethodInterceptor 是around对应的接口。

配置片段如下:
  1. <!-- around  method -->
  2. bean id="aroundMethod"  class="com.myapp.core.aop.advice.AroundMethod" />

对应的类:

  1. package com.myapp.core.aop.advice;
  2. import java.util.Arrays;
  3. import org.aopalliance.intercept.MethodInterceptor;
  4. import org.aopalliance.intercept.MethodInvocation;
  5. public class AroundMethod  implements MethodInterceptor{
  6. @Override
  7. public Object invoke(MethodInvocation methodInvocation) throws Throwable {
  8. // TODO Auto-generated method stub
  9. System.out.println("method  name:" + methodInvocation.getMethod().getName());
  10. System.out.println("method  arguments" + Arrays.toString(methodInvocation.getArguments()));
  11. System.out.println("Around  method : before ");
  12. try{
  13. Object result = methodInvocation.proceed();
  14. System.out.println("Around method : after ");
  15. return  result;
  16. }catch(IllegalArgumentException e){
  17. System.out.println("Around method : throw  an  exception ");
  18. throw  e;
  19. }
  20. }
  21. }

以上就是一个advice对应的类:

 
3:定义一个 advisor 
advisor 是org.springframework.aop.support.DefaultPointcutAdvisor的bean里面有对应的属性:
包括:
pointcut  相当于以上定义的:bookPointcut
advice相当于以上定义的:aroundMethod
 
所以advisor对应的配置如下:
  1. <!-- define a advisor -->
  2. <bean id="bookAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
  3. <property name="pointcut"  ref="bookPointcut"/>
  4. <property name="advice" ref="aroundMethod"></property>
  5. </bean>

其他部分不变:

 
完整的配置文件如下:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <!-- more bean definitions for data access objects go here -->
  7. <bean id="book" class="com.myapp.core.aop.advice.Book">
  8. <property name="name" value="Effective java" />
  9. <property name="url" value="www.google.cn"/>
  10. <property name="pages" value="300" />
  11. </bean>
  12. <!-- around  method -->
  13. <bean id="aroundMethod"  class="com.myapp.core.aop.advice.AroundMethod" />
  14. <!-- define  a  pointcut -->
  15. <bean id="bookPointcut" class="org.springframework.aop.support.NameMatchMethodPointcut">
  16. <property name="mappedName" value="printName" />
  17. </bean>
  18. <!-- define a advisor -->
  19. <bean id="bookAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
  20. <property name="pointcut"  ref="bookPointcut"/>
  21. <property name="advice" ref="aroundMethod"></property>
  22. </bean>
  23. <bean id="bookProxy" class="org.springframework.aop.framework.ProxyFactoryBean" >
  24. <property name="target" ref="book"/>
  25. <property name="interceptorNames">
  26. <list>
  27. <value>bookAdvisor</value>
  28. </list>
  29. </property>
  30. </bean>
  31. </beans>

对应的测试类:

  1. package com.myapp.core.aop.advice;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainTest {
  5. public static void main(String[] args) {
  6. ApplicationContext  context  = new  ClassPathXmlApplicationContext("resource/aop.xml");
  7. Book   book  =   (Book) context.getBean("bookProxy");
  8. System.out.println("---------------------");
  9. book.printName();
  10. System.out.println("---------------------");
  11. book.printUrl();
  12. System.out.println("----------------------");
  13. try{
  14. book.printThrowException();
  15. }catch(Exception e){
  16. //  e.printStackTrace();
  17. }
  18. }
  19. }

测试结果:

只有在调用printName方法的时候才应用around的,其他方法不应用around,所以输出结果如下:
  1. 三月 20, 2013 5:37:23 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
  2. INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@eb67e8: startup date [Wed Mar 20 17:37:23 CST 2013]; root of context hierarchy
  3. 三月 20, 2013 5:37:23 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  4. INFO: Loading XML bean definitions from class path resource [resource/aop.xml]
  5. 三月 20, 2013 5:37:23 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
  6. INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@157985: defining beans [book,aroundMethod,bookPointcut,bookAdvisor,bookProxy]; root of factory hierarchy
  7. ---------------------
  8. method  name:printName
  9. method  arguments[]
  10. Around  method : before
  11. Book name Effective java
  12. Around method : after
  13. ---------------------
  14. Book URL www.google.cn
  15. ----------------------

只有在printName方法上作用了around,其他方法没有调用,over。

Spring 运用 pointcut 和 advisor 对特定的方法进行切面编程的更多相关文章

  1. spring aop pointcut 切入点是类的公共方法(私有方法不行),还是接口的方法

    spring aop pointcut 切入点是类的公共方法(私有方法不行),还是接口的方法 类的公共方法可以,但是私有方法不行 测试一下接口的方法是否能够捕捉到

  2. 不依赖Spring使用AspectJ达到AOP面向切面编程

    网上大多数介绍AspectJ的文章都是和Spring容器混用的,但有时我们想自己写框架就需要抛开Spring造轮子,类似使用原生AspectJ达到面向切面编程.步骤很简单,只需要两步. 1.导入依赖 ...

  3. (精简)Spring框架的IoC(替代工厂类实现方法)和AOP(定义规则,约定大于配置)

    Spring的核心框架主要包含两个技术,分别用来处理工厂类,以及事务处理和连接管理的. 两大核心概念 1)  IoC:控制反转,在现在的开发中,如果想建立对象并设置属性,是需要先new对象,再通过se ...

  4. spring aop方式配置事务中的三个概念 pointcut advice advisor

    AOP的3个关键概念 因为AOP的概念难于理解,所以在前面首先对Java动态代理机制进行了一下讲解,从而使读者能够循序渐进地来理解AOP的思想. 学习AOP,关键在于理解AOP的思想,能够使用AOP. ...

  5. Spring学习笔记:使用Pointcut 和Advisor实现AOP

    基础知识 在 Spring AOP 中,有 3 个常用的概念,Advices . Pointcut . Advisor ,解释如下: Advices :表示一个 method 执行前或执行后的动作. ...

  6. Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法

    Spring3系列10- Spring AOP——Pointcut,Advisor 上一篇的Spring AOP Advice例子中,Class(CustomerService)中的全部method都 ...

  7. 关于spring.net的面向切面编程 (Aspect Oriented Programming with Spring.NET)-切入点(pointcut)API

    本文翻译自Spring.NET官方文档Version 1.3.2. 受限于个人知识水平,有些地方翻译可能不准确,但是我还是希望我的这些微薄的努力能为他人提供帮助. 侵删. 让我们看看 Spring.N ...

  8. 学习AOP之透过Spring的Ioc理解Advisor

    花了几天时间来学习Spring,突然明白一个问题,就是看书不能让人理解Spring,一方面要结合使用场景,另一方面要阅读源代码,这种方式理解起来事半功倍.那看书有什么用呢?主要还是扩展视野,毕竟书是别 ...

  9. Spring面向切面编程(AOP)

    1 spring容器中bean特性 Spring容器的javabean对象默认是单例的. 通过在xml文件中,配置可以使用某些对象为多列. Spring容器中的javabean对象默认是立即加载(立即 ...

随机推荐

  1. 关于Web与JS

    Web包含的范围比较广, JS只是代码逻辑而已. Web比如HTTP Message, SOAP Message, 浏览器流程,工具等. 不仅仅是代码.

  2. Notepad++中常用的插件

    Notepad++中常用的插件 Notepad++实用插件分享 otepad++前端开发常用插件介绍

  3. [初级教程]用SecureCRT+Xming轻松远程实现Linux的X DISPLAY

    [初级教程]用SecureCRT+Xming轻松远程实现Linux的X DISPLAY 发布者:sqqdugdu 时间:10-06 阅读数:2117 测试环境:RHEL 6.1,SecureCRT 5 ...

  4. HDU 1247 Hat’s Words(map,STL,字符处理,string运用)

    题目 用map写超便捷 也可以用字典树来写 我以前是用map的: #include<stdio.h> #include<string.h> #include<algori ...

  5. LINGO使用教程(一)

    LINGO是用来求解线性和非线性优化问题的简易工具.LINGO内置了一种建立最优化模型的语言,可以简便地表达大规模问题,利用LINGO高效的求解器可快速求解并分析结果. 1.LINGO快速入门 当你在 ...

  6. 强大的grep命令

    1.作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全 ...

  7. 【蛙蛙推荐】Lucene.net试用

    [蛙蛙推荐]Lucene.net试用   [简介] lucene.net好多人都知道的吧,反正我是最近才好好的看了一下,别笑我拿历史当新闻哦,不太了解Lucence的朋友先听我说两句哦.Lucene的 ...

  8. hdu 1526(最大匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1526 思路:floyd求传递闭包,然后就是最大匹配了,不过一开始输入没看清,被坑了将近2个小时. #i ...

  9. linq 常用语句

    自己练习的 switch (productDataAnalysisQuery.DataType) { : var data = (from hp in GPEcontext.hbl_product j ...

  10. asp.net跳转页面的三种方法比较

    目前,对于学习asp.net的很多朋友来讲,实现跳转页面的方法还不是很了解.本文将为朋友们介绍利用asp.net跳转页面的三种方法,并对其之间的形式进行比较,希望能够对朋友们有所帮助. ASP.NET ...