獲取值我有一個樣品春季啓動應用程序下面的代碼@Value與規劃環境地政司無法從性能
@Configuration
@PropertySource("classpath:second.properties")
public class PropertyConfig {
@Value("#{guru.username}")
String user;
@Value("#{guru.password}")
String password;
@Value("#{guru.url}")
String url;
@Bean
FakeDataSource getFakeDataSource() {
FakeDataSource fk = new FakeDataSource();
fk.setName(user);
fk.setPassword(password);
fk.setUrl(url);
return fk;
}
@Bean
PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer placeholderConfigurer= new PropertySourcesPlaceholderConfigurer();
//placeholderConfigurer.setLocation(new ClassPathResource("second.properties"));
return placeholderConfigurer;
}
}
而且FakeDataSource是一個簡單的POJO的名稱,passowrd,URL屬性。
然後我的主要應用
@SpringBootApplication
public class SpringGuru101DependencyInjectionApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(SpringGuru101DependencyInjectionApplication.class, args);
// Step 2: Make Class
FakeDataSource fakeDataSource = ctx.getBean(FakeDataSource.class);
System.out.println(fakeDataSource.getName());
}
}
但SOUT語句打印空, 我second.properties文件存在於我的資源目錄中有以下內容
guru.username=Saurabh
guru.password=ido
guru.url=http://example.com
嘗試'@ImportResource( 「classpath:second.properties」)' –
請嘗試將井號(#)替換爲美元符號($),以便從配置文件中讀取值。例如:'@Value(「$ {guru.username}」)' – LHCHIN
@ScaryWombat應用程序無法編譯,如果我使用@ImportSource(_classpath_) 'org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:Line 1 in來自類路徑資源[second.properties]的XML文檔無效;嵌套異常是org.xml.sax.SAXParseException; lineNumber:1; columnNumber:1;內容沒有prolog.'不允許 – Saurabh