Load third party library with different config

Viewed 36

Suppose I have the following third party class

@ConfigurationProperties(prefix = "third.party")
public class ThirdParty {
    String value;
}

Now I'd like to create two separate beans automatically from the config without using setters.

Desired config:

some:
  third:
    party:
      value: some-third-party-value
another:
  third:
    party:
      value: another-third-party-value

I'd like to achieve something along the lines of:

@Configuration
@ConfigurationProperties
class ApplicationConfig {

    @Bean(name = "someThirdParty")
    @ConfigurationProperties(prefix="some")
    public ThirdParty someThirdParty() {
        return new ThirdParty();
    }

    @Bean(name = "anotherThirdParty")
    @ConfigurationProperties(prefix="another")
    public ThirdParty anotherThirdParty() {
        return new ThirdParty();
    }
}

Is this possible? Or should I find another way?

Note:

The code above would work with a class that does not have the @ConfigurationProperties annotation present. Now they seem to 'clash' but as it's a class from a third-party lib I cannot edit the annotation

1 Answers

Found out that the top-level overwrites the already present ConfigurationProperties on the ThirdParty class.

So, using the 'desired' config in the original question, the following code will work

@Configuration
@ConfigurationProperties
class ApplicationConfig {

    @Bean(name = "someThirdParty")
    @ConfigurationProperties(prefix="some.third.party")
    public ThirdParty someprop() {
        return new ThirdParty();
    }

    @Bean(name = "anotherThirdParty")
    @ConfigurationProperties(prefix="another.third.party")
    public ThirdParty anotherprop() {
        return new ThirdParty();
    }
}
Related