参考文献

@AliasFor注解的使用方式

在同一个注解中显示使用,将注解中的多个属性互相设置别名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// org.springframework.core.annotation.AliasFor
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping
public @interface RequestMapping {

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

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

//...
}
  • @AliasFor标签有一些使用限制
    • 互为别名的属性属性值类型,默认值,都是相同的;
    • 互为别名的注解必须成对出现,比如 value 属性添加了@AliasFor("path"),那么 path 属性就必须添加@AliasFor("value")
    • 另外还有一点,互为别名的属性必须定义默认值。

给元注解中的属性设定别名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@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 {};
//...略
}
  • 这种使用方式的好处是可以将几个注解的功能组合成一个新的注解。