AOP实现Controller参数日志
生活随笔
收集整理的這篇文章主要介紹了
AOP实现Controller参数日志
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
package com.jie.common;import java.lang.annotation.*;/*** @author wuchunjie* @date 2018/2/23*/
@Retention(RetentionPolicy.RUNTIME)//注解會在class中存在,運行時可通過反射獲取
@Target(ElementType.METHOD)//目標(biāo)是方法
@Documented//文檔生成時,該注解將被包含在javadoc中,可去掉
public @interface OperationLogger {/*** 模塊名字*/String modelName() default "";/*** 操作類型*/String option();
} package com.jie.common;import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;/*** @author wuchunjie* @date 2018/2/23*/
@Aspect
@Component
public class SysLogAspect {private static final Logger logger = Logger.getLogger(SysLogAspect.class);/*** 定義Pointcut,Pointcut的名稱,此方法不能有返回值,該方法只是一個標(biāo)示*/@Pointcut("@annotation(com.jie.common.OperationLogger)")public void controllerAspect(){System.out.println("我是一個切入點");}/*** 前置通知(Before advice) :在某連接點(JoinPoint)之前執(zhí)行的通知,但這個通知不能阻止連接點前的執(zhí)行。* @param joinPoint*/@Before("controllerAspect()")public void doBefore(JoinPoint joinPoint){System.out.println("=====SysLogAspect前置通知開始=====");//handleLog(joinPoint, null);}/*** 后通知(After advice) :當(dāng)某連接點退出的時候執(zhí)行的通知(不論是正常返回還是異常退出)。* @param joinPoint*/@AfterReturning(pointcut = "controllerAspect()")public void doAfter(JoinPoint joinPoint){System.out.println("=====SysLogAspect后置通知開始=====");//handleLog(joinPoint, null);}/*** 拋出異常后通知(After throwing advice) : 在方法拋出異常退出時執(zhí)行的通知。* @param joinPoint* @param e*/@AfterThrowing(value = "controllerAspect()", throwing = "e")public void doAfter(JoinPoint joinPoint, Exception e){System.out.println("=====SysLogAspect異常通知開始=====");//handleLog(joinPoint, e);}/*** 環(huán)繞通知(Around advice) :包圍一個連接點的通知,類似Web中Servlet規(guī)范中的Filter的doFilter方法。可以在方法的調(diào)用前后完成自定義的行為,也可以選擇不執(zhí)行。* @param joinPoint*/@Around("controllerAspect()")public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable{System.out.println("=====SysLogAspect 環(huán)繞通知開始=====");//handleLog(joinPoint, null);Object obj= joinPoint.proceed();System.out.println("=====SysLogAspect 環(huán)繞通知結(jié)束=====");return obj;}/*** 日志處理** @param joinPoint* @param e*/private void handleLog(JoinPoint joinPoint, Exception e){try{//獲得注解OperationLogger logger = giveController(joinPoint);if (logger == null){return;}String signature = joinPoint.getSignature().toString(); // 獲取目標(biāo)方法簽名String methodName = signature.substring(signature.lastIndexOf(".") + 1,signature.indexOf("("));String longTemp = joinPoint.getStaticPart().toLongString();String classType = joinPoint.getTarget().getClass().getName();Class<?> clazz = Class.forName(classType);Method[] methods = clazz.getDeclaredMethods();System.out.println("methodName: " + methodName);for (Method method : methods){if (method.isAnnotationPresent(OperationLogger.class)&& method.getName().equals(methodName)){//OpLogger logger = method.getAnnotation(OpLogger.class);String clazzName = clazz.getName();System.out.println("clazzName: " + clazzName + ", methodName: "+ methodName);}}} catch (Exception exp){logger.error("異常信息:{}", exp);exp.printStackTrace();}}/*** 獲得注解* @param joinPoint* @return* @throws Exception*/private static OperationLogger giveController(JoinPoint joinPoint) throws Exception{Signature signature = joinPoint.getSignature();MethodSignature methodSignature = (MethodSignature) signature;Method method = methodSignature.getMethod();if (method != null){return method.getAnnotation(OperationLogger.class);}return null;}}
spring-mvc.xml
<!-- 1、配置映射器與適配器 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- aop --> <bean id="logService" class="com.jie.common.SysLogAspect"></bean> <!-- 開啟切面編程功能 --> <aop:aspectj-autoproxy proxy-target-class="true"/>切到controller的參數(shù)之后,將pojo直接反序列化為參數(shù)保存到數(shù)據(jù)庫中即可
轉(zhuǎn)載于:https://my.oschina.net/wugong/blog/1632553
總結(jié)
以上是生活随笔為你收集整理的AOP实现Controller参数日志的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA WEB部分易混淆问题总结
- 下一篇: C# WebApi+Task+WebSo