quarkus中使用什么而不是弃用@configprefix(“ prefix”)

发布于 2025-01-22 15:05:09 字数 830 浏览 1 评论 0 原文

我想在应用程序文件中注入具有相同名称但前缀不同的前缀的属性。 例如:

application.properties:

greeting.message = hello
bye.message = bye

为此, documentation 建议使用以下用法:

@ConfigProperties(prefix = "greeting")
public class GreetingConfiguration {

    @Size(min = 20)
    public String message;

}

@ApplicationScoped
public class SomeBean {

    @Inject 
    GreetingConfiguration greetingConfiguration;

    @ConfigPrefix("bye") 
    GreetingConfiguration byeConfiguration;

}

这种方式 :可以使用相同的变量(消息)调用engringConfiguration.message和Byeconfiguration消息。

但不建议使用它,因为@configproperties和@configprefix注释被弃用。

建议改用@configmapping。

同样,我如何使用具有相同名称的不同前缀和属性,而不会在Java侧创建多个变量(EX: - 消息)?

I want to inject properties that have the same names but different prefixes in the application.properties file.
For example :

application.properties :

greeting.message = hello
bye.message = bye

For this, the documentation suggests the following usage:

@ConfigProperties(prefix = "greeting")
public class GreetingConfiguration {

    @Size(min = 20)
    public String message;

}

@ApplicationScoped
public class SomeBean {

    @Inject 
    GreetingConfiguration greetingConfiguration;

    @ConfigPrefix("bye") 
    GreetingConfiguration byeConfiguration;

}

This way I can call greetingConfiguration.message and byeConfiguration message using the same variable (message).

But it's use is not recommended because @ConfigProperties and @ConfigPrefix annotations are deprecated.

It is recommended to use @ConfigMapping instead.

Likewise, How can I use different prefixes and properties with the same name without creating more than one variable (ex :- message) on the java side?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿童谣 2025-01-29 15:05:09

根据文件

使用配置映射可以分组多个配置
单个接口中共享相同前缀的属性。配置映射需要具有最小元数据配置的接口,并用 @io.smallrye.config.configmapping注释进行注释。

示例(同一前缀),

application.properties

message.greeting= hello
message.bye= bye

消息接口

import io.quarkus.runtime.annotations.StaticInitSafe;
import io.smallrye.config.ConfigMapping;

@StaticInitSafe
@ConfigMapping(prefix = "message")
interface Message {
    String greeting();

    String bye();
}

config映射接口可以注入任何CDI意识到的bean:

@Path("/backend/api/v1")
public class BackEndSecretResource {

    @Inject
    Message message;

    @GET
    public String get() {
        return message.greeting() + message.bye();
    }
}

因为由于以下限制。

 @Experimental("ConfigMapping API to group configuration properties")
    public @interface ConfigMapping {
        /**
         * The prefix of the configuration properties.
         *
         * @return the configuration property prefix
         */
        String prefix() default "";
    
        NamingStrategy namingStrategy() default NamingStrategy.KEBAB_CASE;
    
        enum NamingStrategy {
          
            VERBATIM,
          
            KEBAB_CASE,
          
            SNAKE_CASE
        }
    }

在同一后缀的情况下,这是不可能的, 仍然是一种罕见的情况,也许您可​​以找出其他方法。

注明 * @exprementim-指定元素是实验性并且可能会改变而无需通知的注释。

参考- https://quarkus.io/guides/guides/guides/config-mappings/config-mappings

refer-a> refer-refo- https://smallrye.io/docs/docs/docs/docs/smallrye-config/main/main/main/mappapple/mapping/mpapping/mpapping一下。 html

As per the document

With config mappings it is possible to group multiple configuration
properties in a single interface that share the same prefix. A config mapping requires an interface with minimal metadata configuration and annotated with the @io.smallrye.config.ConfigMapping annotation.

Example(Same prefix),

application.properties

message.greeting= hello
message.bye= bye

Message Interface

import io.quarkus.runtime.annotations.StaticInitSafe;
import io.smallrye.config.ConfigMapping;

@StaticInitSafe
@ConfigMapping(prefix = "message")
interface Message {
    String greeting();

    String bye();
}

A config mapping interface can be injected into any CDI aware bean:

@Path("/backend/api/v1")
public class BackEndSecretResource {

    @Inject
    Message message;

    @GET
    public String get() {
        return message.greeting() + message.bye();
    }
}

This is not possible in the case of the same suffix because due to the below limitation.

 @Experimental("ConfigMapping API to group configuration properties")
    public @interface ConfigMapping {
        /**
         * The prefix of the configuration properties.
         *
         * @return the configuration property prefix
         */
        String prefix() default "";
    
        NamingStrategy namingStrategy() default NamingStrategy.KEBAB_CASE;
    
        enum NamingStrategy {
          
            VERBATIM,
          
            KEBAB_CASE,
          
            SNAKE_CASE
        }
    }

Means String suffix() unavailable. Still, yours is a rare scenario, maybe you can figure out other ways.

Note * @Experimental - Annotation that specifies that an element is experimental and may change without notice.

Refer - https://quarkus.io/guides/config-mappings

Refer - https://smallrye.io/docs/smallrye-config/main/mapping/mapping.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文