Spring AOP 5种切面方式的应用(顶)

来源:互联网 发布:东华大学数据库试卷 编辑:程序博客网 时间:2024/05/16 18:28
import java.util.Map;import javax.servlet.http.HttpServletRequest;import org.apache.log4j.Logger;import org.apache.shiro.web.servlet.ShiroHttpServletRequest;import org.aspectj.lang.JoinPoint;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;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;/** * 类说明:系统服务组件Aspect切面Bean *  * @author 作者 LzwGlory * @version 创建时间:2015年12月14日 下午5:36:13 */@Component@Aspectpublic class ServiceAspect {private static final Logger log = Logger.getLogger(ServiceAspect.class);// 配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点@Pointcut("execution(* com.ultrapower.rw.web.ows.controller.app..*(..))")public void aspect() {}/* * 配置前置通知,使用在方法aspect()上注册的切入点 同时接受JoinPoint切入点对象,可以没有该参数 */@Before("aspect()")public void before(JoinPoint joinPoint) {Object[] args = joinPoint.getArgs();// 获得目标方法的参数String name = joinPoint.getSignature().getName();// 获得目标方法名log.info("<=============" + name + "方法--AOP 前置通知=============>");if (args != null && args.length > 0&& args[0].getClass() == ShiroHttpServletRequest.class) {HttpServletRequest request = (HttpServletRequest) joinPoint.getArgs()[0];String requestURI = request.getRequestURI();@SuppressWarnings("unchecked")Map<String, String> parameterMap = request.getParameterMap();StringBuilder paramStr = new StringBuilder();for (Map.Entry<String, String> param : parameterMap.entrySet()) {paramStr.append(param.getKey()).append("=").append(param.getValue());}if (paramStr.length() > 0) {requestURI = requestURI + "?" + paramStr.toString();}log.info(name + " 方法请求路径与参数:" + requestURI);}}// 配置后置通知,使用在方法aspect()上注册的切入点@After("aspect()")public void after(JoinPoint joinPoint) {if (log.isInfoEnabled()) {String name = joinPoint.getSignature().getName();// 获得目标方法名log.info("<=============" + name + "方法--AOP 后置通知=============>");}}// 配置环绕通知,使用在方法aspect()上注册的切入点@Around("aspect()")public Object around(ProceedingJoinPoint joinPoint) {String name = joinPoint.getSignature().getName();// 获得目标方法名log.info("<=============" + name + "方法--AOP 环绕通知=============>");long start = System.currentTimeMillis();Object result = null;try {result = joinPoint.proceed();long end = System.currentTimeMillis();if (log.isInfoEnabled()) {log.info("around " + joinPoint + "\tUse time : "+ (end - start) + " ms!");}} catch (Throwable e) {long end = System.currentTimeMillis();if (log.isInfoEnabled()) {log.info("around " + joinPoint + "\tUse time : "+ (end - start) + " ms with exception : "+ e.getMessage());}}return result;}// 配置后置返回通知,使用在方法aspect()上注册的切入点@AfterReturning(pointcut = "aspect()", returning = "result")public void afterReturn(JoinPoint joinPoint, Object result) {String name = joinPoint.getSignature().getName();// 获得目标方法名log.info("<=============" + name + "方法--AOP 后置返回通知=============>");log.info(name + "方法返回参数:" + result);}// 配置抛出异常后通知,使用在方法aspect()上注册的切入点@AfterThrowing(pointcut = "aspect()", throwing = "ex")public void afterThrow(JoinPoint joinPoint, Exception ex) {String name = joinPoint.getSignature().getName();// 获得目标方法名log.info("<=============" + name + "方法--AOP 异常后通知=============>");log.info(name + "方法抛出异常为:" + "\t" + ex.getMessage());}}

0 0