2

With following yml

app:
  a:
    prop: aaa
  b:
    prop: bbb
@Component
public abstract class Common {

    @Value("${prop}")
    private String prop;

    @ConfigurationProperties(prefix = "app.a")
    @PropertySource("classpath:app.yml")
    @Component
    public static class A extends Common {
    }

    @ConfigurationProperties(prefix = "app.b")
    @PropertySource("classpath:app.yml")
    @Component
    public static class B extends Common {
    }
}

But those two classes has same value, either for a or b.

How can I solve this?

Jin Kwon
  • 20,295
  • 14
  • 115
  • 184

2 Answers2

2

I found the problem. Simply. yml doesn't work with PropertySource.

I'm still want to believe I'm wrong.

I changed the .yml file to properties and tried with this.

@PropertySource("classpath:/vendor.properties")
@EnableConfigurationProperties
public abstract class Common {

    @Value("${prop}")
    private String prop;

    @ConfigurationProperties(prefix = "app.a")
    @Component
    public static class A extends Common {
    }

    @ConfigurationProperties(prefix = "app.b")
    @Component
    public static class B extends Common {
    }
}

And it worked.

Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
1

You could use a list for your configuration parameters :

app:
  props: 
    - key: a
      value: aaa

    - key: b
      value: bbb

And retreive your value with a more complex way in a separate bean :

@ConfigurationProperties(prefix = "app")
public class CommonConfiguration {
    List<Prop> props;
    //Getters and setters
    public Prop retreiveSpecificConfiguration(String className) {
        //some kind of logic here
    }

    public static class Prop {
      private String key, value;
      //Getters and setters
    }
}

Inject it in your Common class implementation :

@Autowired
CommonConfiguration config;
Karbos 538
  • 2,977
  • 1
  • 23
  • 34