@AliasFor注解
在Spring的众多注解中,经常会发现很多注解的不同属性起着相同的作用,比如@RequestMapping的value属性和path属性,这就需要做一些基本的限制,比如value和path的值不能冲突,比如任意设置value或者设置path属性的值,都能够通过另一个属性来获取值等等。为了统一处理这些情况,Spring创建了@AliasFor标签。即别名注解
方式一:在同一注解内使用
1 2 3 4 5 6 7 8 9 10 11 12
| @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented@Mappingpublic @interface RequestMapping {
@AliasFor("path") String[] value() default {};
@AliasFor("value") String[] path() default {};
}
|
注意:互为别名的属性值类型、默认值都需要相同。必须成对配置,必须设置默认值。
方式二:显式覆盖元注解属性
1 2 3 4 5 6 7
| @Retention(RetentionPolicy.RUNTIME) @ContextConfigurationpublic @interface STC {
@AliasFor(value = "classes", annotation = ContextConfiguration.class) Class<?>[] cs() default {};
}
|
方式三:隐士声明别名
1 2 3 4 5 6 7 8 9 10 11 12
| @ContextConfiguration public @interface MyTestConfig {
@AliasFor(annotation = ContextConfiguration.class, attribute = "locations") String[] value() default {};
@AliasFor(annotation = ContextConfiguration.class, attribute = "locations") String[] groovyScripts() default {};
@AliasFor(annotation = ContextConfiguration.class, attribute = "locations") String[] xmlFiles() default {}; }
|
相当于value、groovyScripts和xmlFiles也互为别名
方式四:别名传递
1 2 3 4 5 6 7 8 9
| @MyTestConfig public @interface GroovyOrXmlTestConfig {
@AliasFor(annotation = MyTestConfig.class, attribute = "groovyScripts") String[] groovy() default {};
@AliasFor(annotation = ContextConfiguration.class, attribute = "locations") String[] xml() default {}; }
|
- GroovyOrXmlTestConfig把 @MyTestConfig(参考上一个案例)作为元注解;
- 定义了groovy属性,并作为MyTestConfig中的groovyScripts属性的别名;
- 定义了xml属性,并作为ContextConfiguration中的locations属性的别名;
- 因为MyTestConfig中的groovyScripts属性本身就是ContextConfiguration中的locations属性的别名;所以xml属性和groovy属性也互为别名;
参考资料:
[1]https://www.jianshu.com/p/869ed7037833
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者、原文超链接,感谢各位看官!!!
本文出自:monkeyGeek
座右铭:生于忧患,死于安乐
欢迎志同道合的朋友一起交流、探讨!
monkeyGeek