日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > windows >内容正文

windows

面试官:实际工作中哪里用到了自定义注解?

發(fā)布時(shí)間:2024/1/16 windows 34 coder
生活随笔 收集整理的這篇文章主要介紹了 面试官:实际工作中哪里用到了自定义注解? 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

自定義注解可以標(biāo)記在方法上或類上,用于在編譯期或運(yùn)行期進(jìn)行特定的業(yè)務(wù)功能處理。在 Java 中,自定義注解使用 @interface 關(guān)鍵字來(lái)定義,它可以實(shí)現(xiàn)如:日志記錄、性能監(jiān)控、權(quán)限校驗(yàn)等功能。

在 Spring Boot 中實(shí)現(xiàn)一個(gè)自定義注解,可以通過(guò) AOP(面向切面編程)或攔截器(Interceptor)來(lái)實(shí)現(xiàn)。

1.實(shí)現(xiàn)自定義注解

下面我們先使用 AOP 的方式來(lái)實(shí)現(xiàn)一個(gè)打印日志的自定義注解,它的實(shí)現(xiàn)步驟如下:

  1. 添加 Spring AOP 依賴。
  2. 創(chuàng)建自定義注解。
  3. 編寫(xiě) AOP 攔截(自定義注解)的邏輯代碼。
  4. 使用自定義注解。

具體實(shí)現(xiàn)如下。

① 添加 Spring AOP 依賴

在 pom.xml 中添加如下依賴:

<dependencies>
  <!-- Spring AOP dependency -->
  <dependency>
    <groupIdorg.springframework.boot</groupId>
      <artifactIdspring-boot-starter-aop</artifactId>
      </dependency>
</dependencies>

② 創(chuàng)建自定義注解

創(chuàng)建一個(gè)新的 Java 注解類,通過(guò) @interface 關(guān)鍵字來(lái)定義,并可以添加元注解以及屬性。

import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomLogAnnotation {
    String value() default "";
    boolean enable() default true;
}

在上面的例子中,我們定義了一個(gè)名為 CustomLogAnnotation 的注解,它有兩個(gè)屬性:value 和 enable,分別設(shè)置了默認(rèn)值。

  • @Target(ElementType.METHOD) 指定了該注解只能應(yīng)用于方法級(jí)別。
  • @Retention(RetentionPolicy.RUNTIME) 表示這個(gè)注解在運(yùn)行時(shí)是可見(jiàn)的,這樣 AOP 代理才能在運(yùn)行時(shí)讀取到這個(gè)注解。

③ 編寫(xiě) AOP 攔截(自定義注解)的邏輯代碼

使用 Spring AOP 來(lái)攔截帶有自定義注解的方法,并在其前后執(zhí)行相應(yīng)的邏輯。

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class CustomLogAspect {
    @Around("@annotation(customLog)")
    public Object logAround(ProceedingJoinPoint joinPoint, CustomLogAnnotation customLog) throws Throwable {
        if (customLog.enable()) {
            // 方法執(zhí)行前的處理
            System.out.println("Before method execution: " + joinPoint.getSignature().getName());
            long start = System.currentTimeMillis();
            // 執(zhí)行目標(biāo)方法
            Object result = joinPoint.proceed();
            // 方法執(zhí)行后的處理
            long elapsedTime = System.currentTimeMillis() - start;
            System.out.println("After method execution (" + elapsedTime + 
                               "ms): " + customLog.value());
            return result;
        } else {
            return joinPoint.proceed();
        }
    }
}

④ 使用自定義注解

將自定義注解應(yīng)用于需要進(jìn)行日志記錄的方法上,如下代碼所示:

@RestController
public class MyController {
    @CustomLogAnnotation(value = "This is a test method", enable = true)
    @GetMapping("/test")
    public String testMethod() {
        // 業(yè)務(wù)邏輯代碼
        return "Hello from the annotated method!";
    }
}

2.實(shí)際工作中的自定義注解

實(shí)際工作中我們通常會(huì)使用自定義注解來(lái)實(shí)現(xiàn)如權(quán)限驗(yàn)證,或者是冪等性判斷等功能。

冪等性判斷是指在分布式系統(tǒng)或并發(fā)環(huán)境中,對(duì)于同一操作的多次重復(fù)請(qǐng)求,系統(tǒng)的響應(yīng)結(jié)果應(yīng)該是一致的。簡(jiǎn)而言之,無(wú)論接收到多少次相同的請(qǐng)求,系統(tǒng)的行為和結(jié)果都應(yīng)該是相同的。

3.如何實(shí)現(xiàn)自定義冪等性注解?

