在本教程中,我们将向你展示如何转换上章节中 Spring AOP+AspectJ 注解转成基于XML的配置。
对于那些不喜欢注释,使用JDK1.4,则可以基于XML,而不使用 AspectJ。
再次回顾上个 customerBo 接口中的几个方法,以后你将学会如何在 XML文件实现 AspectJ 拦截。
package com.yiibai.customer.bo; public interface CustomerBo { void addCustomer(); String addCustomerReturnValue(); void addCustomerThrowException() throws Exception; void addCustomerAround(String name); }
1. AspectJ <aop:before> = @Before
AspectJ @Before 示例.
package com.yiibai.aspect; import org.aspectj.lang.JoinYiibai; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class LoggingAspect { @Before("execution(* com.yiibai.customer.bo.CustomerBo.addCustomer(..))") public void logBefore(JoinYiibai joinYiibai) { //... } }
在XML同等功能,使用 <aop:before>.
<!-- Aspect --> <bean id="logAspect" class="com.yiibai.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @Before --> <aop:pointcut id="pointCutBefore" expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomer(..))" /> <aop:before method="logBefore" pointcut-ref="pointCutBefore" /> </aop:aspect> </aop:config>
2. AspectJ <aop:after> = @After
AspectJ @After 示例.
package com.yiibai.aspect; import org.aspectj.lang.JoinYiibai; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.After; @Aspect public class LoggingAspect { @After("execution(* com.yiibai.customer.bo.CustomerBo.addCustomer(..))") public void logAfter(JoinYiibai joinYiibai) { //... } }
在XML同等功能,使用 <aop:after>实现。
<!-- Aspect --> <bean id="logAspect" class="com.yiibai.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @After --> <aop:pointcut id="pointCutAfter" expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomer(..))" /> <aop:after method="logAfter" pointcut-ref="pointCutAfter" /> </aop:aspect> </aop:config>
3. AspectJ <aop:after-returning> = @AfterReturning
AspectJ @AfterReturning 示例.
package com.yiibai.aspect; import org.aspectj.lang.JoinYiibai; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.AfterReturning; @Aspect public class LoggingAspect { @AfterReturning( pointcut = "execution(* com.yiibai.customer.bo.CustomerBo.addCustomerReturnValue(..))", returning= "result") public void logAfterReturning(JoinYiibai joinYiibai, Object result) { //... } }
在XML同等功能 - 使用 <aop:after-returning>.
<!-- Aspect --> <bean id="logAspect" class="com.yiibai.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @AfterReturning --> <aop:pointcut id="pointCutAfterReturning" expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomerReturnValue(..))" /> <aop:after-returning method="logAfterReturning" returning="result" pointcut-ref="pointCutAfterReturning" /> </aop:aspect> </aop:config>
4. AspectJ <aop:after-throwing> = @AfterReturning
AspectJ @AfterReturning 示例.
package com.yiibai.aspect; import org.aspectj.lang.JoinYiibai; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.AfterThrowing; @Aspect public class LoggingAspect { @AfterThrowing( pointcut = "execution(* com.yiibai.customer.bo.CustomerBo.addCustomerThrowException(..))", throwing= "error") public void logAfterThrowing(JoinYiibai joinYiibai, Throwable error) { //... } }
在XML同等功能 - 使用 <aop:after-throwing>.
<!-- Aspect --> <bean id="logAspect" class="com.yiibai.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @AfterThrowing --> <aop:pointcut id="pointCutAfterThrowing" expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomerThrowException(..))" /> <aop:after-throwing method="logAfterThrowing" throwing="error" pointcut-ref="pointCutAfterThrowing" /> </aop:aspect> </aop:config>
5. AspectJ <aop:after-around> = @Around
AspectJ @Around 示例.
package com.yiibai.aspect; import org.aspectj.lang.ProceedingJoinYiibai; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Around; @Aspect public class LoggingAspect { @Around("execution(* com.yiibai.customer.bo.CustomerBo.addCustomerAround(..))") public void logAround(ProceedingJoinYiibai joinYiibai) throws Throwable { //... } }
在XML同等功能 - 使用 <aop:after-around>.
<!-- Aspect --> <bean id="logAspect" class="com.yiibai.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect" > <!-- @Around --> <aop:pointcut id="pointCutAround" expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomerAround(..))" /> <aop:around method="logAround" pointcut-ref="pointCutAround" /> </aop:aspect> </aop:config>
完整的 XML 实例
查看完整的基于XML的 AspectJ 配置文件。
<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.yiibai.customer.bo.impl.CustomerBoImpl" /> <!-- Aspect --> <bean id="logAspect" class="com.yiibai.aspect.LoggingAspect" /> <aop:config> <aop:aspect id="aspectLoggging" ref="logAspect"> <!-- @Before --> <aop:pointcut id="pointCutBefore" expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomer(..))" /> <aop:before method="logBefore" pointcut-ref="pointCutBefore" /> <!-- @After --> <aop:pointcut id="pointCutAfter" expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomer(..))" /> <aop:after method="logAfter" pointcut-ref="pointCutAfter" /> <!-- @AfterReturning --> <aop:pointcut id="pointCutAfterReturning" expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomerReturnValue(..))" /> <aop:after-returning method="logAfterReturning" returning="result" pointcut-ref="pointCutAfterReturning" /> <!-- @AfterThrowing --> <aop:pointcut id="pointCutAfterThrowing" expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomerThrowException(..))" /> <aop:after-throwing method="logAfterThrowing" throwing="error" pointcut-ref="pointCutAfterThrowing" /> <!-- @Around --> <aop:pointcut id="pointCutAround" expression="execution(* com.yiibai.customer.bo.CustomerBo.addCustomerAround(..))" /> <aop:around method="logAround" pointcut-ref="pointCutAround" /> </aop:aspect> </aop:config> </beans>
下载源代码 – http://pan.baidu.com/s/1nuwrmOh
易百教程移动端:请扫描本页面底部(右侧)二维码并关注微信公众号,回复:"教程" 选择相关教程阅读或直接访问:http://m.yiibai.com 。
加QQ群啦,易百教程官方技术学习群
注意:建议每个人选自己的技术方向加群,同一个QQ最多限加3个群。
- Java技术群: 227270512 (人数:2000,等级:LV5,免费:否)
- MySQL/SQL群: 418407075 (人数:2000,等级:LV5,免费:否)
- 大数据开发群: 655154550 (人数:2000,等级:LV5,免费:否)
- Python技术群: 287904175 (人数:2000,等级:LV5,免费:否)
- 人工智能深度学习: 456236082 (人数:2000,等级:LV5,免费:否)
- 测试工程师(新群): 415553199 (人数:1000,等级:LV1,免费:是)
- 前端技术群(新群): 410430016 (人数:1000,等级:LV1,免费:是)
- C/C++技术(新群): 629264796 (人数:1000,等级:LV1,免费:是)
- Node.js技术(新群): 621549808 (人数:1000,等级:LV1,免费:是)
- PostgreSQL数据库(新群): 539504187 (人数:1000,等级:LV1,免费:否)
- Linux技术: 479429477 (人数:2000,等级:LV5,免费:否)
- PHP开发者: 460153241 (人数:2000,等级:LV5,免费:是)
- Oracle数据库: 175248146 (人数:2000,等级:LV5,免费:是)
- C#/ASP.Net开发者: 579821706 (人数:2000,等级:LV5,免费:是)
- 数据分析师: 397883996 (人数:1000,等级:LV1,免费:是)R语言,Matlab语言等技术