我很难从属性文件中加载属性。我试图加载一个属性到我下面列出的配置类中。属性和env始终为空
package dk.fitfit.budget.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
@Configuration
@PropertySource("classpath:application.properties")
public class ApplicationConfig {
private static final Logger logger = LoggerFactory.getLogger(ApplicationConfig.class);
@Autowired
private Environment env;
@Value("${snot:test}")
private String snot;
public ApplicationConfig() {
logger.info("Application config loaded!"); // Displays as expected
logger.info("snot: {}", snot); // snot: null
logger.info("env: {}", env); // env: null
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
如果我更改文件(application.properties)或“@PropertySource的名称(”类路径:application.properties“)”我得到一个错误约不存在的文件。很显然,它正在被加载(无论如何)。
我至少期待看到默认字符串“测试”注入变量鼻涕。但即使这样做也完成了。我无法自动装入环境......不知道是否有关系。
我的application.properties的内容如下。
snot=snog
我的application.properties文件放在src/main/resources /中。
任何人都知道我做错了什么线索?
恭喜你,我得到了最后一票......不能再投一小时。 – 2014-10-19 23:17:15