gpt4 book ai didi

spring - 从 Spring Boot 配置中排除 Java 系统属性和环境变量

转载 作者:行者123 更新时间:2023-12-05 04:08:32 25 4
gpt4 key购买 nike

关于SpringBoot的属性解析在这里解释:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

我想从机制中排除:

9.  Java System properties (System.getProperties()).
10. OS environment variables.

这可能吗?

谢谢

最佳答案

在实例化 Spring Boot 应用程序时,您可以提供自己的 StandardEnvironment 实现。

例如:

public static void main(String[] args) {
SpringApplicationBuilder applicationBuilder = new SpringApplicationBuilder(Application.class)
.environment(new StandardEnvironment(){
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
// do not add system or env properties to the set of property sources
}
});
applicationBuilder.run(args);
}

或者:

public static void main(String[] args) {
SpringApplicationBuilder applicationBuilder = new SpringApplicationBuilder(Application.class)
.environment(new StandardEnvironment(){
@Override
public Map<String, Object> getSystemEnvironment() {
return new HashMap<>();
}

@Override
public Map<String, Object> getSystemProperties() {
return new HashMap<>();
}
});
applicationBuilder.run(args);
}

无论哪种方式,您都可以确保应用程序的属性不包含任何系统或环境属性。

关于spring - 从 Spring Boot 配置中排除 Java 系统属性和环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47353695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com