ITeye博客

Spring3.0中的AOP配置方法

zywang · 2011-03-24 16:21 · Spring · 企业架构 · Markdown

第一种配置方法:使用@AspectJ标签

  1. 在配置文件中添加<aop:aspectj-autoproxy/>注解
  2. 创建一个Java文件,使用@Aspect注解修饰该类
  3. 创建一个方法,使用@Before、@After、@Around等进行修饰,在注解中写上切入点的表达式

说明:上述Java文件创建好后,需要将其在Spring的容器中进行声明,可以在配置文件中定义<bean/>节点,也可以使用@Component组件进行修饰

示例:

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * 基于注解的AOP日志示例
 * @author ZYWANG 2011-3-24
 */
@Component
@Aspect
public class AopLog {
	
	//方法执行前调用
	@Before("execution (* com.zywang.services.impl.*.*(..))")
	public void before() {
		System.out.println("before");
	}
	
	//方法执行后调用
	@After("execution (* com.zywang.services.impl.*.*(..))")
	public void after() {
		System.out.println("after");
	}
	
	//方法执行的前后调用
	@Around("execution (* com.zywang.services.impl.*.*(..))")
	public Object around(ProceedingJoinPoint point) throws Throwable{
		System.out.println("begin around");
		Object object = point.proceed();
		System.out.println("end around");
		return object;
	}
	
	//方法运行出现异常时调用
	@AfterThrowing(pointcut = "execution (* com.zywang.services.impl.*.*(..))",throwing = "ex")
	public void afterThrowing(Exception ex){
		System.out.println("afterThrowing");
		System.out.println(ex);
	}
}

上面这段代码中多次使用了重复的切入点,这种情况下,可以使用@Pointcut标注,来修改一个切入点方法(这个方法不需要参数和方法体),然后就可以在@Before等标注中引用该方法作为切入点,示例如下:

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 基于注解的AOP日志示例
 * @author ZYWANG 2011-3-24
 */
@Component
@Aspect
public class AopLog {
	
	@Pointcut("execution (* com.iflysse.school.services.impl.*.*(..))")
	public void pointcut(){}
	
	//方法执行前调用
	@Before("pointcut()")
	public void before() {
		System.out.println("before");
	}
	
	//方法执行的前后调用
	@Around("pointcut()")
	public Object around(ProceedingJoinPoint point) throws Throwable{
		System.out.println("begin around");
		Object object = point.proceed();
		System.out.println("end around");
		return object;
	}
}

 

 

 

 

第二种配置方法:基于配置文件的配置

  1. 创建一个Java文件,并指定一个用于执行拦截的方法,该方法可以有0个或多个参数
  2. 在Spring配置文件中注册该Java类为一个Bean
  3. 使用<aop:config/>、<aop:aspect/>等标签进行配置

示例:

Java文件

import org.aspectj.lang.ProceedingJoinPoint;

/**
 * 基于配置文件的AOP日志示例
 * @author ZYWANG 2011-3-24
 */
public class AopLog {
	
	//方法执行的前后调用
	public Object runOnAround(ProceedingJoinPoint point) throws Throwable{
		System.out.println("begin around");
		Object object = point.proceed();
		System.out.println("end around");
		return object;
	}
	
}

 Spring配置文件

	<bean id="aopLog" class="com.iflysse.school.aop.AopLog"></bean>
	
	<aop:config>
		<aop:aspect ref="aopLog">
			<aop:around method="runOnAround" pointcut="execution (* com.zywang.services.impl.*.*(..))"/>
		</aop:aspect>
	</aop:config>

 注意:上面这个示例使用的是around方式的拦截,该方法要求Java类中的方法有一个ProceedingJoinPoint类型的参数

使用第二种方式的AOP配置,在Eclipse(有SpringIDE插件)中被拦截到的方法中有标识显示

 

以上配置基于Spring 3.0.5 进行设置,参考其《Reference Documentation》

评论(6)

  1. #6 楼 · yanlun0323 · 2013-12-09 10:04

    夜曲6763 writes:

    [quote=yanlun0323]博主你好!感谢你的技术分享! 我有些疑问,那个…… @Pointcut("execution (* com.iflysse.school.services.impl..(..))")
    public void pointcut(){}

    方法pointcut具体有什么作用?该方法内部可以写表达式吗?我试过写System.out.println输出语句,没有执行。

    难道只是作为一个引用的表示符?那为什么不用静态常量?将这个静态常量作为一个基础父类的属性,被子类继承,不是也可以吗?

    还有,那个execution到底做了些什么事?[/quote] 是的,pointcut()是个方法签名,不需要实际代码。 execution里面的表达式,让程序知道在哪织入切面,也就是规定连接点。 感谢!!

  2. #5 楼 · 夜曲6763 · 2013-11-18 09:16

    yanlun0323 writes:

    博主你好!感谢你的技术分享! 我有些疑问,那个…… @Pointcut("execution (* com.iflysse.school.services.impl..(..))")
    public void pointcut(){}

    方法pointcut具体有什么作用?该方法内部可以写表达式吗?我试过写System.out.println输出语句,没有执行。

    难道只是作为一个引用的表示符?那为什么不用静态常量?将这个静态常量作为一个基础父类的属性,被子类继承,不是也可以吗?

    还有,那个execution到底做了些什么事?

    是的,pointcut()是个方法签名,不需要实际代码。 execution里面的表达式,让程序知道在哪织入切面,也就是规定连接点。

  3. #4 楼 · yanlun0323 · 2013-10-08 08:32

    博主你好!感谢你的技术分享! 我有些疑问,那个…… @Pointcut("execution (* com.iflysse.school.services.impl..(..))")
    public void pointcut(){}

    方法pointcut具体有什么作用?该方法内部可以写表达式吗?我试过写System.out.println输出语句,没有执行。

    难道只是作为一个引用的表示符?那为什么不用静态常量?将这个静态常量作为一个基础父类的属性,被子类继承,不是也可以吗?

    还有,那个execution到底做了些什么事?

  4. #3 楼 · lzshgdsg · 2013-03-11 16:08

    潜水几年了,今天不得不冒出来 :idea:

  5. #2 楼 · ycyangcai · 2013-01-22 12:33

    很不错! :idea:

  6. #1 楼 · ynyee · 2012-06-17 19:54

    嗯,写的很简明。收了

评论功能维护中,当前仅展示历史评论。