结论:
当一个被@Inherited注解修饰的注解作用于类上时,其子类也会被加上该注解。
源码
package java.lang.annotation;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}
该注解是一个源注解,是jdk中定义的。
各种情况
定义注解
HasInherited注解
package com.example.springtest.Inherited;
import java.lang.annotation.*;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface HasInherited {
}
NoInherited
package com.example.springtest.Inherited;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface NoInherited {
}
类继承关系(父类注解被@Inherited修饰)
package com.example.springtest.Inherited;
@HasInherited
public class FatherClass_HasInherited {
}
package com.example.springtest.Inherited;
public class ChildH extends FatherClass_HasInherited{
}
@SpringBootTest
class SpringTestApplicationTests {
@Test
public void fatherClass_HasInherited(){
Class<ChildH> childHClass = ChildH.class;
Annotation[] annotations = childHClass.getAnnotations();
System.out.println("------------------");
for (Annotation annotation : annotations) {
System.out.println("注解名:"+annotation);
}
System.out.println("------------------");
}
}
结果:子类成功获取父类中被@Inherited修饰的注解
类继承关系(父类注解没有@Inherited修饰)
package com.example.springtest.Inherited;
@NoInherited
public interface FatherInterface_NoInherited {
}
package com.example.springtest.Inherited;
public class ChildN extends FatherClass_NoInherited{
}
@SpringBootTest
class SpringTestApplicationTests {
@Test
public void fatherClass_NoInherited(){
Class<ChildN> childN = ChildN.class;
Annotation[] annotations = childN.getAnnotations();
System.out.println("------------------");
for (Annotation annotation : annotations) {
System.out.println("注解名:"+annotation);
}
System.out.println("------------------");
}
}
结果:子类成功获取不到父类中没有被@Inherited修饰的注解
类实现接口关系(接口注解有@Inherited修饰)
package com.example.springtest.Inherited;
@HasInherited
public interface FatherInterface_HasInherited {
}
package com.example.springtest.Inherited;
public class ChildHI implements FatherInterface_HasInherited{
}
@SpringBootTest
class SpringTestApplicationTests {
@Test
public void fatherInterface_HasInherited(){
Class<ChildHI> childHI = ChildHI.class;
Annotation[] annotations = childHI.getAnnotations();
System.out.println("------------------");
for (Annotation annotation : annotations) {
System.out.println("注解名:"+annotation);
}
System.out.println("------------------");
}
}
结果:子类获取不到父接口中被@Inherited修饰的注解
接口继承接口关系(接口注解有@Inherited修饰)
package com.example.springtest.Inherited;
@HasInherited
public interface FatherInterface_HasInherited {
}
package com.example.springtest.Inherited;
public interface ChildInterface extends FatherInterface_HasInherited{
}
@SpringBootTest
class SpringTestApplicationTests {
@Test
public void childInterface(){
Class<ChildInterface> childInterface = ChildInterface.class;
Annotation[] annotations = childInterface.getAnnotations();
System.out.println("------------------");
for (Annotation annotation : annotations) {
System.out.println("注解名:"+annotation);
}
System.out.println("------------------");
}
}
结果:子接口获取不到父接口中被@Inherited修饰的注解
总结
@Inherited修饰的注解只对实现类的继承有效,对接口的实现是无效的。