Java 正在尝试将env变量读取到spring引导中application.property中未包含的自定义属性文件中
我试图从custom.properties文件中的env变量中读取一些凭据,但它无法识别下面给出的属性文件的代码,但它无法像我这样提取env变量Java 正在尝试将env变量读取到spring引导中application.property中未包含的自定义属性文件中,java,spring,spring-boot,maven,spring-data,Java,Spring,Spring Boot,Maven,Spring Data,我试图从custom.properties文件中的env变量中读取一些凭据,但它无法识别下面给出的属性文件的代码,但它无法像我这样提取env变量“${varName}” 有人能帮我找到如何将env变量保存到custom.properties文件中的方法吗?请尝试下面的示例代码 它从自定义特性文件中读取带有前缀值的特性。 例如:下面的示例使用org.apache.ws.security.crypto.provider键从属性中读取值 @Configuration @PropertySource(v
“${varName}”
有人能帮我找到如何将env变量保存到custom.properties文件中的方法吗?请尝试下面的示例代码 它从自定义特性文件中读取带有前缀值的特性。 例如:下面的示例使用org.apache.ws.security.crypto.provider键从属性中读取值
@Configuration
@PropertySource(value = "classpath:<properytfilename>.properties")
@ConfigurationProperties(prefix = "org.apache.ws.security.crypto")
public class CustomProperties {
private String provider;
}
@配置
@PropertySource(value=“classpath:.properties”)
@ConfigurationProperties(前缀=“org.apache.ws.security.crypto”)
公共类自定义属性{
私有字符串提供者;
}
使用java.util.Properties
....
@Value("${what.ever.env}")
private String keystorePassword;
...
Properties properties = new Properties();
properties.setProperty("org.apache.ws.security.crypto.provider", "org.apache.wss4j.common.crypto.Merlin");
properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", keystorePassword);
File file = new File("custom.properties");
FileOutputStream fileOut = new FileOutputStream(file);
properties.store(fileOut, "Comment here");
fileOut.close();
...
将属性添加到Spring application.properties文件。否则,您需要编写一个实用程序类来读取带有环境变量的属性值。检查此答案否我想要的是从环境变量到自定义属性文件中获取值我不想从custom.property获取值,但我想从env variable获取自定义属性中的值这段代码是从custom获取值属性我想要的是从环境变量到自定义属性文件的值不想要从custom.property得到值,但是我想要从env variable得到自定义属性中的值,但是这是在运行时写入文件,我正在尝试我想要从env变量直接读取到属性文件,就像我们可以读入一样application.properties文件
....
@Value("${what.ever.env}")
private String keystorePassword;
...
Properties properties = new Properties();
properties.setProperty("org.apache.ws.security.crypto.provider", "org.apache.wss4j.common.crypto.Merlin");
properties.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", keystorePassword);
File file = new File("custom.properties");
FileOutputStream fileOut = new FileOutputStream(file);
properties.store(fileOut, "Comment here");
fileOut.close();
...