Spring AOP + AspectJ的XML配置示例

在本教程中,我们向您展示如何将最后一个Spring AOP + AspectJ注释转换为基于XML的配置。

对于那些不喜欢注释或不使用JDK 1.4的用户,可以在基于XML的方面使用AspectJ。

再次使用几种方法再次查看最后一个customerBo接口,稍后您将学习如何通过AspectJ截取XML文件中的接口。

package com.mkyong.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.mkyong.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class LoggingAspect {

	@Before("execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))")
	public void logBefore(JoinPoint joinPoint) {
		//...
	}

}

XML的等效功能,带有<aop:before>

<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" />

<aop:config>

  <aop:aspect id="aspectLoggging" ref="logAspect" >

     <!-- @Before -->
     <aop:pointcut id="pointCutBefore"
	expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" />

     <aop:before method="logBefore" pointcut-ref="pointCutBefore" />
			
  </aop:aspect>

</aop:config>

2. AspectJ <aop:after> = @After

AspectJ @After示例。

package com.mkyong.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.After;

@Aspect
public class LoggingAspect {

	@After("execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))")
	public void logAfter(JoinPoint joinPoint) {
		//...
	}

}

XML的等效功能,带有<aop:after>

<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" />

<aop:config>

  <aop:aspect id="aspectLoggging" ref="logAspect" >

     <!-- @After -->
     <aop:pointcut id="pointCutAfter"
	expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" />

     <aop:after method="logAfter" pointcut-ref="pointCutAfter" />
			
  </aop:aspect>

</aop:config>

3. AspectJ <aop:返回后> = @AfterReturning

AspectJ @AfterReturning示例。

package com.mkyong.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterReturning;

@Aspect
public class LoggingAspect {

  @AfterReturning(
   pointcut = "execution(* com.mkyong.customer.bo.CustomerBo.addCustomerReturnValue(..))",
   returning= "result")
   public void logAfterReturning(JoinPoint joinPoint, Object result) {
	//...
   }

}

XML中的等效功能,带有<aop:after-returning>

<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" />

<aop:config>

  <aop:aspect id="aspectLoggging" ref="logAspect" >

    <!-- @AfterReturning -->
    <aop:pointcut id="pointCutAfterReturning"
      expression="execution(* com.mkyong.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.mkyong.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterThrowing;

@Aspect
public class LoggingAspect {

  @AfterThrowing(
   pointcut = "execution(* com.mkyong.customer.bo.CustomerBo.addCustomerThrowException(..))",
   throwing= "error")
  public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
	//...
  }
}

XML中的等效功能,带有<aop:after-throwing>

<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" />

<aop:config>

  <aop:aspect id="aspectLoggging" ref="logAspect" >

    <!-- @AfterThrowing -->
    <aop:pointcut id="pointCutAfterThrowing"
      expression="execution(* com.mkyong.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.mkyong.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;

@Aspect
public class LoggingAspect {

	@Around("execution(* com.mkyong.customer.bo.CustomerBo.addCustomerAround(..))")
	public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
		//...
	}
	
}

XML中的等效功能,带有<aop:after-around>

<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" />

<aop:config>

   <aop:aspect id="aspectLoggging" ref="logAspect" >

    <!-- @Around -->
   <aop:pointcut id="pointCutAround"
      expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerAround(..))" />
			
   <aop:around method="logAround" pointcut-ref="pointCutAround"  />
			
  </aop:aspect>

</aop:config>

完整的XML示例

请参阅完整的基于AspectJ 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.mkyong.customer.bo.impl.CustomerBoImpl" />

<!-- Aspect -->
<bean id="logAspect" class="com.mkyong.aspect.LoggingAspect" />

<aop:config>

  <aop:aspect id="aspectLoggging" ref="logAspect">

    <!-- @Before -->
    <aop:pointcut id="pointCutBefore"
      expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" />

    <aop:before method="logBefore" pointcut-ref="pointCutBefore" />

    <!-- @After -->
    <aop:pointcut id="pointCutAfter"
       expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomer(..))" />

    <aop:after method="logAfter" pointcut-ref="pointCutAfter" />

    <!-- @AfterReturning -->
    <aop:pointcut id="pointCutAfterReturning"
       expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerReturnValue(..))" />

    <aop:after-returning method="logAfterReturning"
      returning="result" pointcut-ref="pointCutAfterReturning" />

    <!-- @AfterThrowing -->
    <aop:pointcut id="pointCutAfterThrowing"
      expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerThrowException(..))" />

    <aop:after-throwing method="logAfterThrowing"
      throwing="error" pointcut-ref="pointCutAfterThrowing" />

    <!-- @Around -->
    <aop:pointcut id="pointCutAround"
      expression="execution(* com.mkyong.customer.bo.CustomerBo.addCustomerAround(..))" />

    <aop:around method="logAround" pointcut-ref="pointCutAround" />

  </aop:aspect>

</aop:config>

</beans>

下载源代码

下载它– Spring3-AOP-AspectJ-XML-Example.zip (8 KB)

参考文献

  1. Spring AOP + AspectJ注释示例
  2. AspectJ编程指南
  3. Spring AOP + AspectJ参考

翻译自: https://mkyong.com/spring3/spring-aop-aspectj-in-xml-configuration-example/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值