上一个例子演示了对特定的bean中的所有的方法进行面向切面编程,包括了 before , after , after throwing, around 几种形式:
如果想对一个bean中的特定方法进行切面编程,而不是所有的方法,就需要设置pointcut了,pointcut允许拦截一个方法通过 方法名 ,一个 pointcut必须和一个advisor想关联。
一般有以下配置组成:
1:advice 在方法执行前(before)后(after)做出相应的响应。通常是定义一些实现接口的类,然后实现相应的方法,比如:before 对应的实现MethodBeforeAdvice接口 , after对应的实现AfterReturningAdvice , around对应的实现MethodInterceptor接口 , after throwing 对应的实现:ThrowsAdvice 接口, 实现对应的接口的方法即可。
<!-- define a pointcut --> <bean id="bookPointcut" class="org.springframework.aop.support.NameMatchMethodPointcut"> <property name="mappedName" value="printName" /> </bean>
<!-- 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 ----------------------