SpringJava配置环境变量
我使用Spring,从xml配置切换到java配置。 实际上,我面临着一个环境变量的问题,因为我不知道如何检索环境变量的值 对于xml配置,我有以下几点SpringJava配置环境变量,java,spring,variables,configuration,environment,Java,Spring,Variables,Configuration,Environment,我使用Spring,从xml配置切换到java配置。 实际上,我面临着一个环境变量的问题,因为我不知道如何检索环境变量的值 对于xml配置,我有以下几点 <bean id="myAppProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="file:${MY_ENV_VAR}/appli
<bean id="myAppProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="file:${MY_ENV_VAR}/applicationConfiguration/external.properties"/>
<property name="fileEncoding" value="UTF-8"/>
</bean>
但是没有成功。
我试过上环保课,但没有任何改进
您能帮助我吗?您可以使用
@Value
注释或@ConfigProperties
-类注入它们
试试这个:
@Bean
public PropertiesFactoryBean cvlExternalProperties(@Value("${MY_ENV_VAR}") String envVar) {
PropertiesFactoryBean res = new PropertiesFactoryBean();
res.setFileEncoding("UTF-8");
res.setLocation(new FileSystemResource("file:" + envVar + "/applicationConfiguration/external.propert ies"));
return res;
}
您可以在这里找到更多内容:您可以使用
@Value
注释或@ConfigProperties
-类注入它们
试试这个:
@Bean
public PropertiesFactoryBean cvlExternalProperties(@Value("${MY_ENV_VAR}") String envVar) {
PropertiesFactoryBean res = new PropertiesFactoryBean();
res.setFileEncoding("UTF-8");
res.setLocation(new FileSystemResource("file:" + envVar + "/applicationConfiguration/external.propert ies"));
return res;
}
您可以在这里找到更多信息:我找到了一个解决方案,但我正在尝试一个最好的解决方案 有效的解决办法是
你能帮我吗?我已经找到了解决办法,但我正在尝试最好的办法 有效的解决办法是
您能帮我吗?不幸的是,此解决方案不起作用。错误是java.io.FileNotFoundException:${MY_ENV_VAR}external.properties(不可能的trovare il file specificato)您能告诉我们您有哪些PropertySource吗?您正在使用spring boot吗?我没有使用spring boot,目前我还没有声明任何属性资源。不幸的是,此解决方案不起作用。错误是java.io.FileNotFoundException:${MY_ENV_VAR}external.properties(不可能的trovare il file specificato)您能告诉我们您有哪些PropertySource吗?你在使用spring boot吗?我没有使用spring boot,目前我还没有声明任何propertySource
@Autowired
private Environment env;