@PropertySource 属性 路径未解析
@PropertySource property path not being resolved
我已经设置了环境变量 JAVA_OPTS (-Dappconfig=D:/cc/dd -Dfarm=test 并希望使用以下代码从 属性 读取属性,但无论我尝试了以下错误。我需要针对不同的环境(测试、暂存、开发、生产)执行此操作。感谢任何帮助
Caused by:
java.lang.IllegalArgumentException: Could not resolve placeholder 'appconfig' in string value
"file:${appconfig}/farm/${farm}/myservice.appconfig.properties"
@Configuration
@PropertySource(value = "file:${appconfig}/farm/${farm}/myservice.appconfig.properties", ignoreResourceNotFound = false)
public class AppConfig
{
@Autowired
private Environment environment;
@Bean
public AppConfigSettings getAppConfig()
{
AppConfigSettings properties = new AppConfigSettings()
//I set properties using environment.getRequiredProperty("propertykey") //here
return properties;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer()
{
return new PropertySourcesPlaceholderConfigurer();
}
}
documentation for the PropertySource annotation 的一部分关于 @PropertySource
资源位置中的占位符是这样说的:
Any ${...} placeholders present in a @PropertySource resource location will be resolved against the set of property sources already registered against the environment. For example:
@Configuration
@PropertySource("classpath:/com/${my.placeholder:default/path}/app.properties")
public class AppConfig {
@Autowired
Environment env;
@Bean
public TestBean testBean() {
TestBean testBean = new TestBean();
testBean.setName(env.getProperty("testbean.name"));
return testBean;
}
}
Assuming that "my.placeholder" is present in one of the property sources already registered, e.g. system properties or environment variables, the placeholder will be resolved to the corresponding value. If not, then "default/path" will be used as a default. Expressing a default value (delimited by colon ":") is optional. If no default is specified and a property cannot be resolved, an IllegalArgumentException will be thrown.
我不确定您可能正在使用哪些其他 @PropertySource
注释,或者您是否可以控制它们的应用顺序,但这似乎就是您遇到的情况。
在此点赞answer
我将我的环境变量设置为
_JAVA_OPTIONS= -Dfarm=test -Dappconfig=D:\cc\dd
使用_JAVA_OPTIONS代替JAVA_OPTS
现在完美运行
我已经设置了环境变量 JAVA_OPTS (-Dappconfig=D:/cc/dd -Dfarm=test 并希望使用以下代码从 属性 读取属性,但无论我尝试了以下错误。我需要针对不同的环境(测试、暂存、开发、生产)执行此操作。感谢任何帮助
Caused by:
java.lang.IllegalArgumentException: Could not resolve placeholder 'appconfig' in string value
"file:${appconfig}/farm/${farm}/myservice.appconfig.properties"
@Configuration
@PropertySource(value = "file:${appconfig}/farm/${farm}/myservice.appconfig.properties", ignoreResourceNotFound = false)
public class AppConfig
{
@Autowired
private Environment environment;
@Bean
public AppConfigSettings getAppConfig()
{
AppConfigSettings properties = new AppConfigSettings()
//I set properties using environment.getRequiredProperty("propertykey") //here
return properties;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer()
{
return new PropertySourcesPlaceholderConfigurer();
}
}
documentation for the PropertySource annotation 的一部分关于 @PropertySource
资源位置中的占位符是这样说的:
Any ${...} placeholders present in a @PropertySource resource location will be resolved against the set of property sources already registered against the environment. For example:
@Configuration
@PropertySource("classpath:/com/${my.placeholder:default/path}/app.properties")
public class AppConfig {
@Autowired
Environment env;
@Bean
public TestBean testBean() {
TestBean testBean = new TestBean();
testBean.setName(env.getProperty("testbean.name"));
return testBean;
}
}
Assuming that "my.placeholder" is present in one of the property sources already registered, e.g. system properties or environment variables, the placeholder will be resolved to the corresponding value. If not, then "default/path" will be used as a default. Expressing a default value (delimited by colon ":") is optional. If no default is specified and a property cannot be resolved, an IllegalArgumentException will be thrown.
我不确定您可能正在使用哪些其他 @PropertySource
注释,或者您是否可以控制它们的应用顺序,但这似乎就是您遇到的情况。
在此点赞answer
我将我的环境变量设置为
_JAVA_OPTIONS= -Dfarm=test -Dappconfig=D:\cc\dd
使用_JAVA_OPTIONS代替JAVA_OPTS
现在完美运行