最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于@AliasFor注解的用法及說明

 更新時間:2023年02月27日 14:56:53   作者:怪 咖@  
這篇文章主要介紹了基于@AliasFor注解的用法及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

一、前言

@AliasFor注解基本上都是在spring源碼當中出現(xiàn)的,AliasFor是Spring提供的注解,Alias是別名的意思,For是為了,首先我們通過命名可以得出一個結論,他是為了別名而自定義的注解!

Spring中@AliasFor注解的作用有兩點:

  • 將同一個注解類的屬性設置互為別名
  • 將一個注解上的屬性值傳遞給另一個注解

但這并不是java原生支持的,需要通過Spring中提供的工具類:org.springframework.core.annotation.AnnotationUtils或者org.springframework.core.annotation.AnnotatedElementUtils來解析。AnnotatedElementUtils內(nèi)部還是調(diào)用的AnnotationUtils。

源碼如下:它有三個屬性value和attribute以及annotation,@AliasFor注解注釋了自身,并且value和attribute互為別名,通過源碼很容易知道,當我們使用這個注解,@AliasFor(value=“xxx”)@AliasFor(attribute=“xxx”)是等價的。

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface AliasFor {
    @AliasFor("attribute")
    String value() default "";

    @AliasFor("value")
    String attribute() default "";

    Class<? extends Annotation> annotation() default Annotation.class;
}

二、AnnotationUtils 和 AnnotatedElementUtils

Java 運行時讀取Annotation 需要通過反射,Spring 提供AnnotationUtils , AnnotationElementUtils 用于簡化操作,其他特點如下:

  • 查詢Meta Annotation(注解的注解)
  • 對@AliasFor 解析,生成指定注解的代理,并賦值。(注:定義其他Annotation 的別名)

Utils 調(diào)用涉及的元素:

  • Annotation: Annotation 元數(shù)據(jù)
  • AnnotatedElement: 被Annotation注解的對象,包括annotation , class , method等
  • Method: 類和接口中的方法信息

AnnotationUtils常用方法:

  • getAnnotation: 從某個類獲取某個annotation
  • findAnnotation: 從類或方法中查找某個annotation。
  • isAnnotationDeclaredLocally: 驗證annotation是否直接注釋在類上而不是集成來的。
  • isAnnotationInherited: 驗證annotation是否繼承于另一個class。
  • getAnnotationAttributes: 獲取annotation的所有屬性。
  • getValue: 獲取指定annotation的值.
  • getDefaultValue: 獲取指定annotation或annotation 屬性的默認值

AnnotatedElementUtils常用方法:

findMergedAnnotation:這個方法會合并@AliasFor傳遞的值

三、同一個注解類的屬性設置互為別名

1.自定義一個EnableCVS 注解

一共有兩個屬性

import org.springframework.core.annotation.AliasFor;

import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EnableCVS {
	
	// 這里就算是改成@AliasFor(attribute = "address")測試結果也是一樣的
    @AliasFor(value = "address")
    String value() default "";

    @AliasFor(value = "value")
    String address() default "";
}

2.創(chuàng)建一個類

然后使用自定義的注解@EnableCVS修飾

@Configuration
@EnableCVS(address = "hhh")
public class AppConfig {

}

3.測試:通過兩種方式獲取自定義注解當中的屬性值

import org.springframework.core.annotation.AnnotationUtils;

public class Test {
    public static void main(String[] args) {
    	// spring提供的
        EnableCVS annotation = AnnotationUtils.findAnnotation(AppConfig.class, EnableCVS.class);
        System.out.println("AnnotationUtils:address:" + annotation.address());
        System.out.println("AnnotationUtils:value:" + annotation.value());
		
		// jdk原生
        EnableCVS annotation1 = AppConfig.class.getAnnotation(EnableCVS.class);
        System.out.println("AppConfig:address:" + annotation1.address());
        System.out.println("AppConfig:value:" + annotation1.value());
    }
}

4.輸出結果如下

首先我們設置的注解是@EnableCVS(address = "hhh") ,只設置了address屬性,并沒有設置value屬性,會發(fā)現(xiàn)jdk原生方式獲取value的時候是拿不到值的,而spring提供的AnnotationUtils卻可以獲取到,而且獲取到的就是address的值!

其實就可以理解為,一旦value值設置了如下注解@AliasFor(value = "address"),也就意味著通過AnnotationUtils來獲取value屬性值的時候,當value值沒有設置的時候,實際上會去獲取address屬性的值!

