您好,欢迎来到爱够旅游网。
搜索
您的当前位置:首页Annotations——@AliasFor注解和@Inherite注解

Annotations——@AliasFor注解和@Inherite注解

来源:爱够旅游网

@AliasFor注解的作用

@AliasFor是一个Spring提供的注解,顾名思义,alias单词是别名的意思,可以看出这个注解是起别名用的。具体的例子,我们在其使用方式中一一列举。

几种@AliasFor的使用场景

注解中的属性互相别名

我们摘一段RequestMapping注解中的代码来举例此种情况的使用,为了节省篇幅,省掉了与本例无关的代码。

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

我们用AnnotationUtils来获取RequestMapping注解里具体字段的值

@RequestMapping()
	public void index(){
		Class clazz = DemoApplication.class;
		try {
			clazz.getMethod("index");
			RequestMapping res = AnnotationUtils.findAnnotation(
					DemoApplication.class.getMethod("index"), RequestMapping.class);
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
    }

如果@RequestMapping()不包含任何属性值直接使用,我们会看到path和value属性默认都是一个空数组,这时对任何url的访问都会映射到这个requestMap注解的方法上。

对元注解的值进行重写

我们举个例子,例如我们有一个AnnotaionBase注解和一个AnnotationChild注解,AnnotaionBase是AnnotationChild的元注解,如果我想在使用AnnotationChild的同时,覆盖元注解AnnotaionBase中,某个相同类型的属性,代码见下:

@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotaionBase {
    String value() default "base";
}

@Retention(RetentionPolicy.RUNTIME)
@AnnotaionBase
@Inherited
public @interface AnnotationChild {
	@AliasFor(annotation = AnnotaionBase.class, attribute = "value")
    String extendValue() default "child";
}
@AnnotationChild(extendValue = "extendValue")
public class AliasForDemo {
    public static void main(String[] args) {
        AnnotaionBase base = AnnotationUtils.findAnnotation(AliasForDemo.class, AnnotaionBase.class);
        AnnotationChild child = AnnotationUtils.findAnnotation(AliasForDemo.class, AnnotationChild.class);
        AnnotaionBase base2 = AnnotatedElementUtils.findMergedAnnotation(AliasForDemo.class, AnnotaionBase.class);
    }
}

运行结果如下,我们可以看出我们通过给AnnotationChild注解的extendValue属性赋值,并且传递到了元注解AnnotaionBase的value属性上。这种用法有一点需要注意,就是@AliasFor的annotation属性指定的注解,一定要是元注解才行,否则就会报错。

	@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 {};

@Inherite注解的作用

Inherite意味遗传的、通过继承得到的,从其名字我们知道这个注解跟继承有关。注意,@Inherite注解存在于java.lang.annotation包中,因此这是一个jdk提供的注解。

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}

我们通过@Target元注解的属性值可以看出,这个@Inherited 是专门修饰注解的,其作用为:注释一个其他注解A,被注解A修饰的超类,其子类也能获得这个注解A。这里所说的仅仅是类之间的继承关系,接口的继承、接口的实现都不会得到父级的注解。
下面我们分别定义两个注解@AnnotationWithInherite和@AnnotationWithoutInherite来测试@Inherite的用法:

@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationWithInherite {
    String with() default "with";
}
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationWithoutInherite {
    String without() default "without";
}

@Inherite注解对接口是无效的

@AnnotationWithInherite
@AnnotationWithoutInherite
public interface BaseInterface {
}
public interface ChildInterface extends BaseInterface {
}
public void test {
	Annotation[] annotations = ChildInterface.class.getAnnotations();
}

运行结果annotations是一个空数组,证明@Inherite注解对接口是无效的。

@Inherite注解对接口的实现类是无效的

@AnnotationWithInherite
@AnnotationWithoutInherite
public interface BaseInterface {
}
public class implClass implements BaseInterface {
}
public void test {
	Annotation[] annotations = implClass.class.getAnnotations();
}

运行结果annotations是一个空数组,证明@Inherite注解对接口的实现类是无效的。

@Inherite注解对类继承是有效的

@AnnotationWithInherite
@AnnotationWithoutInherite
class BaseClass {
}
class ChildClass extends BaseClass{
}
public void test {
	Annotation[] annotations = ChildClass.class.getAnnotations();
}

运行结果annotations中有@AnnotationWithInherite这个注解,说明被@Inherite作用的注解,是能通过类继承关系由父类传递到子类的。

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igbc.cn 版权所有 湘ICP备2023023988号-5

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务