Spring AOP+AspectJ在XML配置實例
在本教程中,我們將向你展示如何轉換上章節中 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.
<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" />
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實現。
<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" />
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.
<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" />
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.
<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" />
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.
<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" />
完整的 XML 實例
查看完整的基於XML的 AspectJ 配置文件。
<aop:aspectj-autoproxy />
<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" />
下載源代碼 – http://pan.baidu.com/s/1nuwrmOh