Spring-aop切面并取出参数转换为实体参数测试

1.TestAspectAnnotation

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

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface TestAspectAnnotation {

    int value() default 0;
}

2.AspectService

import com.xxz.common.utils.BeanConvertUtil;
import com.xx.web.domain.request.assistance.AssistanceGetReq;
import lombok.extern.slf4j.Slf4j;
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;

/**
 * @ClassName AspectService
 * @Author fetter
 * @Date 2024/1/23
 */

@Aspect
@Component
@Slf4j
public class AspectService {


    @Pointcut("@annotation(com.xx.web.aspect.TestAspectAnnotation)")
    private void aspectPoint() {
    }

    @Around("aspectPoint()")
    public void afterAspectTest(ProceedingJoinPoint joinPoint) {

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();


        System.err.println("[" + signature.getName() + " : start.....]");

        // 取出参数并转换为对应实体对象
        Object[] args = joinPoint.getArgs();
        AssistanceGetReq assistanceGetReq = BeanConvertUtil.convert(args[0], AssistanceGetReq.class);
        System.err.println("arg:" + assistanceGetReq.getYear());


    }
}

3.BeanConvertUtil

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * BeanUtils List转换
 *
 */
@Slf4j
public class BeanConvertUtil {


    /**
     * 单个类转换
     *
     * @param sourceObj
     * @param targetClass
     * @param <T>
     * @return
     */
    public static <T> T convert(Object sourceObj, Class<T> targetClass) {
        if (sourceObj == null || targetClass == null) {
            return null;
        }
        T targetObj = null;
        try {
            targetObj = targetClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            log.error("sourceObj:{},targetClass:{}", sourceObj, targetClass, e);
            return null;
        }
        BeanUtils.copyProperties(sourceObj, targetObj);
        return targetObj;
    }

    /**
     * List之间转换
     *
     * @param sourceList
     * @param targetClass
     * @param <T>
     * @return
     */
    public static <T> List<T> convertList(List<?> sourceList, Class<T> targetClass) {
        if (CollectionUtils.isEmpty(sourceList) || targetClass == null) {
            return new ArrayList<>();
        }
        return sourceList.stream().map(sourceObj -> convert(sourceObj, targetClass)).collect(Collectors.toList());
    }

}


4.controller

@RequestMapping("/assistance/")
@RestController
@Component
public class AssistanceController {

    @Resource
    private AssistanceService assistanceService;
    @PostMapping("getAssistanceList")
    @TestAspectAnnotation(value = 1)
    public PageResult<AssistanceGetRes> getAssistanceList(@RequestBody AssistanceGetReq assistanceGetReq) {
        Page<AssistanceGetRes> resPage = assistanceService.getAssistancePage(assistanceGetReq);
        return new PageResult<>(resPage.getTotal(), resPage.getResult());
    }

5.测试
Spring-aop切面并取出参数转换为实体参数测试_第1张图片

你可能感兴趣的:(spring)