Java注解的继承

注解继承的说明

1、首先要想Annotation能被继承,需要在注解定义的时候加上@Inherited,并且如果要被反射应用的话,就需要还有个事@Retention(RetentionPolicy.RUNTIME)标识

2、JDK文档中说明的是:只有在类上应用Annotation才能被继承,而实际应用结果是:除了类上应用的Annotation能被继承外,没有被重写的方法的Annotation也能被继承;

3、当方法被重写后,Annotation不会被继承

4、Annotation的继承不能应用在接口上

Spring事务注解@Transactional

@Transactional 可以作用于接口、接口方法、类以及类方法上。但是 Spring 小组建议不要在接口或者接口方法上使用该注解,因为这只有在使用基于接口的代理时它才会生效。另外, @Transactional 注解应该只被应用到 public 方法上,这是由 Spring AOP 的本质决定的(从上面的Spring AOP 事务增强可以看出,就是针对方法的)。如果你在 protected、private 或者默认可见性的方法上使用 @Transactional 注解,这将被忽略,也不会抛出任何异常。

SpringMVC的@RequestMapping注解

 使用反射会发现并没有把@RequestMapping继承过来,但是子类能享有它所带来的的效果,但是如果父类为接口则无效,因为接口可以多实现,类是单继承

参考资料

  https://www.cnblogs.com/flying607/p/8295139.html

  https://stackoverflow.com/questions/4745798/why-java-classes-do-not-inherit-annotations-from-implemented-interfaces

部分原文链接:https://zxmsdyz.iteye.com/blog/1974249

原文地址:https://www.cnblogs.com/xhy-shine/p/11138183.html