springboot aop 自定义注解形式

引入pom

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-aopartifactId>
        dependency>

自定义注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Xxxano{
}

配置切面

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
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.lang.reflect.Method;

@Aspect
@Component
@Slf4j
public class XxxanoAOP {
    @Autowired
    private BizService bizService;
    
    @Pointcut("execution(* com.xx.xxxx.controller..*Controller.*(..)) &&@annotation(com.xx.xxxx.aop.Xxxano)")
    public void pointcut() {
    }

    @Around("pointcut()")
    @SneakyThrows
    public Object arround(ProceedingJoinPoint pjp) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        String methodName = method.getName();
        Object[] args = pjp.getArgs();
        log.info("{}.{}  , param : {}"
                , method.getDeclaringClass().getName()
                , methodName
                , args);

                String head = request.getHeader("head");
              Xxxx x=  bizService.selectXxxxById(head);
              
		  //get传值
		XxxxDTO attr = (XxxxDTO)request.getAttribute("attr");
        attr.setAaa("1111");
        request.setAttribute("attr",attr);
        
//post 传值
        LinkedHashMap<String, Object> param = (LinkedHashMap<String, Object>) args[0];
        if (param.size() > 2) {
            return R.error("参数字段过多");
        }
        param.put("time", LocalDateTime.now().toString());
        Map<String,String> m = new HashMap<>();
        m.put("oooo","ooo1");
        param.put("obj",m);
                

//                todo: 加判断逻辑
        return pjp.proceed(args);
    }
}

java控制层请求接收AOP设置的参数


	@Xxxano
    @GetMapping("/xxx/get")
    @ApiOperation("get请求方法")
    public AjaxResult xxxGet (
            @ApiParam(hidden = true) @RequestAttribute("attr") XxxxDTO xxxDTO
    ){

        return  AjaxResult.success();
    }

springboot aop 自定义注解形式_第1张图片

	@Xxxano
    @PostMapping("/xxx/post")
    @ApiOperation("post请求")
    public AjaxResult xxxPost (
            @ApiParam(value ="{\"aaa\":\"AAA\",\"bbb\":\"BBB\"}",example = "{\"aaa\":\"AAA\",\"bbb\":\"BBB\"}")
            @RequestBody Map<String,Object> map){
        return  AjaxResult.success();
    }

你可能感兴趣的:(spring,boot,后端,java)