Add annotations to Log management based on the previous article “SpringBoot uses @aspect for Log management (based on reflection proxy mode + annotated Log)” update version 1. Modify the Log annotation class

/** * Created by @target ({ElementType.PARAMETER, METHOD}) @Retention(RetentionPolicy.runtime) @documented Public @interface JournalLog { add **/ int operationType() default -1; /** modularTypeName() default-1; /** modularTypeName() default-1; */ String xplain() default""; /** Check type */ Boolean checkBoolean() defaultfalse; [] xplainArray() default"";

}Copy the code

2. Add annotation tool classes

/** * Annotation class * Created by Accn on 2017/9/11. */ public class AnnotationUtils {/** private static final Logger logger = LoggerFactory.getLogger(AnnotationUtils.class); private static AnnotationUtils mInstance; publicAnnotationUtils() {
    }

    public static AnnotationUtils get() {
        if (mInstance == null) {
            synchronized (AnnotationUtils.class) {
                if(mInstance == null) { mInstance = new AnnotationUtils(); }}}returnmInstance; } /** * get annotation attribute value information * @param className className * @param methodName methodName * @param annotationClass * @param attrName annotation attribute name * @throws NotFoundException */ public static Object getAnnotationAttr(String className,String methodName,Class<? > annotationClass,String attrName) throws NotFoundException, NoSuchMethodException { Object value = null;if (StringUtils.isBlank(className))
            return value;
        if (StringUtils.isBlank(methodName))
            return value;
        if (annotationClass==null)
            return value;
        if (StringUtils.isBlank(attrName))
            returnvalue; ClassPool pool = ClassPool.getDefault(); CtClass ct = pool.get(className); CtClass ct = pool.get(className); CtMethod ctMethod = ct.getDeclaredMethod(methodName); MethodInfo methodInfo = ctMethod.getMethodInfo(); AnnotationsAttribute attribute = (AnnotationsAttribute) methodInfo.getAttribute(AnnotationsAttribute.visibleTag); String annotationName = annotationClass.getName(); Method operationType = annotationClass.getDeclaredMethod(attrName); Class<? >returnType = operationType.getReturnType();

        if(attribute! Annotation Annotation = attribute.getannotation (annotationName);if(annotation! =null){// Get the value of the annotationif (int.class.isAssignableFrom(returnType)){
                    IntegerMemberValue memberValue = (IntegerMemberValue) annotation.getMemberValue(attrName);
                    if(memberValue! =null) value = memberValue.getValue() ; }if (String.class.isAssignableFrom(returnType)){
                    StringMemberValue memberValue = (StringMemberValue) annotation.getMemberValue(attrName);
                    if(memberValue! =null) value = memberValue .getValue() ; }if (boolean.class.isAssignableFrom(returnType)){
                    BooleanMemberValue memberValue = (BooleanMemberValue) annotation.getMemberValue(attrName);
                    if(memberValue! =null) value = memberValue.getValue() ; }if (String[].class.isAssignableFrom(returnType)){
                    ArrayMemberValue memberValue = (ArrayMemberValue) annotation.getMemberValue(attrName);
                    if(memberValue! =null){ MemberValue[] strValueArray = memberValue.getValue(); String[] strValueA = new String[strValueArray.length];for(int i = 0; i<strValueArray.length; i++){ StringMemberValue stringMemberValue = (StringMemberValue) strValueArray[i]; strValueA[i] = stringMemberValue.getValue(); } value = strValueA; }}}}returnvalue; } /** * modify annotation attribute * @param className className * @param methodName methodName * @param annotationClass annotation name * @param attrName attribute name * @param Value Modified value * @throws NotFoundException */ public static Boolean updateAnnotationAttr(String className,String) methodName,Class<? > annotationClass,String attrName,Object value) throws NotFoundException, NoSuchMethodException {if (StringUtils.isBlank(className))
            return false;
        if (StringUtils.isBlank(methodName))
            return false;
        if (annotationClass==null)
            return false;
        if (StringUtils.isBlank(attrName))
            return false;
        if (value==null)
            return false; ClassPool pool = ClassPool.getDefault(); CtClass ct = pool.get(className); CtMethod minInfo = ct.getDeclaredMethod(methodName); MethodInfo methodInfo = minInfo.getMethodInfo(); ConstPool cp = methodInfo.getConstPool(); AnnotationsAttribute attribute = new AnnotationsAttribute(cp, AnnotationsAttribute.visibleTag); String annotationName = annotationClass.getName(); Method operationType = annotationClass.getDeclaredMethod(attrName); Class<? >returnType = operationType.getReturnType(); Annotation annotation = new Annotation(annotationName, cp); // Modify the annotation named unitNameif(annotation ! = null) {if (String.class.isAssignableFrom(returnType))
                annotation.addMemberValue(attrName, new StringMemberValue((String) value, cp));
            else if (int.class.isAssignableFrom(returnType))
                annotation.addMemberValue(attrName, new IntegerMemberValue(cp,(Integer)value));
            else if (boolean.class.isAssignableFrom(returnType))
                annotation.addMemberValue(attrName, new BooleanMemberValue((boolean) value, cp));
            else if (String[].class.isAssignableFrom(returnType)){
                String[] stres = (String[])value;
                StringMemberValue[] elements = new StringMemberValue[stres.length];
                for(int i=0; i<stres.length; i++) elements[i] = new StringMemberValue(stres[i],cp); ArrayMemberValue amv = new ArrayMemberValue(cp); amv.setValue(elements); annotation.addMemberValue(attrName,amv); }else
                return false;

            attribute.setAnnotation(annotation);
            methodInfo.addAttribute(attribute);
            return true;
        }
        return false; }}Copy the code

3. Modify the log section class

import com.chen.annotation.JournalLog; import com.chen.exception.CustomException; import com.chen.staticInfos.StaticInfo; import com.chen.utils.AnnotationUtils; import com.chen.utils.JournalUtils; import javassist.NotFoundException; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; /** * Created by Accv on 2017/9/11. */ @component @Aspect public class JournalAspect {/** log output */ private static final Logger logger = LoggerFactory.getLogger(JournalAspect.class); /** / @autoWired Private JournalUtils aspectJournalUtils; /** private final String POINT_CUT = private final String POINT_CUT ="execution(* com.chen.service.. * (..) )";

    @Pointcut(POINT_CUT)
    private void pointcut(){} /** * post final notification (the target method will be executed as soon as it completes) * Log management * @param joinPoint */ @after (value =)"pointcut()")
    @Transactional
    public void doAfterAdvice(JoinPoint joinPoint) throws CustomException, ClassNotFoundException, NotFoundException, NoSuchMethodException { String className = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); int modulerType = -1; int opreationType = -1; modulerType = (int) AnnotationUtils.get().getAnnotationAttr(className, methodName, JournalLog.class, StaticInfo.AOP_LOG_ATTR_NAME1); opreationType = (int) AnnotationUtils.get().getAnnotationAttr(className, methodName, JournalLog.class, StaticInfo.AOP_LOG_ATTR_NAME2); //3. Add logsif(modulerType! =-1&&opreationType! = 1) / / TODO 3.1 from the request for user id aspectJournalUtils. AddJournalInfo (modulerType opreationType, 10086); }}Copy the code

Code link:Git.oschina.net/CatalpaFlat…