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 运用的例子:

一:能够通过以下两种方式匹配相应的方法:
   1:通过name(名称)匹配。
   2:通过正则表达式匹配。

二:通过名称匹配:
1:通过pointcut 和 advisor 拦截printName方法:创建一个org.springframework.aop.support.NameMatchMethodPointcut的切点bean  ----->   bookPointcut   即切点(pointcut) 其属性 mappedName  定义了 需要拦截的方法,就是切点。如下:
   <!-- 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对应的类:

3:定义一个 advisor 
advisor 是org.springframework.aop.support.DefaultPointcutAdvisor的bean里面有对应的属性:
包括:
pointcut  相当于以上定义的:bookPointcut
advice相当于以上定义的:aroundMethod

所以advisor对应的配置如下:
 <!-- 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();
	   }
	   
	   
}
}

测试结果:
只有在调用printName方法的时候才应用around的,其他方法不应用around,所以输出结果如下:
三月 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。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值