JYC
JYC

Reputation: 393

Quarkus CDI with custom annotations

I am currently developing an application respecting as much as possible the principles of hexagonal architecture.

Thus, my "domain" module (groupId: acme ; artifactId: my-domain) does not depend on any technical framework.

All my services are annotated with a custom annotation (itself part of my domain):

package acme.domain;

@Target({ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface DomainService {
}

However, in my "Quarkus application" module (groupId: acme ; artifactId: app-quarkus), I need to inject services defined in my "domain" module (acme:domain).

With SpringBoot, it is quite easy to inject those domain services (based on a custom annotation) with the following annotation:

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;

@SpringBootApplication
@ComponentScan(
    basePackageClasses = {CourtageSpringbootApplication.class, DomainService.class},
    includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = {DomainService.class})}
)
public class MySpringbootApplication {
}

Is there an equivalent to @ComponentScan with Quarkus?

NB : I have added following lines in app-quarkus/src/main/resources/application.properties but it does not work:

quarkus.index-dependency.courtage.group-id=acme
quarkus.index-dependency.courtage.artifact-id=my-domain

Thrown exception: javax.enterprise.inject.UnsatisfiedResolutionException

Upvotes: 4

Views: 3175

Answers (2)

Hamed Hatami
Hamed Hatami

Reputation: 39

What an ugly way Spring supplies you for injecting your isolated CDI based domain whereas with CDI standard you would have two ways to add any external CDI beans from a maven dependency into your CDI context during compile-time.

  1. using Jandex Index maven plugin:

    org.jboss.jandex jandex-maven-plugin {jandex-maven-plugin-version} make-index jandex
  2. configuration level (application.yaml):

quarkus:
index-dependency: courtage: group-id: org.acme artifact-id: my-domain

Upvotes: 0

Ladicek
Ladicek

Reputation: 6577

First of all, I would personally consider strictly avoiding CDI annotations in your domain module a little bit overzealous. But if you really want that, I can see 2 options:

  1. You could limit yourself to only placing CDI annotations on your own annotations, and use @Stereotype. For example, if your @DomainService should be equivalent to @ApplicationScoped, it could be declared like this:

    @Stereotype
    @ApplicationScoped
    @Target({ElementType.TYPE})
    @Retention(value = RetentionPolicy.RUNTIME)
    public @interface DomainService {
    }
    
  2. If you absolutely insist that no CDI annotation should ever be present in the domain module, you could create a Quarkus extension that would register @DomainService as a custom bean defining annotation. The Quarkus CDI Integration guide has more details about that: https://quarkus.io/guides/cdi-integration You would either use AutoAddScopeBuildItem or BeanDefiningAnnotationBuildItem.

Upvotes: 4

Related Questions