本文主要是介绍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{@Overridepublic Object invoke(MethodInvocation methodInvocation) throws Throwable {// TODO Auto-generated method stubSystem.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/beanshttp://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 对特定的方法进行切面编程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!