下面我們使用攔截器 + Redis 的方式來(lái)實(shí)現(xiàn)一下自定義冪等性注解,它的實(shí)現(xiàn)步驟如下:

  1. 創(chuàng)建自定義冪等性注解。
  2. 創(chuàng)建攔截器,實(shí)現(xiàn)冪等性邏輯判斷。
  3. 配置攔截規(guī)則。
  4. 使用自定義冪等性注解。

具體實(shí)現(xiàn)如下。

① 創(chuàng)建自定義冪等性注解

@Retention(RetentionPolicy.RUNTIME) // 程序運(yùn)行時(shí)有效
@Target(ElementType.METHOD) // 方法注解
public @interface Idempotent {
    /**
     * 請(qǐng)求標(biāo)識(shí)符的參數(shù)名稱,默認(rèn)為"requestId"
     */
    String requestId() default "requestId";
    /**
     * 冪等有效時(shí)長(zhǎng)(單位:秒)
     */
    int expireTime() default 60;
}

② 創(chuàng)建攔截器

@Component
public class IdempotentInterceptor extends HandlerInterceptorAdapter {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Method method = ((HandlerMethod) handler).getMethod();
        Idempotent idempotent = method.getAnnotation(Idempotent.class);
        if (idempotent != null) {
            // 獲取請(qǐng)求中的唯一標(biāo)識(shí)符
            String requestId = obtainRequestId(request, idempotent.requestId());
            // 判斷該請(qǐng)求是否已經(jīng)處理過(guò)
            if (redisTemplate.opsForValue().get(idempotentKey(requestId)) != null) {
                // 已經(jīng)處理過(guò),返回冪等響應(yīng)
                response.getWriter().write("重復(fù)請(qǐng)求");
                return false;
            } else {
                // 將請(qǐng)求標(biāo)識(shí)符存入Redis,并設(shè)置過(guò)期時(shí)間
                redisTemplate.opsForValue().set(idempotentKey(requestId), "processed", idempotent.expireTime(), TimeUnit.SECONDS);
                return true; // 繼續(xù)執(zhí)行業(yè)務(wù)邏輯
            }
        }
        return super.preHandle(request, response, handler);
    }

    private String idempotentKey(String requestId) {
        return "idempotent:" + requestId;
    }

    private String obtainRequestId(HttpServletRequest request, String paramName) {
        // 實(shí)現(xiàn)從請(qǐng)求中獲取唯一標(biāo)識(shí)符的方法
        return request.getParameter(paramName);
    }
}

③ 配置攔截器

在 Spring Boot 配置文件類中,添加攔截器配置:

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private IdempotentInterceptor idempotentInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(idempotentInterceptor)
        	.addPathPatterns("/**"); // 攔截所有接口
    }
}

④ 使用自定義注解

最后,在需要進(jìn)行冪等控制的 Controller 方法上使用 @Idempotent 注解:

Java
@RestController
public class TestController {
    @PostMapping("/order")
    @Idempotent(requestId = "orderId") // 假設(shè)orderId是從客戶端傳來(lái)的唯一標(biāo)識(shí)訂單請(qǐng)求的參數(shù)
    public String placeOrder(@RequestParam("orderId") String orderId, ...) {
        // 業(yè)務(wù)處理邏輯
    }
}

這樣,當(dāng)有相同的請(qǐng)求 ID 在指定的有效期內(nèi)再次發(fā)起請(qǐng)求時(shí),會(huì)被攔截器識(shí)別并阻止其重復(fù)執(zhí)行業(yè)務(wù)邏輯。

小結(jié)

自定義注解被廣泛應(yīng)用于日常開(kāi)發(fā)中,像日志記錄、性能監(jiān)控、權(quán)限判斷和冪等性判斷等功能的實(shí)現(xiàn),使用自定義注解來(lái)實(shí)現(xiàn)是非常方便的。在 Spring Boot 中,使用 @interface 關(guān)鍵字來(lái)定義自定義注解,之后再使用 AOP 或攔截器的方式實(shí)現(xiàn)自定義注解,之后就可以方便的使用自定義注解了。

課后思考

那么問(wèn)題來(lái)了,AOP 和攔截器的底層實(shí)現(xiàn)原理是啥呢?歡迎評(píng)論區(qū)留言互動(dòng)。點(diǎn)贊超過(guò) 20,更新下篇文章。

本文已收錄到我的面試小站 www.javacn.site,其中包含的內(nèi)容有:Redis、JVM、并發(fā)、并發(fā)、MySQL、Spring、Spring MVC、Spring Boot、Spring Cloud、MyBatis、設(shè)計(jì)模式、消息隊(duì)列等模塊。

總結(jié)

以上是生活随笔為你收集整理的面试官:实际工作中哪里用到了自定义注解?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。