2

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

Trammalant
  • 31
  • 4
  • Does this answer your question? [Using \`@ConfigurationProperties\` annotation on \`@Bean\` Method](https://stackoverflow.com/questions/43232021/using-configurationproperties-annotation-on-bean-method) – Joe Apr 26 '22 at 12:37
  • 1
    @Joe sadly not as that answer specifies the use of a POJO 'DataSource' class, where my issue is with the already present `@ConfigurationProperties` annotation on the third-party library class. If it were a simple POJO with no annotations that indeed would solve it – Trammalant Apr 26 '22 at 12:40

1 Answers1

1

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();
    }
}
Trammalant
  • 31
  • 4