从抽象类继承注释?

 旺仔牛奶糖2702938317 发布于 2023-01-30 19:01

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

至少以下不起作用:

@Service
@Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
class AbstractService


class PersonService extends AbstractService {
    @Autowired //will not work due to missing qualifier annotation
    private PersonDao dao;
}

Philipp Sand.. 25

答案是不

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

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

2 个回答
  • 答案是不

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

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

    2023-01-30 19:02 回答
  • 简短的回答是:使用您在示例中提到的注释,没有.

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

    但是,正如您在spring源代码中看到的那样,@Service@Scope注释本身并没有注释@Inherited,因此类的存在@Service@Scope它的子类不会继承.

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

    2023-01-30 19:05 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有