spring2.5开启注解AOP

第一步:开启注解




	
	 
	 
	     
	

第二步:


package com.ssh.aop;

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;

@Aspect
public class AspectAop {

	/**
	 * 前置通知
	 */
	@Before("execution(* com.ssh.service.UserService.getUser(..))")
	public void before() {
		System.out.println("前置通知....");
	}

	/**
	 * 后置通知 returnVal,切点方法执行后的返回值
	 */
	@AfterReturning(value = "execution(* com.ssh.service.UserService.getUser(..))", returning = "returnVal")
	public void AfterReturning(Object returnVal) {
		System.out.println("后置通知...." + returnVal);
	}

	/**
	 * 环绕通知
	 * 
	 * @param joinPoint 可用于执行切点的类
	 * @return
	 * @throws Throwable
	 */
	@Around("execution(* com.ssh.service.UserService.getUser(..))")
	public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("环绕通知前....");
		Object obj = (Object) joinPoint.proceed();
		System.out.println("环绕通知后....");
		return obj;
	}

	/**
	 * 抛出通知
	 * 
	 * @param e
	 */
	@AfterThrowing(value = "execution(* com.ssh.service.UserService.getUser(..))", throwing = "e")
	public void afterThrowable(Throwable e) {
		System.out.println("出现异常:msg=" + e.getMessage());
	}

	/**
	 * 无论什么情况下都会执行的方法
	 */
	@After(value = "execution(* com.ssh.service.UserService.getUser(..))")
	public void after() {
		System.out.println("最终通知....");
	}
}

可在切入点方法里面加入:

System.out.println("getuser方法体***********************************************>>");

以便测试运行顺序

当程序运行到getUser(...)方法时控制台输出:

spring2.5开启注解AOP_第1张图片

 

你可能感兴趣的:(三大框架)