package com.zyiz.customer.bo; public interface CustomerBo { void addCustomer(); String addCustomerReturnValue(); void addCustomerThrowException() throws Exception; void addCustomerAround(String name); }
AspectJ @Before 示例.
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) { //... } }
在XML同等功能,使用 <aop:before>.
<!-- Aspect --> <bean id="logAspect" class="com.zyiz.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @Before --> <aop:pointcut id="pointCutBefore" expression="execution(* com.zyiz.customer.bo.CustomerBo.addCustomer(..))" /> <aop:before method="logBefore" pointcut-ref="pointCutBefore" /> </aop:aspect> </aop:config>
AspectJ @After 示例.
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) { //... } }
在XML同等功能,使用 <aop:after>实现。
<!-- Aspect --> <bean id="logAspect" class="com.zyiz.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @After --> <aop:pointcut id="pointCutAfter" expression="execution(* com.zyiz.customer.bo.CustomerBo.addCustomer(..))" /> <aop:after method="logAfter" pointcut-ref="pointCutAfter" /> </aop:aspect> </aop:config>
AspectJ @AfterReturning 示例.
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) { //... } }
在XML同等功能 - 使用 <aop:after-returning>.
<!-- Aspect --> <bean id="logAspect" class="com.zyiz.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @AfterReturning --> <aop:pointcut id="pointCutAfterReturning" expression="execution(* com.zyiz.customer.bo.CustomerBo.addCustomerReturnValue(..))" /> <aop:after-returning method="logAfterReturning" returning="result" pointcut-ref="pointCutAfterReturning" /> </aop:aspect> </aop:config>
AspectJ @AfterReturning 示例.
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) { //... } }
在XML同等功能 - 使用 <aop:after-throwing>.
<!-- Aspect --> <bean id="logAspect" class="com.zyiz.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @AfterThrowing --> <aop:pointcut id="pointCutAfterThrowing" expression="execution(* com.zyiz.customer.bo.CustomerBo.addCustomerThrowException(..))" /> <aop:after-throwing method="logAfterThrowing" throwing="error" pointcut-ref="pointCutAfterThrowing" /> </aop:aspect> </aop:config>
AspectJ @Around 示例.
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 { //... } }
在XML同等功能 - 使用 <aop:after-around>.
<!-- Aspect --> <bean id="logAspect" class="com.zyiz.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @Around --> <aop:pointcut id="pointCutAround" expression="execution(* com.zyiz.customer.bo.CustomerBo.addCustomerAround(..))" /> <aop:around method="logAround" pointcut-ref="pointCutAround" /> </aop:aspect> </aop:config>
<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" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect"> <!-- @Before --> <aop:pointcut id="pointCutBefore" expression="execution(* com.zyiz.customer.bo.CustomerBo.addCustomer(..))" /> <aop:before method="logBefore" pointcut-ref="pointCutBefore" /> <!-- @After --> <aop:pointcut id="pointCutAfter" expression="execution(* com.zyiz.customer.bo.CustomerBo.addCustomer(..))" /> <aop:after method="logAfter" pointcut-ref="pointCutAfter" /> <!-- @AfterReturning --> <aop:pointcut id="pointCutAfterReturning" expression="execution(* com.zyiz.customer.bo.CustomerBo.addCustomerReturnValue(..))" /> <aop:after-returning method="logAfterReturning" returning="result" pointcut-ref="pointCutAfterReturning" /> <!-- @AfterThrowing --> <aop:pointcut id="pointCutAfterThrowing" expression="execution(* com.zyiz.customer.bo.CustomerBo.addCustomerThrowException(..))" /> <aop:after-throwing method="logAfterThrowing" throwing="error" pointcut-ref="pointCutAfterThrowing" /> <!-- @Around --> <aop:pointcut id="pointCutAround" expression="execution(* com.zyiz.customer.bo.CustomerBo.addCustomerAround(..))" /> <aop:around method="logAround" pointcut-ref="pointCutAround" /> </aop:aspect> </aop:config> </beans>