【发布时间】:2014-12-21 03:12:57
【问题描述】:
我正在尝试创建一个包含存储在 Spring Boot 中的属性文件中的值的类
到目前为止,我已经设置了属性“source.Ip”的 sample.properties 文件。
类如下:
@PropertySource({"classpath:sample.properties"})
@Configuration
@Component
public class PropertyLoader {
@Value("${source.Ip}")
private String sourceIp;
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
public String getSourceIP(){
return sourceIp;
}
}
属性文件直接在源文件夹 src/main/resources 下。
但是我得到一个:
java.lang.IllegalArgumentException: Could not resolve placeholder 'source.Ip' in string value "${source.Ip}"
至于我的主类,简单如下:
@Configuration
@EnableAutoConfiguration
public class AppStarter {
public static void main(String args[]){
System.out.println(SpringApplication.run(AppStarter.class, args).getBean(PropertyLoader.class).getSourceIP());
}
@Bean
public PropertyLoader propertyLoader(){
return new PropertyLoader();
}
}
sample.properties 内容:
source.Ip=127.0.0.1
【问题讨论】:
标签: java spring spring-mvc properties spring-boot