1

I have 2 modules in my project app & starter. Starter contains @Configuration and tells how a bean of ServiceFoo should be created.

@Configuration
@EnableConfigurationProperties(FooServiceConfiguration.class)
public class StarterFoo {

    @Bean
    public ServiceFoo defaultBean(FooServiceConfiguration conf){
      new ServiceFooImpl(conf.getName(), conf.getNumber());
    }

}

I have another configuration class in my starter.

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("Foo")
public class FooServiceConfiguration {
    private String name;
    private int number;
    // + accessors
}

in my starter I have application.yml which has

Foo:
 name: DefaultName
 number: 101

starter is configured to be auto-configured

META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=StarterFoo

I want to have opinion on my config about number and user will never worry and override that number. I want users to override name in my config.

As soon as I create application.yml in app (blank file) the effect of starter's config (from application.yml from starter) goes away.

How can I partially override this config from app which is defined in starter ?

user3833308
  • 1,132
  • 3
  • 14
  • 39

1 Answers1

1

There can only be a single Boot configuration file with a specific name regardless of where on the classpath they're located (i.e., you can have application-test.yml and application.yml, but only one of each), and "closer" to runtime (the fat jar) overrides more distant (embedded jars). Boot doesn't merge the contents, it simply only reads a single application.yml.

The simplest way to accomplish what you want is to use Java normally and initialize the class variables with your default values:

private String name = "DefaultName";
private int number = 101;
chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
  • Thank you. Yes hardcoding default is an option. How do I read it from config if not `application.yml` may be `foo.yml` for that particular `foo` starter ? – user3833308 Nov 19 '18 at 03:25
  • The usecase is I want to control some third party starters within my org and provide wrapper starter around it. Those wrapper should be external starter + my config – user3833308 Nov 19 '18 at 03:53
  • 1
    @user3833308 That use case is very tricky to handle cleanly. The least-complicated option is probably to use a special profile and supply the `application-profile.yml`. – chrylis -cautiouslyoptimistic- Nov 19 '18 at 05:35
  • That will do the trick. Thanks Chrylis, Basically I have a starter where I want to configure the embedded tomcat's configuration. If you know of any other way please share – user3833308 Nov 19 '18 at 05:58
  • 1
    @user3833308 It really depends on the organizational setup; for example, unless you can control the specifics of the Boot configuration in the end projects it's not even a given that Tomcat will be used (I prefer Undertow). – chrylis -cautiouslyoptimistic- Nov 19 '18 at 18:16
  • sure I have control of org setup. what is the preferred way to control these ? – user3833308 Nov 19 '18 at 19:22
  • @user3833308 You just have an organizational policy that says "all of our Boot Web projects use Tomcat". – chrylis -cautiouslyoptimistic- Nov 19 '18 at 19:37
  • I have a starter that everybody will use in org. that starter has to control configuration of tomcat. every app will include the base starter – user3833308 Nov 19 '18 at 21:08