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 – 一个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="h3i mook kim" /> <property name="url" value="http://www.h3.com" /> </bean> </beans>
执行它
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[] { "spring-customer.xml" }); customerservice cust = (customerservice) appcontext.getbean("customerservice"); system.out.println("*************************"); cust.printname(); system.out.println("*************************"); cust.printurl(); system.out.println("*************************"); try { cust.printthrowexception(); } catch (exception e) { } } }
输出
************************* customer name : h3 mook kim ************************* customer website : http://www.h3.com *************************
package com.h3.aop; import java.lang.reflect.method; import org.springframework.aop.methodbeforeadvice; public class hijackbeforemethod implements methodbeforeadvice { @override public void before(method method, object[] args, object target) throws throwable { system.out.println("hijackbeforemethod : before method hijacked!"); } }
<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 mook kim" /> <property name="url" value="http://www.h3.com" /> </bean> <bean id="hijackbeforemethodbean" class="com.h3.aop.hijackbeforemethod" /> <bean id="customerserviceproxy" class="org.springframework.aop.framework.proxyfactorybean"> <property name="target" ref="customerservice" /> <property name="interceptornames"> <list> <value>hijackbeforemethodbean</value> </list> </property> </bean> </beans>
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[] { "spring-customer.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) { } } }
输出结果
************************* hijackbeforemethod : before method hijacked! customer name : h3 mook kim ************************* hijackbeforemethod : before method hijacked! customer website : http://www.h3.com ************************* hijackbeforemethod : before method hijacked!
package com.h3.aop; import java.lang.reflect.method; import org.springframework.aop.afterreturningadvice; public class hijackaftermethod implements afterreturningadvice { @override public void afterreturning(object returnvalue, method method, object[] args, object target) throws throwable { system.out.println("hijackaftermethod : after method hijacked!"); } }
<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="hijackaftermethodbean" class="com.h3.aop.hijackaftermethod" /> <bean id="customerserviceproxy" class="org.springframework.aop.framework.proxyfactorybean"> <property name="target" ref="customerservice" /> <property name="interceptornames"> <list> <value>hijackaftermethodbean</value> </list> </property> </bean> </beans>
************************* customer name : h3 mook kim hijackaftermethod : after method hijacked! ************************* customer website : http://www.h3.com hijackaftermethod : after method hijacked! *************************
package com.h3.aop; import org.springframework.aop.throwsadvice; public class hijackthrowexception implements throwsadvice { public void afterthrowing(illegalargumentexception e) throws throwable { system.out.println("hijackthrowexception : throw exception hijacked!"); } }
<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="hijackthrowexceptionbean" class="com.h3.aop.hijackthrowexception" /> <bean id="customerserviceproxy" class="org.springframework.aop.framework.proxyfactorybean"> <property name="target" ref="customerservice" /> <property name="interceptornames"> <list> <value>hijackthrowexceptionbean</value> </list> </property> </bean> </beans>
************************* customer name : h3 mook kim ************************* customer website : http://www.h3.com ************************* hijackthrowexception : throw exception hijacked!
它结合了上面的三个通知,在方法执行过程中执行。创建一个实现了methodinterceptor接口的类。必须调用“methodinvocation.proceed();” 继续在原来的方法执行,否则原来的方法将不会执行。
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())); // same with methodbeforeadvice system.out.println("hijackaroundmethod : before method hijacked!"); try { // proceed to original method call object result = methodinvocation.proceed(); // same with afterreturningadvice system.out.println("hijackaroundmethod : before after hijacked!"); return result; } catch (illegalargumentexception e) { // same with throwsadvice system.out.println("hijackaroundmethod : throw exception hijacked!"); throw e; } } }
<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="hijackaroundmethodbean" class="com.h3.aop.hijackaroundmethod" /> <bean id="customerserviceproxy" class="org.springframework.aop.framework.proxyfactorybean"> <property name="target" ref="customerservice" /> <property name="interceptornames"> <list> <value>hijackaroundmethodbean</value> </list> </property> </bean> </beans>
************************* method name : printname method arguments : [] hijackaroundmethod : before method hijacked! customer name : h3 mook kim 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!