package com.h3.customer.bo; public interface customerbo { void addcustomer(); string addcustomerreturnvalue(); void addcustomerthrowexception() throws exception; void addcustomeraround(string name); }
package com.h3.customer.bo.impl; import com.h3.customer.bo.customerbo; public class customerboimpl implements customerbo { public void addcustomer(){ system.out.println("addcustomer() is running "); } public string addcustomerreturnvalue(){ system.out.println("addcustomerreturnvalue() is running "); return "abc"; } public void addcustomerthrowexception() throws exception { system.out.println("addcustomerthrowexception() is running "); throw new exception("generic error"); } public void addcustomeraround(string name){ system.out.println("addcustomeraround() is running, args : " + name); } }
file : applicationcontext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <aop:aspectj-autoproxy /> <bean id="customerbo" class="com.h3.customer.bo.impl.customerboimpl" /> <!-- aspect --> <bean id="logaspect" class="com.h3.aspect.loggingaspect" /> </beans>
file : loggingaspect.java
package com.h3.aspect; import org.aspectj.lang.joinh3; import org.aspectj.lang.annotation.aspect; import org.aspectj.lang.annotation.before; @aspect public class loggingaspect { @before("execution(* com.h3.customer.bo.customerbo.addcustomer(..))") public void logbefore(joinh3 joinh3) { system.out.println("logbefore() is running!"); system.out.println("hijacked : " + joinh3.getsignature().getname()); system.out.println("******"); } }
运行
customerbo customer = (customerbo) appcontext.getbean("customerbo"); customer.addcustomer();
输出结果
logbefore() is running! hijacked : addcustomer ****** addcustomer() is running
file : loggingaspect.java
package com.h3.aspect; import org.aspectj.lang.joinh3; import org.aspectj.lang.annotation.aspect; import org.aspectj.lang.annotation.after; @aspect public class loggingaspect { @after("execution(* com.h3.customer.bo.customerbo.addcustomer(..))") public void logafter(joinh3 joinh3) { system.out.println("logafter() is running!"); system.out.println("hijacked : " + joinh3.getsignature().getname()); system.out.println("******"); } }
运行它
customerbo customer = (customerbo) appcontext.getbean("customerbo"); customer.addcustomer();
输出结果
addcustomer() is running logafter() is running! hijacked : addcustomer ******
在下面例子中,logafterreturning()方法将在 customerbo 接口的addcustomerreturnvalue()方法执行之后执行。此外,还可以截取返回的值使用“returning”属性。
file : loggingaspect.java
package com.h3.aspect; import org.aspectj.lang.joinh3; import org.aspectj.lang.annotation.aspect; import org.aspectj.lang.annotation.afterreturning; @aspect public class loggingaspect { @afterreturning( pointcut = "execution(* com.h3.customer.bo.customerbo.addcustomerreturnvalue(..))", returning= "result") public void logafterreturning(joinh3 joinh3, object result) { system.out.println("logafterreturning() is running!"); system.out.println("hijacked : " + joinh3.getsignature().getname()); system.out.println("method returned value is : " + result); system.out.println("******"); } }
运行它
customerbo customer = (customerbo) appcontext.getbean("customerbo"); customer.addcustomerreturnvalue();
输出结果
addcustomerreturnvalue() is running logafterreturning() is running! hijacked : addcustomerreturnvalue method returned value is : abc ******
file : loggingaspect.java
package com.h3.aspect; import org.aspectj.lang.joinh3; import org.aspectj.lang.annotation.aspect; import org.aspectj.lang.annotation.afterthrowing; @aspect public class loggingaspect { @afterthrowing( pointcut = "execution(* com.h3.customer.bo.customerbo.addcustomerthrowexception(..))", throwing= "error") public void logafterthrowing(joinh3 joinh3, throwable error) { system.out.println("logafterthrowing() is running!"); system.out.println("hijacked : " + joinh3.getsignature().getname()); system.out.println("exception : " + error); system.out.println("******"); } }
运行它
customerbo customer = (customerbo) appcontext.getbean("customerbo"); customer.addcustomerthrowexception();
输出结果
addcustomerthrowexception() is running logafterthrowing() is running! hijacked : addcustomerthrowexception exception : java.lang.exception: generic error ****** exception in thread "main" java.lang.exception: generic error //...
在下面例子中,logaround()方法将在customerbo接口的addcustomeraround()方法执行之前执行, 必须定义“joinh3.proceed();” 控制何时拦截器返回控制到原来的addcustomeraround()方法。
file : loggingaspect.java
package com.h3.aspect; import org.aspectj.lang.proceedingjoinh3; import org.aspectj.lang.annotation.aspect; import org.aspectj.lang.annotation.around; @aspect public class loggingaspect { @around("execution(* com.h3.customer.bo.customerbo.addcustomeraround(..))") public void logaround(proceedingjoinh3 joinh3) throws throwable { system.out.println("logaround() is running!"); system.out.println("hijacked method : " + joinh3.getsignature().getname()); system.out.println("hijacked arguments : " + arrays.tostring(joinh3.getargs())); system.out.println("around before is running!"); joinh3.proceed(); //continue on the intercepted method system.out.println("around after is running!"); system.out.println("******"); } }
运行它
customerbo customer = (customerbo) appcontext.getbean("customerbo"); customer.addcustomeraround("h3");
输出结果
logaround() is running! hijacked method : addcustomeraround hijacked arguments : [h3] around before is running! addcustomeraround() is running, args : h3 around after is running! ******