Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring云配置:以编程方式定义服务器配置属性_Java_Spring_Spring Cloud_Spring Cloud Config - Fatal编程技术网

Java Spring云配置:以编程方式定义服务器配置属性

Java Spring云配置:以编程方式定义服务器配置属性,java,spring,spring-cloud,spring-cloud-config,Java,Spring,Spring Cloud,Spring Cloud Config,我已经自行配置了SpringCloudConfigServer(它将自己用于自己的配置) 所以基本上我只有一个文件bootstrap.properties,内容如下: spring.cloud.config.server.bootstrap=true spring.cloud.config.server.git.uri=<my GitHub repo> spring.application.name=config spring.profiles.active=development

我已经自行配置了SpringCloudConfigServer(它将自己用于自己的配置)

所以基本上我只有一个文件
bootstrap.properties
,内容如下:

spring.cloud.config.server.bootstrap=true
spring.cloud.config.server.git.uri=<my GitHub repo>
spring.application.name=config
spring.profiles.active=development
spring.cloud.config.server.bootstrap=true
spring.cloud.config.server.git.uri=
spring.application.name=config
spring.profiles.active=development
它工作得很好,但我想用Java代码定义上面的属性。事实上,我可以保留这些属性,我只想以编程方式添加
spring.cloud.config.server.git.username
spring.cloud.config.server.git.password
。有可能吗


我尝试使用常见的方法添加/覆盖
应用程序.properties
中定义的Spring属性,但没有成功:看起来像
引导程序。属性应该以其他方式(如果可能的话)以编程方式声明。

好吧,在深入研究Spring源代码之后,我设法找到了解决办法。我不喜欢它,因为它看起来像一个肮脏的黑客,但至少它的工作。如果有人提出更清洁的解决方案,我将不胜感激

参考资料/META-INF/spring.factories:

# Bootstrap components
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
my.awesome.package.ConfigClientBootstrapConfiguration
ConfigClientBootstrapConfiguration:

@Configuration
public class ConfigClientBootstrapConfiguration {

    @Autowired
    private ConfigurableEnvironment environment;
    @Autowired
    private ConfigServerProperties server;

    @Bean
    public MultipleJGitEnvironmentRepository environmentRepository() {
        Map<String, Object> properties = new HashMap<>();
        properties.put("spring.cloud.config.server.bootstrap", "true");
        properties.put("spring.cloud.config.server.git.uri", "<GitHub repo URI>");
        properties.put("spring.cloud.config.server.git.username", "username");
        properties.put("spring.cloud.config.server.git.password", "password");
        properties.put("spring.application.name", "config");
        properties.put("spring.profiles.active", "development");
        MapPropertySource customPropertySource = new MapPropertySource("applicationConfig: [classpath:/bootstrap.properties]", properties);
        environment.getPropertySources().replace("applicationConfig: [classpath:/bootstrap.properties]", customPropertySource);
        MultipleJGitEnvironmentRepository repository = new MultipleJGitEnvironmentRepository(this.environment);
        if (this.server.getDefaultLabel() != null) {
            repository.setDefaultLabel(this.server.getDefaultLabel());
        }
        return repository;
    }

}
@配置
公共类ConfigClientBootstrapConfiguration{
@自动连线
私人可配置环境;
@自动连线
私有配置服务器属性服务器;
@豆子
公共MultipleGitEnvironmentRepository环境存储库(){
映射属性=新的HashMap();
properties.put(“spring.cloud.config.server.bootstrap”、“true”);
properties.put(“spring.cloud.config.server.git.uri”,”;
properties.put(“spring.cloud.config.server.git.username”、“username”);
properties.put(“spring.cloud.config.server.git.password”、“password”);
properties.put(“spring.application.name”、“config”);
properties.put(“spring.profiles.active”、“development”);
MapPropertySource customPropertySource=新的MapPropertySource(“applicationConfig:[classpath:/bootstrap.properties]”,properties);
environment.getPropertySources().replace(“applicationConfig:[classpath:/bootstrap.properties]”,customPropertySource);
MultipleJGitEnvironmentRepository=新的MultipleJGitEnvironmentRepository(this.environment);
if(this.server.getDefaultLabel()!=null){
repository.setDefaultLabel(this.server.getDefaultLabel());
}
返回存储库;
}
}
另外,还可以在现有属性上添加迭代,以使它们保持在文件中(并可能覆盖)

p.p.S.我不知道为什么,但它只适用于类名
ConfigClientBootstrapConfiguration
。当我重新命名它时,它停止工作了。我不太关心这里的命名…

为什么您更喜欢:

properties.put("spring.cloud.config.server.git.uri", "<GitHub repo URI>");
properties.put("spring.cloud.config.server.git.username", "username");
properties.put("spring.cloud.config.server.git.password", "password");
properties.put(“spring.cloud.config.server.git.uri”,”);
properties.put(“spring.cloud.config.server.git.username”、“username”);
properties.put(“spring.cloud.config.server.git.password”、“password”);
要将它们添加到
bootstrap.yml


或者通过VM参数(
…-Dspring.cloud.config.server.git.uri=xxxxxx…
)或通过env变量(
…SPRING\u cloud\u config\u server\u git\u uri=xxxxx…java-jar application.jar
)设置它们

    username: ${SPRING_CLOUD_CONFIG_USERNAME:defaultuser}
    password: ${SPRING_CLOUD_CONFIG_PASSWORD:defaultpass}
并将这些值作为环境变量添加到-

SPRING_CLOUD_CONFIG_USERNAME
SPRING_CLOUD_CONFIG_PASSWORD

bootstrap.properties是spring framework首先读取的内容之一。我不认为您可以通过编程方式添加到它。但我相信您可以从命令行(如果您不想将其放入属性文件)@pvpkiran中传递该值。这是我的第一个想法。但后来我想到了一个“黑客”方法解决方案,请在下面找到。你为什么称它为肮脏的黑客?首先,我认为它很麻烦:通常,Spring提供了更平滑的配置方式。为什么我应该知道一些
Spring.factories
?我以前从未使用过它,理想情况下我宁愿不知道这个文件:)其次,我找不到向现有属性添加属性的简单方法:我必须迭代现有属性并手动将它们复制到新映射中,然后添加两个我自己的属性,然后重新设置此映射。我还认为这有点麻烦,一点也不优雅。