带有自定义注释的 Quarkus CDI
我目前正在开发一个尽可能尊重六边形架构原则的应用程序。
因此,我的“域”模块(groupId:acme
;artifactId:my-domain
)不依赖于任何技术框架。
我的所有服务都使用自定义注释(本身是我的域的一部分)进行注释:
package acme.domain;
@Target({ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface DomainService {
}
但是,在我的“Quarkus 应用程序”模块中(groupId:acme
;artifactId:app-quarkus
),我需要注入在我的“域”模块(acme:domain
)中定义的服务。
使用SpringBoot,注入这些领域服务非常容易(基于自定义注释)< /a> 带有以下注释:
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 {
}
是否有与 Quarkus 等效的 @ComponentScan
?
注意:我在 app-quarkus/src/main/resources/application.properties
中添加了以下几行,但它不起作用:
quarkus.index-dependency.courtage.group-id=acme
quarkus.index-dependency.courtage.artifact-id=my-domain
抛出异常:javax.enterprise.inject.UnsatisfiedResolutionException
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我个人认为在域模块中严格避免 CDI 注释有点过于热心。但如果你真的想要这样,我可以看到 2 个选项:
你可以限制自己只将 CDI 注释放在你自己的注释上,并使用
@Stereotype
。例如,如果您的@DomainService
应等效于@ApplicationScoped
,则可以这样声明:如果您绝对坚持不应该出现任何 CDI 注释在域模块中,您可以创建一个 Quarkus 扩展,它将注册
@DomainService
作为自定义 bean 定义注释。 Quarkus CDI 集成指南对此有更多详细信息:https://quarkus.io/guides/cdi-integration 您可以使用AutoAddScopeBuildItem
或BeanDefiningAnnotationBuildItem
。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:
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: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 useAutoAddScopeBuildItem
orBeanDefiningAnnotationBuildItem
.Spring 为您提供了一种丑陋的方式来注入基于隔离的 CDI 域,而使用 CDI 标准,您将有两种方法在编译时将任何外部 CDI bean 从 Maven 依赖项添加到您的 CDI 上下文中。
使用 Jandex Index maven 插件:
org.jboss.jandex
jandex-maven-插件
{jandex-maven-插件版本}
制造索引
詹德克斯
配置级别(application.yaml):
quarkus:
索引依赖性:
求爱:
组 ID:org.acme
工件 ID:我的域
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.
using Jandex Index maven plugin:
org.jboss.jandex
jandex-maven-plugin
{jandex-maven-plugin-version}
make-index
jandex
configuration level (application.yaml):
quarkus:
index-dependency:
courtage:
group-id: org.acme
artifact-id: my-domain