从抽象类inheritance注释?

我可以以某种方式在抽象类上对一组注释进行分组,并且扩展此类的每个类都自动分配了这些注释吗?

至少以下不起作用:

@Service @Scope(value = BeanDefinition.SCOPE_PROTOTYPE) class AbstractService class PersonService extends AbstractService { @Autowired //will not work due to missing qualifier annotation private PersonDao dao; } 

答案是不

除非注释类型具有@Inherited元注释,否则不会inheritanceJava注释: https : //docs.oracle.com/javase/7/docs/api/java/lang/annotation/Inherited.html 。

Spring的@Component注释上没有@Inherited ,因此您需要将注释放在每个组件类上。 @ Service,@ Controller和@Repository都没有。

简短的回答是:使用您在示例中提到的注释, 没有

答案很长:有一个名为java.lang.annotation.Inherited的元注释。 如果注释本身使用此注释进行注释,那么当使用它对类进行注释时,其子类也会通过暗示自动注释。

但是,正如您在spring源代码中看到的那样, @Service@Scope注释本身并未使用@Inherited注释,因此类的@Service@Scope的存在不会被其子类inheritance。

也许这是可以在Spring中修复的东西。

我在我的项目中有这段代码,它工作得很好,虽然它没有注释为服务:

 public abstract class AbstractDataAccessService { @Autowired protected GenericDao genericDao; } 

 @Component public class ActorService extends AbstractDataAccessService { // you can use genericDao here } 

因此,您不需要在抽象类上添加注释,但即使您这样做,仍然必须在所有子类上添加注释,因为@Component@Service注释不会被inheritance。