SpringAOP / programming examples section

Original: Reprinted indicate the original address https://www.cnblogs.com/fanerwei222/p/11833954.html

 

Spring AOP / cut programming examples and some notes, mainly using annotations to achieve the specific theory not much to say here, because practice makes perfect, a lot of techniques and methods Logically, it should first learn theory, but too deep Exploration will fall into the trap of academic theory inside, there are some theoretical concepts of knowledge then you can make some real, then slowly understand the technology used in combat or some of the annotation features is what, again slowly combine theoretical knowledge strengthen consolidate their understanding, or I tell you straight up @Aspect is doing with, @Pointcut is doing with, AOP there are a few key points, the effect is not good, these just say it again, then looked about there are several layers after knowing something can begin to combat a comprehensive theoretical knowledge of practical experience to remember! in my opinion it is the best way to learn a technician.

First class notes:

import java.lang.annotation.*;

/**
 * ALL
 * Record operating notes
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface OperateRecord {
    /**
     * Belongs controller name
     * @Return 
     * / 
    String controllername () default "Default" ;
     / **
     * The type of operation (CRUD)
     * @Return 
     * / 
    String operateType () default "Default" ;
     / **
     * Belongs module
     * @Return 
     * / 
    String Module () default "Default" ;
}

The following is a highlight annotation processing class:

/**
 * ALL
 * Class operation recording processing section
 */
@Aspect
@Component
public class OperateRecordAspect {

    @Resource
    private OperateRecordService recordService;

    /**
     * The client ip address
     */
    private String clientIp = "";
    /**
     * The type of operation
     */
    private String operateType = "";
    /**
     * Operation Controller
     */
    private String operateController = "";
    /**
     * Belongs to the module name
     */
    private String module = "";
    /**
     * Operating records starting point, here is a comment like @annotation
     */
    @Pointcut("@annotation(com.xxx.OperateRecord)")
    public void logOperateRecordPointCut() {

    }

    /**
     Rear notification (@After value is above the entry point) operation record entry points *
     * @Param Joinpoint connection point operation record
      * / 
    @After ( "logOperateRecordPointCut ()" )
     public  void afterPointCut (the JoinPoint Joinpoint) {
         the try {
             / **
             * The method of acquiring information via a connection point, and information acquired annotation
             */
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            OperateRecord record = method.getAnnotation(OperateRecord.class);
            HttpServletRequest REQUEST = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
            clientIp = IpUtils.getIp(REQUEST);
            operateType = record.operateType();
            operateController = record.controllerName();
            module = record.controllerName();
            /**
             * Setting the recording information entity class
             */
            OperateRecord recordEntity = new OperateRecord();
            recordEntity.setId(UuidUtils.getUUID());
            recordEntity.setClientIp(clientIp);
            recordEntity.setCreateTime(new Date());
            recordEntity.setModule(module);
            recordEntity.setOperateController(operateController);
            recordEntity.setOperateType(operateType);

            recordService.addOperateRecord(recordEntity);
        } The catch (Exception E) {
             // an exception than dealing directly ignored 
            System.out.println ( "This information is purely for information does not affect the operation of the program runs ------ add information recording section Exception Handling ---- --- " );
        }
    }

    /**
     * Exception notification: target method exception occurs when the following code is executed
     * Value = "execution (* com.xxxx.impl. *. * (..))" matching performed in all methods of all classes in the packet as an entry point
     * @param joinPoint
     * @param e
     */
    @AfterThrowing(value="execution(* com.xxxx.impl.*.*(..))",throwing="e")
    public void afterThorwingMethod(JoinPoint joinPoint, NullPointerException e){
        try {
            /**
             * The method of acquiring information via a connection point, and information acquired annotation
             */
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            OperateRecord annotation = method.getAnnotation(OperateRecord.class);
        } The catch (Exception EX) {
             // abnormality and leave 
        }
    }

}

Above classes and service entity and UuidUtils where user-written, different services have different approach.

Guess you like

Origin www.cnblogs.com/fanerwei222/p/11833954.html