再次回顾上一个 spring aop通知的例子。
file : customerservice.java
package com.h3.customer.services; public class customerservice { private string name; private string url; public void setname(string name) { this.name = name; } public void seturl(string url) { this.url = url; } public void printname(){ system.out.println("customer name : " + this.name); } public void printurl(){ system.out.println("customer website : " + this.url); } public void printthrowexception(){ throw new illegalargumentexception(); } }
file : applicationcontext.xml
<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-2.5.xsd"> <bean id="customerservice" class="com.h3.customer.services.customerservice"> <property name="name" value="yong mook kim" /> <property name="url" value="http://www.h3.com" /> </bean> <bean id="hijackaroundmethodbeanadvice" class="com.h3.aop.hijackaroundmethod" /> <bean id="customerserviceproxy" class="org.springframework.aop.framework.proxyfactorybean"> <property name="target" ref="customerservice" /> <property name="interceptornames"> <list> <value>hijackaroundmethodbeanadvice</value> </list> </property> </bean> </beans>
file : hijackaroundmethod.java
package com.h3.aop; import java.util.arrays; import org.aopalliance.intercept.methodinterceptor; import org.aopalliance.intercept.methodinvocation; public class hijackaroundmethod implements methodinterceptor { @override public object invoke(methodinvocation methodinvocation) throws throwable { system.out.println("method name : " + methodinvocation.getmethod().getname()); system.out.println("method arguments : " + arrays.tostring(methodinvocation.getarguments())); system.out.println("hijackaroundmethod : before method hijacked!"); try { object result = methodinvocation.proceed(); system.out.println("hijackaroundmethod : before after hijacked!"); return result; } catch (illegalargumentexception e) { system.out.println("hijackaroundmethod : throw exception hijacked!"); throw e; } } }
执行它
package com.h3.common; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import com.h3.customer.services.customerservice; public class app { public static void main(string[] args) { applicationcontext appcontext = new classpathxmlapplicationcontext( new string[] { "applicationcontext.xml" }); customerservice cust = (customerservice) appcontext .getbean("customerserviceproxy"); system.out.println("*************************"); cust.printname(); system.out.println("*************************"); cust.printurl(); system.out.println("*************************"); try { cust.printthrowexception(); } catch (exception e) { } } }
输出
************************* method name : printname method arguments : [] hijackaroundmethod : before method hijacked! customer name : h3 hijackaroundmethod : before after hijacked! ************************* method name : printurl method arguments : [] hijackaroundmethod : before method hijacked! customer website : http://www.h3.com hijackaroundmethod : before after hijacked! ************************* method name : printthrowexception method arguments : [] hijackaroundmethod : before method hijacked! hijackaroundmethod : throw exception hijacked!
<bean id="customerh3cut" class="org.springframework.aop.support.namematchmethodh3cut"> <property name="mappedname" value="printname" /> </bean>
<bean id="customeradvisor" class="org.springframework.aop.support.defaulth3cutadvisor"> <property name="pointcut" ref="customerh3cut" /> <property name="advice" ref="hijackaroundmethodbeanadvice" /> </bean>
<bean id="customerserviceproxy" class="org.springframework.aop.framework.proxyfactorybean"> <property name="target" ref="customerservice" /> <property name="interceptornames"> <list> <value>customeradvisor</value> </list> </property> </bean>
<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-2.5.xsd"> <bean id="customerservice" class="com.h3.customer.services.customerservice"> <property name="name" value="h3" /> <property name="url" value="http://www.h3.com" /> </bean> <bean id="hijackaroundmethodbeanadvice" class="com.h3.aop.hijackaroundmethod" /> <bean id="customerserviceproxy" class="org.springframework.aop.framework.proxyfactorybean"> <property name="target" ref="customerservice" /> <property name="interceptornames"> <list> <value>customeradvisor</value> </list> </property> </bean> <bean id="customerh3cut" class="org.springframework.aop.support.namematchmethodh3cut"> <property name="mappedname" value="printname" /> </bean> <bean id="customeradvisor" class="org.springframework.aop.support.defaulth3cutadvisor"> <property name="pointcut" ref="customerh3cut" /> <property name="advice" ref="hijackaroundmethodbeanadvice" /> </bean> </beans>
************************* method name : printname method arguments : [] hijackaroundmethod : before method hijacked! customer name : h3 hijackaroundmethod : before after hijacked! ************************* customer website : http://www.h3.com *************************
<bean id="customeradvisor" class="org.springframework.aop.support.namematchmethodh3cutadvisor"> <property name="mappedname" value="printname" /> <property name="advice" ref="hijackaroundmethodbeanadvice" /> </bean>
也可以通过使用正则表达式匹配切入点方法的名称 – regexpmethodh3cutadvisor.
<bean id="customeradvisor" class="org.springframework.aop.support.regexpmethodh3cutadvisor"> <property name="patterns"> <list> <value>.*url.*</value> </list> </property> <property name="advice" ref="hijackaroundmethodbeanadvice" /> </bean>
现在,它拦截方法名称中有“url”的方法。在实践中,可以用它来管理dao层,声明“.*dao.*” 拦截所有的dao类来支持事务。