'What to use instead of deprecated @ConfigPrefix("prefix") in quarkus
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?
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
