我很難從屬性文件中加載屬性。我試圖加載一個屬性到我下面列出的配置類中。屬性和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