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对象默认是立即加载(立即 ...
随机推荐
- 关于Web与JS
Web包含的范围比较广, JS只是代码逻辑而已. Web比如HTTP Message, SOAP Message, 浏览器流程,工具等. 不仅仅是代码.
- Notepad++中常用的插件
Notepad++中常用的插件 Notepad++实用插件分享 otepad++前端开发常用插件介绍
- [初级教程]用SecureCRT+Xming轻松远程实现Linux的X DISPLAY
[初级教程]用SecureCRT+Xming轻松远程实现Linux的X DISPLAY 发布者:sqqdugdu 时间:10-06 阅读数:2117 测试环境:RHEL 6.1,SecureCRT 5 ...
- HDU 1247 Hat’s Words(map,STL,字符处理,string运用)
题目 用map写超便捷 也可以用字典树来写 我以前是用map的: #include<stdio.h> #include<string.h> #include<algori ...
- LINGO使用教程(一)
LINGO是用来求解线性和非线性优化问题的简易工具.LINGO内置了一种建立最优化模型的语言,可以简便地表达大规模问题,利用LINGO高效的求解器可快速求解并分析结果. 1.LINGO快速入门 当你在 ...
- 强大的grep命令
1.作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全 ...
- 【蛙蛙推荐】Lucene.net试用
[蛙蛙推荐]Lucene.net试用 [简介] lucene.net好多人都知道的吧,反正我是最近才好好的看了一下,别笑我拿历史当新闻哦,不太了解Lucence的朋友先听我说两句哦.Lucene的 ...
- hdu 1526(最大匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1526 思路:floyd求传递闭包,然后就是最大匹配了,不过一开始输入没看清,被坑了将近2个小时. #i ...
- linq 常用语句
自己练习的 switch (productDataAnalysisQuery.DataType) { : var data = (from hp in GPEcontext.hbl_product j ...
- asp.net跳转页面的三种方法比较
目前,对于学习asp.net的很多朋友来讲,实现跳转页面的方法还不是很了解.本文将为朋友们介绍利用asp.net跳转页面的三种方法,并对其之间的形式进行比较,希望能够对朋友们有所帮助. ASP.NET ...