Springboot常用注解之@Inherited和@interface
2019-03-13 23:55
274 查看
1. @Inherited:允许子类继承父类的注解,仅限于类注解有用,对于方法和属性无效。
测试元注解
1.1 包含@Inherited注解
[code]@Documented @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface InheritedTrue { String value(); }
1.2 不包含@Inherited注解
[code]@Documented @Retention(RetentionPolicy.RUNTIME) public @interface InheritedFalse { String value(); }
1.3 测试父类
[code]@InheritedTrue("InheritedTrue class") @InheritedFalse("InheritedFalse class") public class InheritedParent { @InheritedTrue("InheritedTrue method") @InheritedFalse("InheritedFalse method") public void methodTest(){ } @InheritedTrue("InheritedTrue no override method") @InheritedFalse("InheritedFalse no override method") public void methodInherttTest(){ } @InheritedTrue("InheritedTrue field") @InheritedFalse("InheritedFalse field") public String stringTest; }
1.4 测试子类
[code]public class InheritedChild extends InheritedParent{ @Override public void methodTest(){} }
1.5 单元测试父类
[code]@RunWith(SpringRunner.class) @SpringBootTest public class WalletTests { private Logger logger = LoggerFactory.getLogger(WalletTests.class); @Before public void init() { logger.info("开始测试-----------------"); } @After public void after() { logger.info("测试结束-----------------"); } }
1.6 单元测试
[code]public class InheritedTest extends WalletTests { private Logger logger = LoggerFactory.getLogger(InheritedTest.class); @Test public void InheritTest() throws NoSuchMethodException, NoSuchFieldException { Class<InheritedChild> child = InheritedChild.class; logger.info("对类进行测试"); if(child.isAnnotationPresent(InheritedTrue.class)){ logger.info(child.getAnnotation(InheritedTrue.class).value()); } if(child.isAnnotationPresent(InheritedFalse.class)){ logger.info(child.getAnnotation(InheritedFalse.class).value()); } logger.info("对重写方法进行测试"); Method method = child.getMethod("methodTest", null); if(method.isAnnotationPresent(InheritedTrue.class)){ logger.info(method.getAnnotation(InheritedTrue.class).value()); } if(method.isAnnotationPresent(InheritedFalse.class)){ logger.info(method.getAnnotation(InheritedFalse.class).value()); } logger.info("对继承方法进行测试"); Method methodInherit = child.getMethod("methodInherttTest", null); if(methodInherit.isAnnotationPresent(InheritedTrue.class)){ logger.info(methodInherit.getAnnotation(InheritedTrue.class).value()); } if(methodInherit.isAnnotationPresent(InheritedFalse.class)){ logger.info(methodInherit.getAnnotation(InheritedFalse.class).value()); } logger.info("对属性进行测试"); Field field = child.getField("stringTest"); if(field.isAnnotationPresent(InheritedTrue.class)){ logger.info(field.getAnnotation(InheritedTrue.class).value()); } if(field.isAnnotationPresent(InheritedFalse.class)){ logger.info(field.getAnnotation(InheritedFalse.class).value()); } } }
1.7 测试结果
可以看出只对类的继承有用,对于方法的重写和继承以及属性无效。
2.@Interface
自定义注释类型。java用 @interface Annotation{ } 定义一个注解 @Annotation,一个注解是一个类。
[code]@Documented @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface InterfaceTest { String value() default "InterfaceTest"; }
相关文章推荐
- spring boot 的常用注解使用小结
- Springboot 常用注解
- spring boot 常用注解
- Spring Boot常用注解(二) - 注入Bean的注解
- spring boot 常用注解解析
- Spring Boot系列(三) Spring Boot 常用注解
- 整理的springboot的常用注解
- Springboot 常用注解
- SpringBoot常用注解@Configuration
- SpringBoot 中常用注解@PathVaribale/@RequestParam/@GetMapping介绍
- spring boot之spring mvc常用注解(2)
- Springboot 常用注解
- springboot常用注解集锦
- Spring Boot常用注解总结
- Spring boot 学习笔记(05)——各种常用注解
- Spring Boot---(5)SpringBoot常用注解
- Springboot 常用注解
- spring boot 常用注解
- SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍(转)
- Spring Boot 集成 resteasy篇 — jax-rs常用注解介绍