javaconfigurationquarkusproperties-filesmallrye

Quarkus Load multiple Configurations to create multiple Instance of the same Java-Configuration Class


I have a .properties file with the following example content:

test1.name=test1
test1.url=www.test.com

test2.name=test2
test2.url=www.test2.com

I want to map this to different instances of the same Java interface/class in Quarkus. Currently, I’ve found the following approach, which should work:

interface Generic {
  String name();
  String url();
}

@ConfigMapping(prefix = "test1")
interface Test1 extends Generic {}

@ConfigMapping(prefix = "test2")
interface Test2 extends Generic {}

However, since I need several instances of this class based on configurations, I wanted to ask if there’s a smarter way to achieve this (without so many interfaces). Here a Spring Boot inspired example:

@Dependent
class Configuration {

  @Produces
  @Name("test1")
  Generic test1(@ConfigMapping(prefix="test1") Generic test1) {
    return test1;
  }

  @Produces
  @Name("test2")
  Generic test2(@ConfigMapping(prefix="test2") Generic test2) {
    return test2;
  }
}

I would also be very interested in other solutions to this type of problem.

Thanks in advance!


Solution

  • You can override the prefix on injection point as:

    @Inject
    @ConfigMapping // uses the one set in the interface
    Generic generic
    
    @Inject
    @ConfigMapping(prefix = "override")
    Generic genericOverride