Springbootカスタムアノテーションとパラメータ転送とシンプルなアプリケーション

Springbootカスタムアノテーションとパラメータ転送とシンプルなアプリケーション

1.ディレクトリ構造:

1.1注釈はカスタム注釈位置です

ここに画像の説明を挿入
ここに画像の説明を挿入

2.カスタム注釈

2.1 2つの注釈LogControllerとTimeConsumingをカスタマイズして、ログと時間のかかる統計メソッドを記録します。LogControllerには3つのパラメーターがあります。

ここに画像の説明を挿入

@Target({
    
    ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogController {
    
    
    // 具体操作
    String description();

    // 日志级别
    int logLevel() default LogLevelConstant.INFO;

    // 日志进程/方法名
    String method();
}

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

3.注釈の使用

3.1ロギングと時間のかかる統計的手法を必要とする手法に注釈を使用する

 	@PostMapping("/createUser")
    @LogController(description = "创建用户", method = "/createUser")
    @TimeConsuming
    public ResponseEntity<ResponseResultVO> createUser(@Valid @RequestBody SysUsersVo sysUsersVo) {
    
    
        log.info("UserManager = start create user [{}] pwd [{}]", sysUsersVo.getUserName(), sysUsersVo.getUserPwd());

        return ResponseEntity.ok(usersService.createUser(sysUsersVo).orElse(ResponseResultVO.builder()
                .code(ErrorCodeConstant.SYSTEM_ERROR)
                .msg(ErrorMsgConstant.SYSTEM_ERROR)
                .build()));
    }

4.アスペクトを使用して関数を実装します

4.1 Aopを通じて、ビジネス機能を実現するためのポイントカット方法として注釈を使用する

ここに画像の説明を挿入

	@Pointcut("@annotation(com.pet.annotation.LogController)")
    public void annotationPoint() {
    
    
    }

    @Pointcut("@annotation(com.pet.annotation.TimeConsuming)")
    public void methodTimePoint() {
    
    
    }

4.2注釈のパラメータを取得します

たとえば、次のメソッドはロギング機能を実装します

    @Before(value = "annotationPoint() && @annotation(logController)", argNames = "joinPoint, logController")
    public void beforeController(JoinPoint joinPoint, LogController logController) {
    
    
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        HttpSession session = request.getSession();
        SysUsers user = (SysUsers) session.getAttribute(HttpConstant.SESSION_USER);
        String realMethodName = joinPoint.getSignature().getName();

        log.info("Aspect = [{}] ,user [{}] , method [{}] , logLevel [{}] , do [{}] , realMethod [{}]",
                new Date(), user == null ? "system" : user.getUserName(), logController.method(), logController.logLevel(), logController.description(), realMethodName);

        // 异步处理日志
        publisher.publishEvent(new LogToDbEvent(
                LogToDbEventEntity.builder()
                        .date(new Date())
                        .userName(user == null ? "system" : user.getUserName())
                        .method(logController.method())
                        .logLevel(logController.logLevel())
                        .description(logController.description())
                        .realMethod(realMethodName)
                        .build()));
    }

統計的手法時間のかかる手法

	@Around(value = "methodTimePoint()")
    public Object apiTimeConsuming(ProceedingJoinPoint pjp) throws Throwable {
    
    
        long begin = System.currentTimeMillis();
        String method = pjp.getSignature().getName();
        String className = pjp.getTarget().getClass().getName();

        Object ret = pjp.proceed();
        log.info("Aspect = [{}] ,class [{}] , method [{}] , time consuming[{}]", new Date(), className, method, System.currentTimeMillis() - begin);
        return ret;
    }

おすすめ

転載: blog.csdn.net/weixin_38045214/article/details/114967252