我正在构建一个库,该库为使用我们的应用程序提供固执己见的配置Spring Cloud Config/Eureka
设置。这个想法是将此配置作为自定义启动器提供,在各个微服务应用程序中很少或没有与 Spring Cloud 相关的样板。
此时,我想要放入此库中的大部分共享配置由以下内容组成bootstrap.yml
。我想提供bootstrap.yml
在我的自定义启动器中,但是使用该库的应用程序仍然需要能够提供自己的bootstrap.yml
,即使只是为了让他们可以正确设置他们的 spring.application.name 。
由于方式bootstrap.yml
从类路径加载,如果应用程序有自己的类路径,Spring 似乎会忽略共享库中的类路径bootstrap.yml
。我什至不能使用ApplicationContextInitializer
由于引导上下文处理的特殊方式来自定义环境ApplicationContextInitializers
.
有人对在这里可行的方法有任何建议吗?我想提供一个嵌入式库,使我们固执己见的引导程序配置可以工作,而无需复制样板文件bootstrap.yml
在我们所有的项目中。
您可以使用以下命令将共享库中的 PropertySource 添加到引导属性org.springframework.cloud.bootstrap.BootstrapConfiguration
键入META-INF/spring.factories
file.
例如,您可以创建一个包含以下内容的库:
src/main/java/com/example/mylib/MyLibConfig.java
package com.example.mylib;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:mylib-config.properties")
public class MyLibConfig {
}
src/main/resources/mylib-config.properties
eureka.instance.public=true
# or whatever...
src/main/resources/META-INF/spring.factories
org.springframework.cloud.bootstrap.BootstrapConfiguration=com.example.mylib.MyLibConfig
更多细节:http://projects.spring.io/spring-cloud/spring-cloud.html#_customizing_the_bootstrap_configuration http://projects.spring.io/spring-cloud/spring-cloud.html#_customizing_the_bootstrap_configuration
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)