springboot how to use aop

Some tips mess it
Import org.hibernate.validator.constraints.NotEmpty;
the @NotEmpty (the Message = "Name Required")

 @PostMapping("/create")
    public ResultVO<Map<String, String>> create(@Valid OrderForm orderForm,
                                                BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【创建订单】参数不正确, orderForm={}", orderForm);
            throw new SellException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }

@Min (value = 18, message = " minors into the binary")
Private int Age; // This intercept, then controller in front of the parameter object plus @Valid comment, an error message will be placed BindingResult bindResult this object
(Girl @Valid Girl, the BindingResult bindResult) {
IF (bindResult.hasErrors ()) {
System.out.println (bindResult.getFieldError.getDefaultMessage ());
}
}
@JsonIgnore added ignored when it turns on the attribute value json properties
on @DynamicUpdate added to the class, it is the database is set to field type timestamp, and CreateTime updatetime automatically updated
public class ProductInfo {

The following step is the real use aop
maven dependent

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-aop</artifactId>
   <version>1.5.3.RELEASE</version>
</dependency>

package com.imooc.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

//todo boke
@Aspect
@Component
@Slf4j
public class HttpAspect {
    /**
     * 这样不用在每个before , after上面都写一次路径了
     */
    @Pointcut("execution(public * com.imooc.controller.HelloController.*(..))")
    public void log(){
    }

    @Before("log()")
    public void doBefore(JoinPoint joinPoint){

        /**
         * HttpServletRequest  javax.servlet.http.HttpServletRequest;
         */
        ServletRequestAttributes attributes=(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=attributes.getRequest();
        //url
        log.info("url={}",request.getRequestURL());
        //method
        log.info("method={}",request.getMethod());
        //ip
        log.info("ip={}",request.getRemoteAddr());
        //获取请求的哪个类的哪个方法
        log.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName());
        //参数
        log.info("args={}",joinPoint.getArgs());
        System.out.println("before");
    }

    @After("log()")
    public void logAfter(){
        System.out.println("after");
    }

    /**
     * 获取切点方法的返回值
     * @param object
     */
    @AfterReturning(returning = "object",pointcut = "log()")
    public void doAfterReturning(Object object){
        log.info("response={}",object);
    }
}

Guess you like

Origin blog.csdn.net/sinat_36748650/article/details/89333361