基于@AliasFor注解的用法及說明
一、前言
@AliasFor注解基本上都是在spring源碼當(dāng)中出現(xiàn)的,AliasFor是Spring提供的注解,Alias是別名的意思,For是為了,首先我們通過命名可以得出一個(gè)結(jié)論,他是為了別名而自定義的注解!
Spring中@AliasFor注解的作用有兩點(diǎn):
- 將同一個(gè)注解類的屬性設(shè)置互為別名
- 將一個(gè)注解上的屬性值傳遞給另一個(gè)注解
但這并不是java原生支持的,需要通過Spring中提供的工具類:org.springframework.core.annotation.AnnotationUtils或者org.springframework.core.annotation.AnnotatedElementUtils來解析。AnnotatedElementUtils內(nèi)部還是調(diào)用的AnnotationUtils。
源碼如下:它有三個(gè)屬性value和attribute以及annotation,@AliasFor注解注釋了自身,并且value和attribute互為別名,通過源碼很容易知道,當(dāng)我們使用這個(gè)注解,
@AliasFor(value=“xxx”)和@AliasFor(attribute=“xxx”)是等價(jià)的。
@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 運(yùn)行時(shí)讀取Annotation 需要通過反射,Spring 提供AnnotationUtils , AnnotationElementUtils 用于簡(jiǎn)化操作,其他特點(diǎn)如下:
- 查詢Meta Annotation(注解的注解)
- 對(duì)@AliasFor 解析,生成指定注解的代理,并賦值。(注:定義其他Annotation 的別名)
Utils 調(diào)用涉及的元素:
- Annotation: Annotation 元數(shù)據(jù)
- AnnotatedElement: 被Annotation注解的對(duì)象,包括annotation , class , method等
- Method: 類和接口中的方法信息
AnnotationUtils常用方法:
getAnnotation: 從某個(gè)類獲取某個(gè)annotationfindAnnotation: 從類或方法中查找某個(gè)annotation。isAnnotationDeclaredLocally: 驗(yàn)證annotation是否直接注釋在類上而不是集成來的。isAnnotationInherited: 驗(yàn)證annotation是否繼承于另一個(gè)class。getAnnotationAttributes: 獲取annotation的所有屬性。getValue: 獲取指定annotation的值.getDefaultValue: 獲取指定annotation或annotation 屬性的默認(rèn)值
AnnotatedElementUtils常用方法:
findMergedAnnotation:這個(gè)方法會(huì)合并@AliasFor傳遞的值
三、同一個(gè)注解類的屬性設(shè)置互為別名
1.自定義一個(gè)EnableCVS 注解
一共有兩個(gè)屬性
import org.springframework.core.annotation.AliasFor;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EnableCVS {
// 這里就算是改成@AliasFor(attribute = "address")測(cè)試結(jié)果也是一樣的
@AliasFor(value = "address")
String value() default "";
@AliasFor(value = "value")
String address() default "";
}
2.創(chuàng)建一個(gè)類
然后使用自定義的注解@EnableCVS修飾
@Configuration
@EnableCVS(address = "hhh")
public class AppConfig {
}
3.測(cè)試:通過兩種方式獲取自定義注解當(dāng)中的屬性值
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.輸出結(jié)果如下
首先我們?cè)O(shè)置的注解是@EnableCVS(address = "hhh") ,只設(shè)置了address屬性,并沒有設(shè)置value屬性,會(huì)發(fā)現(xiàn)jdk原生方式獲取value的時(shí)候是拿不到值的,而spring提供的AnnotationUtils卻可以獲取到,而且獲取到的就是address的值!

其實(shí)就可以理解為,一旦value值設(shè)置了如下注解@AliasFor(value = "address"),也就意味著通過AnnotationUtils來獲取value屬性值的時(shí)候,當(dāng)value值沒有設(shè)置的時(shí)候,實(shí)際上會(huì)去獲取address屬性的值!
@AliasFor(value = "address") String value() default "";
注意:如果@AliasFor注解當(dāng)中兩個(gè)屬性互相設(shè)置了@AliasFor別名,并且使用自定義注解的時(shí)候,同時(shí)設(shè)置address和value的值,這時(shí)候通過AnnotationUtils#findAnnotation(Class<?>, annotationType)獲取屬性值,則會(huì)拋出異常!
示例如下:
@Configuration
@EnableCVS(value = "hhh",address = "222")
public class AppConfig {
}
四、將一個(gè)注解上的屬性值傳遞給另一個(gè)注解
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.聲明一個(gè)類
使用@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;
// 這個(gè)注解一定要加,不然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的方法不會(huì)組合@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 所以存在實(shí)例,@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.輸出結(jié)果
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注解應(yīng)用場(chǎng)景
5.1. @SpringBootApplication源碼
如下所示@SpringBootApplication并沒有定義新的屬性,而是復(fù)用其他注解已有的注解屬性,并對(duì)其進(jìn)行組合形成新的注解從而到達(dá)到便捷的目的。
這樣的注解我們可以稱之為復(fù)合注解。
@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 時(shí)我們只需要@SpringBootApplication一個(gè)注解就能開啟自動(dòng)配置,自動(dòng)掃描的功能。
而不再需要使下面三個(gè)注解來達(dá)到同樣的目的。
@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 {};
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Java中的checked異常和unchecked異常區(qū)別
這篇文章主要介紹了詳解Java中的checked異常和unchecked異常區(qū)別,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02
response.sendRedirect()實(shí)現(xiàn)重定向(頁(yè)面跳轉(zhuǎn))
在Java web開發(fā)中,使用response.sendRedirect()可實(shí)現(xiàn)重定向功能。本文將介紹如何使用該方法進(jìn)行頁(yè)面跳轉(zhuǎn),以及該方法的使用場(chǎng)景和注意事項(xiàng),感興趣的可以了解一下2023-04-04
idea?springBoot項(xiàng)目自動(dòng)注入mapper為空?qǐng)?bào)錯(cuò)的解決方法
這篇文章主要介紹了idea?springBoot項(xiàng)目自動(dòng)注入mapper為空?qǐng)?bào)錯(cuò)的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
java-jsp springmvc-controller 傳值到頁(yè)面的方法
下面小編就為大家分享一篇java-jsp springmvc-controller 傳值到頁(yè)面的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03
教你使用java將excel數(shù)據(jù)導(dǎo)入MySQL
今天教大家如何使用Java將excel數(shù)據(jù)導(dǎo)入MySQL,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴呢很有幫助,需要的朋友可以參考下2021-05-05
Mybatis-Plus進(jìn)階分頁(yè)與樂觀鎖插件及通用枚舉和多數(shù)據(jù)源詳解
這篇文章主要介紹了Mybatis-Plus的分頁(yè)插件與樂觀鎖插件還有通用枚舉和多數(shù)據(jù)源的相關(guān)介紹,文中代碼附有詳細(xì)的注釋,感興趣的朋友來看看吧2022-03-03
Java中while語(yǔ)句的簡(jiǎn)單知識(shí)及應(yīng)用
這篇文章主要給大家介紹了關(guān)于Java中while語(yǔ)句的簡(jiǎn)單知識(shí)及應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