@AliasFor(value = "address")
String value() default "";

注意:如果@AliasFor注解當中兩個屬性互相設置了@AliasFor別名,并且使用自定義注解的時候,同時設置address和value的值,這時候通過AnnotationUtils#findAnnotation(Class<?>, annotationType)獲取屬性值,則會拋出異常!

示例如下:

@Configuration
@EnableCVS(value = "hhh",address = "222")
public class AppConfig {

}

四、將一個注解上的屬性值傳遞給另一個注解

1.自定義注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Scope("singleton")
@Component
@Inherited
public @interface SingletonComponent {

    @AliasFor(annotation = Component.class, attribute = "value")
    String value() default "";
}

2.聲明一個類

使用@SingletonComponent修飾

@SingletonComponent("simpleService")
public class SimpleSingletonService {

}

3.通過AnnotationUtils和AnnotatedElementUtils獲取注解

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Scope;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

import java.util.Map;

// 這個注解一定要加,不然getAllAnnocations方法獲取不到值
@ComponentScan
public class AnnotationUtilsDemo {
    private static void annotationUtilsDemo() {

        // 獲取類注解
        SingletonComponent singletonComponentAnnocation = AnnotationUtils.
                findAnnotation(SimpleSingletonService.class, SingletonComponent.class);

        System.out.println("@SingletonComponent : " + singletonComponentAnnocation);
        System.out.println("@SingletonComponent value: " + AnnotationUtils.getValue(singletonComponentAnnocation, "value"));


        System.out.println("----------------------------------------------");

        Scope scopeAnnocation = AnnotationUtils.findAnnotation(SimpleSingletonService.class, Scope.class);
        System.out.println("@Scope : " + scopeAnnocation);
        System.out.println("@Scope value: " + AnnotationUtils.getValue(scopeAnnocation, "scopeName"));

        System.out.println("----------------------------------------------");

        // 獲取@AliasFor Marge 后的注解,直接調(diào)用 AnnotationUtils的方法不會組合@AliasFor的值,需要調(diào)用AnnotatedElementUtils
        Component componentAnnocation = AnnotatedElementUtils.findMergedAnnotation(SimpleSingletonService.class, Component.class);
        System.out.println("@Component : " + componentAnnocation);
        System.out.println("@Component value: " + AnnotationUtils.getValue(componentAnnocation, "value"));
    }

    private static void getAllAnnocations() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AnnotationUtilsDemo.class);
        // 獲取SingletonComponent注解修飾的類
        Map<String, Object> beans = context.getBeansWithAnnotation(SingletonComponent.class);
        for (Object bean : beans.values()) {
            System.out.println("bean : " + bean);
            // @SingletonComponent 繼承了 @Component 所以存在實例,@Component的value值就是通過@AliasFor注解傳遞過去的
            Component componentAnnocation = AnnotatedElementUtils.findMergedAnnotation(bean.getClass(), Component.class);
            System.out.println(componentAnnocation);
        }
    }

    public static void main(String[] args) {
        AnnotationUtilsDemo.annotationUtilsDemo();

        System.out.println("----------------------------------------------");

        AnnotationUtilsDemo.getAllAnnocations();
    }
}

4.輸出結果

Connected to the target VM, address: '127.0.0.1:49763', transport: 'socket'
@SingletonComponent : @com.gzl.cn.springbootnacos.aa.SingletonComponent(value="simpleService")
@SingletonComponent value: simpleService
----------------------------------------------
@Scope : @org.springframework.context.annotation.Scope(proxyMode=DEFAULT, scopeName="singleton", value="singleton")
@Scope value: singleton
----------------------------------------------
@Component : @org.springframework.stereotype.Component(value="simpleService")
@Component value: simpleService
----------------------------------------------
bean : com.gzl.cn.springbootnacos.aa.SimpleSingletonService@1b759d6
@org.springframework.stereotype.Component(value="simpleService")

五、@AliasFor注解應用場景

5.1. @SpringBootApplication源碼

如下所示@SpringBootApplication并沒有定義新的屬性,而是復用其他注解已有的注解屬性,并對其進行組合形成新的注解從而到達到便捷的目的。

這樣的注解我們可以稱之為復合注解。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
    @AliasFor(annotation = EnableAutoConfiguration.class)
    Class<?>[] exclude() default {};

    @AliasFor(annotation = EnableAutoConfiguration.class)
    String[] excludeName() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackages"
    )
    String[] scanBasePackages() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackageClasses"
    )
    Class<?>[] scanBasePackageClasses() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "nameGenerator"
    )
    Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;

    @AliasFor(annotation = Configuration.class)
    boolean proxyBeanMethods() default true;
}

