注解形式的通知
package com.zhujie; import java.lang.management.GarbageCollectorMXBean; import org.aopalliance.intercept.Invocation; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; 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.springframework.stereotype.Component; 0 @Component("jj") //注解 @Aspect //声明通知类 public class zhujietongzhi { @Before("execution(public * addstudent(..))") public void mybefore(JoinPoint jp) { System.out.println("注解形式前置通知"); System.out.println(jp.getTarget()+"\n"+jp.getArgs().length+"\n"); } @AfterReturning(pointcut="execution(public * addstudent(..))",returning="returnvalue") public void myafter(JoinPoint jp ,Object returnvalue/*方法没有返回值则不用*/) { System.out.println("注解形式后置通知"); System.out.print(jp.getThis()); } @AfterThrowing(pointcut="execution(public * addstudent(..))",throwing="e") public void myexception(JoinPoint jp ,IndexOutOfBoundsException e) { System.out.println("注解形式的异常通知"); System.out.println(e.getMessage()); } @Around("execution(public * addstudent(..))") public void myaround(ProceedingJoinPoint pjp) { try { System.out.println("beforex"); pjp.proceed(); System.out.println("endx"); } catch (Exception e) { // TODO Auto-generated catch block System.out.println("error1x"); } catch (Throwable e) { // TODO Auto-generated catch block System.out.println("error1x"); }finally { System.out.println("end-advice"); } } }
package com.bysen.web.controller.tool; import org.apache.commons.lang3.builder.ToStringBuilder; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; /** * 日志管理: * @author qinyi **/ @Aspect @Service public class LoggerAdvice { protected Logger logger = LoggerFactory.getLogger(this.getClass()); @Before("within(com.bysen.web..*) && @annotation(loggerManage)") public void addBefoLogger(JoinPoint joinPoint,LoggerManage loggerManage){ logger.info("######################################################"); logger.info("执行"+loggerManage.description()+"开始"); logger.info(joinPoint.getSignature().toString()); logger.info(parseParames(joinPoint.getArgs())); logger.info("######################################################"); } @AfterReturning("within(com.bysen.web..*) && @annotation(loggerManage)") public void addAfterRetruningLogger(JoinPoint joinPoint,LoggerManage loggerManage){ logger.info("执行"+loggerManage.description()+" 结束"); } @AfterThrowing(pointcut = "within(com.bysen.web..*) && @annotation(loggerManage)", throwing ="ex") public void addAfterThrowingLogger(JoinPoint joinPoint,LoggerManage loggerManage, Exception ex){ logger.error("执行"+loggerManage.description()+" 异常",ex); } private String parseParames(Object[] parames){ if(null == parames || parames.length <= 0 || parames.length>1024){ return ""; } StringBuffer param = new StringBuffer("传入参数[{}] "); for(Object obj : parames){ param.append(ToStringBuilder.reflectionToString(obj)).append(" "); } return param.toString(); } }
package com.bysen.web.controller.tool; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 日志注解 * @author yu * */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface LoggerManage { public String description(); }
package com.bysen.web.controller.tool; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; @Aspect @Component public class LoginAspect { private Logger logger = LoggerFactory.getLogger(this.getClass()); //声明切入点 @Pointcut("@annotation(com.bysen.web.controller.tool.LoggerManage)") public void poincut(){} //定义前切切面 @Before("poincut()") public void doBefore(JoinPoint joinPoint){ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); logger.info("ip: " + request.getRemoteAddr()); logger.info("url:" + request.getRequestURL().toString()); logger.info("HTTP_METHOD:" + request.getMethod()); //joinPoint.getSignature()获取切入点的相关信息 logger.info("target: " + joinPoint.getSignature()); logger.info("class_method:" + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName()); logger.info("args:" + Arrays.toString(joinPoint.getArgs())); } //定义return切面 @AfterReturning(pointcut = "poincut()", returning = "rep") public void doAfterReturn(Object rep){ logger.info("rep: " + rep); } @Around("poincut()") public Object around(ProceedingJoinPoint point) { Object result = null; long beginTime = System.currentTimeMillis(); try { result = point.proceed(); } catch (Throwable e) { e.printStackTrace(); } long time = System.currentTimeMillis() - beginTime; logger.info("进入接口运行时长 " + time); return result; } }