日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

面試官:實際工作中哪里用到了自定義注解?

來源: 責編: 時間:2024-01-15 09:21:20 184觀看
導讀自定義注解可以標記在方法上或類上,用于在編譯期或運行期進行特定的業務功能處理。在 Java 中,自定義注解使用 @interface 關鍵字來定義,它可以實現如:日志記錄、性能監控、權限校驗等功能。在 Spring Boot 中實現一個自

g3T28資訊網——每日最新資訊28at.com

自定義注解可以標記在方法上或類上,用于在編譯期或運行期進行特定的業務功能處理。在 Java 中,自定義注解使用 @interface 關鍵字來定義,它可以實現如:日志記錄、性能監控、權限校驗等功能。g3T28資訊網——每日最新資訊28at.com

在 Spring Boot 中實現一個自定義注解,可以通過 AOP(面向切面編程)或攔截器(Interceptor)來實現。g3T28資訊網——每日最新資訊28at.com

1、實現自定義注解

下面我們先使用 AOP 的方式來實現一個打印日志的自定義注解,它的實現步驟如下:g3T28資訊網——每日最新資訊28at.com

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

具體實現如下。g3T28資訊網——每日最新資訊28at.com

(1)添加 Spring AOP 依賴

在 pom.xml 中添加如下依賴:g3T28資訊網——每日最新資訊28at.com

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

(2)創建自定義注解

創建一個新的 Java 注解類,通過 @interface 關鍵字來定義,并可以添加元注解以及屬性。g3T28資訊網——每日最新資訊28at.com

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

在上面的例子中,我們定義了一個名為 CustomLogAnnotation 的注解,它有兩個屬性:value 和 enable,分別設置了默認值。g3T28資訊網——每日最新資訊28at.com

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

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

使用 Spring AOP 來攔截帶有自定義注解的方法,并在其前后執行相應的邏輯。g3T28資訊網——每日最新資訊28at.com

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

(4)使用自定義注解

將自定義注解應用于需要進行日志記錄的方法上,如下代碼所示:g3T28資訊網——每日最新資訊28at.com

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

2、實際工作中的自定義注解

實際工作中我們通常會使用自定義注解來實現如權限驗證,或者是冪等性判斷等功能。g3T28資訊網——每日最新資訊28at.com

g3T28資訊網——每日最新資訊28at.com

冪等性判斷是指在分布式系統或并發環境中,對于同一操作的多次重復請求,系統的響應結果應該是一致的。簡而言之,無論接收到多少次相同的請求,系統的行為和結果都應該是相同的。g3T28資訊網——每日最新資訊28at.com

g3T28資訊網——每日最新資訊28at.com

3、如何實現自定義冪等性注解?

下面我們使用攔截器 + Redis 的方式來實現一下自定義冪等性注解,它的實現步驟如下:g3T28資訊網——每日最新資訊28at.com

  • 創建自定義冪等性注解。
  • 創建攔截器,實現冪等性邏輯判斷。
  • 配置攔截規則。
  • 使用自定義冪等性注解。

具體實現如下。g3T28資訊網——每日最新資訊28at.com

(1)創建自定義冪等性注解

@Retention(RetentionPolicy.RUNTIME) // 程序運行時有效@Target(ElementType.METHOD) // 方法注解public @interface Idempotent {    /**     * 請求標識符的參數名稱,默認為"requestId"     */    String requestId() default "requestId";    /**     * 冪等有效時長(單位:秒)     */    int expireTime() default 60;}

(2)創建攔截器

@Componentpublic 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) {            // 獲取請求中的唯一標識符            String requestId = obtainRequestId(request, idempotent.requestId());            // 判斷該請求是否已經處理過            if (redisTemplate.opsForValue().get(idempotentKey(requestId)) != null) {                // 已經處理過,返回冪等響應                response.getWriter().write("重復請求");                return false;            } else {                // 將請求標識符存入Redis,并設置過期時間                redisTemplate.opsForValue().set(idempotentKey(requestId), "processed", idempotent.expireTime(), TimeUnit.SECONDS);                return true; // 繼續執行業務邏輯            }        }        return super.preHandle(request, response, handler);    }    private String idempotentKey(String requestId) {        return "idempotent:" + requestId;    }    private String obtainRequestId(HttpServletRequest request, String paramName) {        // 實現從請求中獲取唯一標識符的方法        return request.getParameter(paramName);    }}

(3)配置攔截器

在 Spring Boot 配置文件類中,添加攔截器配置:g3T28資訊網——每日最新資訊28at.com

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

(4)使用自定義注解

最后,在需要進行冪等控制的 Controller 方法上使用 @Idempotent 注解:g3T28資訊網——每日最新資訊28at.com

Java@RestControllerpublic class TestController {    @PostMapping("/order")    @Idempotent(requestId = "orderId") // 假設orderId是從客戶端傳來的唯一標識訂單請求的參數    public String placeOrder(@RequestParam("orderId") String orderId, ...) {        // 業務處理邏輯    }}

這樣,當有相同的請求 ID 在指定的有效期內再次發起請求時,會被攔截器識別并阻止其重復執行業務邏輯。g3T28資訊網——每日最新資訊28at.com

小結

自定義注解被廣泛應用于日常開發中,像日志記錄、性能監控、權限判斷和冪等性判斷等功能的實現,使用自定義注解來實現是非常方便的。在 Spring Boot 中,使用 @interface 關鍵字來定義自定義注解,之后再使用 AOP 或攔截器的方式實現自定義注解,之后就可以方便的使用自定義注解了。g3T28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-60966-0.html面試官:實際工作中哪里用到了自定義注解?

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: Python數據分析中備受歡迎的庫和工具

下一篇: 背會了常見的幾個線程池用法,結果被問翻

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 福清市| 延边| 东宁县| 三门峡市| 辽宁省| 红河县| 滦南县| 博客| 耿马| 龙里县| 分宜县| 五峰| 双辽市| 镇雄县| 本溪市| 天峨县| 天气| 肇庆市| 韩城市| 永吉县| 东光县| 长沙市| 瓮安县| 清河县| 清水河县| 万年县| 玉林市| 芦山县| 武乡县| 广汉市| 通海县| 屏南县| 宣化县| 读书| 鄄城县| 呼和浩特市| 会理县| 永年县| 肥城市| 杨浦区| 马尔康县|