Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quarkus CDI with custom annotations

Tags:

java

cdi

quarkus

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

like image 627
JYC Avatar asked Oct 27 '25 06:10

JYC


1 Answers

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.

like image 185
Ladicek Avatar answered Oct 29 '25 20:10

Ladicek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!