所以在使用SpringBoot 時我們只需要@SpringBootApplication一個注解就能開啟自動配置,自動掃描的功能。

而不再需要使下面三個注解來達到同樣的目的。

@Configuration
@ComponentSan
@EnnableAutoConfiguration

5.2. @RequestMapping源碼

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {
    String name() default "";

    @AliasFor("path")
    String[] value() default {};

    @AliasFor("value")
    String[] path() default {};

    RequestMethod[] method() default {};

    String[] params() default {};

    String[] headers() default {};

    String[] consumes() default {};

    String[] produces() default {};
}

總結

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 詳解記錄Java Log的幾種方式

    詳解記錄Java Log的幾種方式

    很多小伙伴不知道如何記錄日志,今天特地整理了本篇文章,文中有非常詳細的介紹及代碼示例,對小伙伴們很有幫助,需要的朋友可以參考下
    2021-06-06
  • java8時間 yyyyMMddHHmmss格式轉為日期的代碼

    java8時間 yyyyMMddHHmmss格式轉為日期的代碼

    這篇文章主要介紹了java8時間 yyyyMMddHHmmss格式轉為日期的代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Spring整合ehCache全過程

    Spring整合ehCache全過程

    這篇文章主要介紹了Spring整合ehCache全過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • MybatisPlus操作符和運算值詳解

    MybatisPlus操作符和運算值詳解

    在前端到后端的數(shù)據(jù)傳遞中,處理動態(tài)運算條件是一個常見的需求,本文介紹了如何在MybatisPlus中處理運算符和運算值的動態(tài)拼接問題,感興趣的朋友一起看看吧
    2024-10-10
  • Javamail使用過程中常見問題解決方案

    Javamail使用過程中常見問題解決方案

    這篇文章主要介紹了Javamail使用過程中常見問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-08-08
  • Java多線程編程中的并發(fā)安全問題及解決方法

    Java多線程編程中的并發(fā)安全問題及解決方法

    保障多線程并發(fā)安全,解決線程同步與鎖競爭問題,提高應用性能與可靠性。多線程編程需要考慮線程安全性,使用同步機制保證共享變量的一致性,避免線程競爭導致的數(shù)據(jù)不一致與死鎖等問題。常用的同步機制包括synchronized、ReentrantLock、volatile等
    2023-04-04
  • Java遍歷字符串和統(tǒng)計字符個數(shù)的操作方法

    Java遍歷字符串和統(tǒng)計字符個數(shù)的操作方法

    這篇文章主要介紹了Java遍歷字符串和統(tǒng)計字符個數(shù)的操作方法,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-12-12
  • Java實現(xiàn)鼠標拖放功能的方法

    Java實現(xiàn)鼠標拖放功能的方法

    這篇文章主要介紹了Java實現(xiàn)鼠標拖放功能的方法,很實用的功能,需要的朋友可以參考下
    2014-07-07
  • 區(qū)塊鏈常用數(shù)據(jù)庫leveldb用java來實現(xiàn)常規(guī)操作的方法

    區(qū)塊鏈常用數(shù)據(jù)庫leveldb用java來實現(xiàn)常規(guī)操作的方法

    這篇文章主要介紹了區(qū)塊鏈常用數(shù)據(jù)庫leveldb用java來實現(xiàn)常規(guī)操作,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • Java8通過Function獲取字段名的方法(獲取實體類的字段名稱)

    Java8通過Function獲取字段名的方法(獲取實體類的字段名稱)

    Java8通過Function獲取字段名。不用再硬編碼,效果類似于mybatis-plus的LambdaQueryWrapper,對Java8通過Function獲取字段名相關知識感興趣的朋友一起看看吧
    2021-09-09

最新評論

石家庄市| 梁河县| 南开区| 清镇市| 祁连县| 崇仁县| 西丰县| 水富县| 益阳市| 图木舒克市| 馆陶县| 雅安市| 合肥市| 湖北省| 嫩江县| 霍州市| 双城市| 子长县| 绥宁县| 喀喇沁旗| 永寿县| 曲阜市| 荥阳市| 东山县| 贡觉县| 济南市| 普兰县| 镇雄县| 澄迈县| 阜城县| 贵德县| 南木林县| 张掖市| 台湾省| 凤台县| 阳高县| 库车县| 昌吉市| 鄄城县| 铜梁县| 青冈县|