注解通知的写法


和XML方式的通知不同的是,注解通知采取在切面类的通知方法上面直接加上相应注解即可。

主要有以下5个注解:

1)@Before
2)@After
3)@AfterReturning
4)@AfterThrowing
5)@Around

以下给出切面类的参考案例:

package com.yiidian.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * Spring的AOP的切面类
 * 
 * @author http://www.yiidian.com
 * 
 */
@Aspect
public class MyAspect {

	// 前置通知
	@Before(value = "execution(public * com.yiidian.service.impl.CustomerServiceImpl.*(..))")
	public void before() {
		System.out.println("前置通知");
	}

	// 最终通知
	@After(value = "execution(public * com.yiidian.service.impl.CustomerServiceImpl.*(..))")
	public void after() {
		System.out.println("最终通知");
	}

	// 后置通知
	@AfterReturning(value = "execution(public * com.yiidian.service.impl.CustomerServiceImpl.*(..))")
	public void afterReturning() {
		System.out.println("后置通知");
	}

	// 异常通知
	@AfterThrowing(value = "execution(public * com.yiidian.service.impl.CustomerServiceImpl.*(..))")
	public void afterThrowing() {
		System.out.println("异常通知");
	}

	// 环绕通知
	@Around(value = "execution(public * com.yiidian.service.impl.CustomerServiceImpl.*(..))")
	public void around(ProceedingJoinPoint pjp) {
		System.out.println("前置通知--前面代码");
		//执行目标对象方法
		try {
			pjp.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		System.out.println("前置通知--后面代码");
	}

}

 

源码下载:http://pan.baidu.com/s/1qXDAaVq