Spring 专题
专题目录
您的位置:java > Spring专题 > Spring AOP+AspectJ在XML配置实例
Spring AOP+AspectJ在XML配置实例
作者:--    发布时间:2019-11-20
在本教程中,我们将向你展示如何转换上章节中 spring aop+aspectj 注解转成基于xml的配置。
对于那些不喜欢注释,使用jdk1.4,则可以基于xml,而不使用 aspectj。
再次回顾上个 customerbo 接口中的几个方法,以后你将学会如何在 xml文件实现 aspectj 拦截。
package com.h3.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.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>

2. aspectj <aop:after> = @after

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>

3. aspectj <aop:after-returning> = @afterreturning

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>

4. aspectj <aop:after-throwing> = @afterreturning

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>

5. aspectj <aop:after-around> = @around

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>

完整的 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.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>
下载源代码 – http://pan.baidu.com/s/1nuwrmoh

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册