我有2个模块在我的项目应用程序和启动器。Starter包含@Configuration并告诉应该如何创建ServiceFoo的bean。
@Configuration
@EnableConfigurationProperties(FooServiceConfiguration.class)
public class StarterFoo {
@Bean
public ServiceFoo defaultBean(FooServiceConfiguration conf){
new ServiceFooImpl(conf.getName(), conf.getNumber());
}
}我的starter中有另一个配置类。
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("Foo")
public class FooServiceConfiguration {
private String name;
private int number;
// + accessors
}在我的启动器中,我有application.yml,它有
Foo:
name: DefaultName
number: 101starter配置为自动配置
META-INF/spring.factory
org.springframework.boot.autoconfigure.EnableAutoConfiguration=StarterFoo我想在我的配置上有关于数字的意见,用户永远不会担心和覆盖那个数字。我希望用户覆盖我配置中的名称。
一旦我在应用程序(空白文件)中创建了application.yml,starter的配置(来自starter的application.yml )的效果就消失了。
如何部分覆盖在starter中定义的应用程序中的此配置?
发布于 2018-11-19 10:32:57
只能有一个具有特定名称的引导配置文件,而不管它们位于类路径的什么位置(即,您可以有application-test.yml和application.yml,但只能有其中一个),并且“更接近”运行时( fat jar)覆盖更远的(嵌入式jar)。Boot并不合并内容,它只读取一个application.yml。
要完成您想要的任务,最简单的方法是正常使用Java,并使用默认值初始化类变量:
private String name = "DefaultName";
private int number = 101;https://stackoverflow.com/questions/53367341
复制相似问题