【发布时间】:2017-10-04 12:33:50
【问题描述】:
需要帮助,问题出在哪里?
我有一个将属性加载为的配置类
WebConfig.java
@Configuration
@PropertySource(value={"classpath:application.properties"})
class WebConfig extends WebMvcConfigurerAdapter{
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
我有另一个配置类,我正在尝试将属性用作
MyServerConfig.java
@Configuration
class MyServerConfig {
@Value("${server.url}")
private String url;
...
}
application.properties
server.url=http://localhost:8080/test/abc
但是得到:
java.lang.IllegalArgumentException:无法解析占位符“server.url”。
不知道这里缺少什么?有什么想法吗?
【问题讨论】:
-
你为什么在
WebConfigstatic类中创建方法propertySourcesPlaceholderConfigurer? -
你能发布你的配置 application.properties 文件吗?
-
@Jesper 只是尝试我在谷歌找到的解决方案之一,即使您删除静态或完全删除该方法也无法正常工作
-
@SamDev 已经在问题中了,再检查一遍
标签: java spring dependency-injection properties