【问题标题】:How to create a custom Spring Boot @component - annotation to adhere clean-code/architecture principles?如何创建自定义 Spring Boot @component - 注释以遵守干净代码/架构原则?
【发布时间】:2021-09-15 22:14:28
【问题描述】:

我想遵循干净/六边形架构,因此我想保持我的服务/交互器没有任何 spring 依赖项。

是否可以将@Component注解替换为自定义注解并注册到spring,所以我的新注解和内置的@Component注解一样?

另一种方法可能是使用 XML 配置。但如果可能的话,我想避免这样做。

【问题讨论】:

  • 你会得到什么?您可以创建自己的 @YourComponent 注释并在其上添加 @Component (组合注释)。这样你就不需要做额外的工作,你唯一的 Spring 相关代码就是注释(但我怀疑你也使用@Transactional 和其他人,所以问题仍然是你得到了什么)。

标签: spring spring-boot hexagonal-architecture


【解决方案1】:

刚刚发现。这是完全可能的:

  1. 向代码库添加注释
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Animal { }
  1. @ComponentScan 注释添加到您的 Spring-Boot 应用程序入口点类。
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,
        classes = Animal.class))

来源:https://www.baeldung.com/spring-componentscan-filter-type#annotation_filter

【讨论】:

    猜你喜欢
    • 2021-12-09
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 2019-03-15
    • 2022-05-02
    相关资源
    最近更新 更多