0

I'd like to follow clean/hexagonal architecture and therefore I'd like to keep my services/interactors clean of any spring dependencies.

Is it possible to replace the @Component annotation with a custom annotation and register it with spring, so my new annotation does the same as the built-in @Component annotation?

An alternative might be to use XML configuration. But I'd like to refrain from that if possible.

crushervx
  • 587
  • 1
  • 7
  • 18
  • What would you gain? You could just create your own `@YourComponent` annotation and add the `@Component` on that as well (a composed annotation). That way you don't need to do additional work, and your only point of Spring related code is the annotation (but I suspect you also use `@Transactional` and others, so the question remains what do you gain). – M. Deinum Sep 16 '21 at 05:28

1 Answers1

2

Just found out. This is perfectly possible:

  1. Add Annotation to Codebase
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Animal { }
  1. Add @ComponentScan annotation to your Spring-Boot application entrypoint class.
@ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION,
        classes = Animal.class))

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

crushervx
  • 587
  • 1
  • 7
  • 18