package com.h3.customer.bo; public interface customerbo { void addcustomer(); string addcustomerreturnvalue(); void addcustomerthrowexception() throws exception; void addcustomeraround(string name); }
aspectj @before 示例.
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) { //... } }
在xml同等功能,使用 <aop:before>.
<!-- aspect --> <bean id="logaspect" class="com.h3.aspect.loggingaspect" /> <aop:config> <aop:aspect id="aspectloggging" ref="logaspect" > <!-- @before --> <aop:pointcut id="pointcutbefore" expression="execution(* com.h3.customer.bo.customerbo.addcustomer(..))" /> <aop:before method="logbefore" pointcut-ref="pointcutbefore" /> </aop:aspect> </aop:config>
aspectj @after 示例.
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) { //... } }
在xml同等功能,使用 <aop:after>实现。
<!-- aspect --> <bean id="logaspect" class="com.h3.aspect.loggingaspect" /> <aop:config> <aop:aspect id="aspectloggging" ref="logaspect" > <!-- @after --> <aop:pointcut id="pointcutafter" expression="execution(* com.h3.customer.bo.customerbo.addcustomer(..))" /> <aop:after method="logafter" pointcut-ref="pointcutafter" /> </aop:aspect> </aop:config>
aspectj @afterreturning 示例.
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) { //... } }
在xml同等功能 - 使用 <aop:after-returning>.
<!-- aspect --> <bean id="logaspect" class="com.h3.aspect.loggingaspect" /> <aop:config> <aop:aspect id="aspectloggging" ref="logaspect" > <!-- @afterreturning --> <aop:pointcut id="pointcutafterreturning" expression="execution(* com.h3.customer.bo.customerbo.addcustomerreturnvalue(..))" /> <aop:after-returning method="logafterreturning" returning="result" pointcut-ref="pointcutafterreturning" /> </aop:aspect> </aop:config>
aspectj @afterreturning 示例.
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) { //... } }
在xml同等功能 - 使用 <aop:after-throwing>.
<!-- aspect --> <bean id="logaspect" class="com.h3.aspect.loggingaspect" /> <aop:config> <aop:aspect id="aspectloggging" ref="logaspect" > <!-- @afterthrowing --> <aop:pointcut id="pointcutafterthrowing" expression="execution(* com.h3.customer.bo.customerbo.addcustomerthrowexception(..))" /> <aop:after-throwing method="logafterthrowing" throwing="error" pointcut-ref="pointcutafterthrowing" /> </aop:aspect> </aop:config>
aspectj @around 示例.
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 { //... } }
在xml同等功能 - 使用 <aop:after-around>.
<!-- aspect --> <bean id="logaspect" class="com.h3.aspect.loggingaspect" /> <aop:config> <aop:aspect id="aspectloggging" ref="logaspect" > <!-- @around --> <aop:pointcut id="pointcutaround" expression="execution(* com.h3.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.h3.customer.bo.impl.customerboimpl" /> <!-- aspect --> <bean id="logaspect" class="com.h3.aspect.loggingaspect" /> <aop:config> <aop:aspect id="aspectloggging" ref="logaspect"> <!-- @before --> <aop:pointcut id="pointcutbefore" expression="execution(* com.h3.customer.bo.customerbo.addcustomer(..))" /> <aop:before method="logbefore" pointcut-ref="pointcutbefore" /> <!-- @after --> <aop:pointcut id="pointcutafter" expression="execution(* com.h3.customer.bo.customerbo.addcustomer(..))" /> <aop:after method="logafter" pointcut-ref="pointcutafter" /> <!-- @afterreturning --> <aop:pointcut id="pointcutafterreturning" expression="execution(* com.h3.customer.bo.customerbo.addcustomerreturnvalue(..))" /> <aop:after-returning method="logafterreturning" returning="result" pointcut-ref="pointcutafterreturning" /> <!-- @afterthrowing --> <aop:pointcut id="pointcutafterthrowing" expression="execution(* com.h3.customer.bo.customerbo.addcustomerthrowexception(..))" /> <aop:after-throwing method="logafterthrowing" throwing="error" pointcut-ref="pointcutafterthrowing" /> <!-- @around --> <aop:pointcut id="pointcutaround" expression="execution(* com.h3.customer.bo.customerbo.addcustomeraround(..))" /> <aop:around method="logaround" pointcut-ref="pointcutaround" /> </aop:aspect> </aop:config> </beans>