【问题标题】:Portability issue with MicroProfile Config implementation in Thorntail在 Thorntail 中实现 MicroProfile Config 的可移植性问题
【发布时间】:2019-06-18 16:46:44
【问题描述】:

这个问题是关于以可移植方式读取 REST 服务中配置的正确方法,例如应该在 Thorntail 2.4.0Wildfly 15 上运行。

这是 Thorntail 建议的原始实现

@Inject
    @org.wildfly.swarm.spi.runtime.annotations.ConfigurationValue("swarm.port.offset")
private Optional<String> portOffset;

这在 WildFly 15 中不起作用,因此我们按以下方式更改了此代码:

@Inject
@ConfigProperty(name="swarm.port.offset")
private Optional<String> portOffset;

并且只要设置了系统属性,它就可以很好地工作。

但是,在 Thorntail 中,它会生成以下异常:

org.jboss.weld.exceptions.DeploymentException: 焊接-001408: 带有限定符的 Optional 类型的不满足依赖关系 @ConfigProperty
在注入点 [BackedAnnotatedField] @Inject @ConfigProperty 私人 com.my-company.core.internal.util.ZookeeperRegistry.portOffset
在 com.my-company.core.internal.util.ZookeeperRegistry.portOffset(ZookeeperRegistry.java:0) WELD-001475:以下 bean 按类型匹配,但没有匹配 限定词: - 生产者方法 [可选] 带有限定符 [@Any @ConfigurationValue] 声明为 [[UnbackedAnnotatedMethod] @ConfigurationValue @Dependent @Produces org.wildfly.swarm.container.runtime.cdi.ConfigurationValueProducer.produceOptionalConfigValue(InjectionPoint)]

非常感谢。

【问题讨论】:

  • 这看起来就像您只依赖于 MicroProfile 配置 API (org.eclipse.microprofile.config:microprofile-config-api),而不依赖于 Thorntail MP 配置部分 (io.thorntail:microprofile-config)。不是这样吗?

标签: java jakarta-ee wildfly thorntail microprofile


【解决方案1】:

代码最终在两个环境中运行,只有一个 pom 文件。

我在下面详细介绍了采用的解决方案。

  1. 使用@ConfigProperty,而不是@org.wildfly.swarm.spi.runtime.annotations.ConfigurationValue
  2. 使用 @Any @ConfigProperty,已解决 WELD-001475
  3. 在 maven 依赖方面,无论我们是为 Thorntail 还是为 WildFLy 构建,我都包含了这个依赖

    <dependency>
        <groupId>org.eclipse.microprofile.config</groupId>
        <artifactId>microprofile-config-api</artifactId>
    </dependency>
    

使用 Eclipse 微配置文件的 dependencyManagement 解析实际版本:

<dependencyManagement>
    <dependencies>
        <dependency>
           <groupId>org.eclipse.microprofile</groupId>
           <artifactId>microprofile</artifactId>
           <version>2.2</version>
           <type>pom</type>
           <scope>import</scope>
       </dependency>   
...
</dependencyManagement>
  1. Maven 配置文件用于导入非“核心”的 Thorntail 实现,例如microprofile-health,但在 microprofile-config 的情况下,没有必要。对于 WildFly,提供了实现 org.wildfly.extension.microprofile.config.smallrye,因此该库不应包含在 war/ear 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多