Spring 运用 pointcut 和 advisor 对特定的方法进行切面编程
Posted MrLiub
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 运用 pointcut 和 advisor 对特定的方法进行切面编程相关的知识,希望对你有一定的参考价值。
如果想对一个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;
- }
- }
- }
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[email protected]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.s[email protected]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:JoinPoint 和 PointCut 有啥区别?
Spring AOP中pointcut expression表达式解析