package com.zyiz.customer.bo; public interface CustomerBo { void addCustomer(); String addCustomerReturnValue(); void addCustomerThrowException() throws Exception; void addCustomerAround(String name); }
package com.zyiz.customer.bo.impl; import com.zyiz.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.zyiz.customer.bo.impl.CustomerBoImpl" /> <!-- Aspect --> <bean id="logAspect" class="com.zyiz.aspect.LoggingAspect" /> </beans>
File : LoggingAspect.java
package com.zyiz.aspect; import org.aspectj.lang.Joinzyiz; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class LoggingAspect { @Before("execution(* com.zyiz.customer.bo.CustomerBo.addCustomer(..))") public void logBefore(Joinzyiz joinzyiz) { System.out.println("logBefore() is running!"); System.out.println("hijacked : " + joinzyiz.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.zyiz.aspect; import org.aspectj.lang.Joinzyiz; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.After; @Aspect public class LoggingAspect { @After("execution(* com.zyiz.customer.bo.CustomerBo.addCustomer(..))") public void logAfter(Joinzyiz joinzyiz) { System.out.println("logAfter() is running!"); System.out.println("hijacked : " + joinzyiz.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.zyiz.aspect; import org.aspectj.lang.Joinzyiz; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.AfterReturning; @Aspect public class LoggingAspect { @AfterReturning( pointcut = "execution(* com.zyiz.customer.bo.CustomerBo.addCustomerReturnValue(..))", returning= "result") public void logAfterReturning(Joinzyiz joinzyiz, Object result) { System.out.println("logAfterReturning() is running!"); System.out.println("hijacked : " + joinzyiz.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.zyiz.aspect; import org.aspectj.lang.Joinzyiz; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.AfterThrowing; @Aspect public class LoggingAspect { @AfterThrowing( pointcut = "execution(* com.zyiz.customer.bo.CustomerBo.addCustomerThrowException(..))", throwing= "error") public void logAfterThrowing(Joinzyiz joinzyiz, Throwable error) { System.out.println("logAfterThrowing() is running!"); System.out.println("hijacked : " + joinzyiz.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()方法执行之前执行, 必须定义“joinzyiz.proceed();” 控制何时拦截器返回控制到原来的addCustomerAround()方法。
File : LoggingAspect.java
package com.zyiz.aspect; import org.aspectj.lang.ProceedingJoinzyiz; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Around; @Aspect public class LoggingAspect { @Around("execution(* com.zyiz.customer.bo.CustomerBo.addCustomerAround(..))") public void logAround(ProceedingJoinzyiz joinzyiz) throws Throwable { System.out.println("logAround() is running!"); System.out.println("hijacked method : " + joinzyiz.getSignature().getName()); System.out.println("hijacked arguments : " + Arrays.toString(joinzyiz.getArgs())); System.out.println("Around before is running!"); joinzyiz.proceed(); //continue on the intercepted method System.out.println("Around after is running!"); System.out.println("******"); } }
运行它
CustomerBo customer = (CustomerBo) appContext.getBean("customerBo"); customer.addCustomerAround("zyiz");
输出结果
logAround() is running! hijacked method : addCustomerAround hijacked arguments : [zyiz] Around before is running! addCustomerAround() is running, args : zyiz Around after is running! ******