我想從我的Spring MVC應用程序使用基於Java的配置從WEB-INF讀取屬性文件。當我將屬性目錄放在src目錄中並使用class:(ClassPathResource)時,我可以完成這項工作。@Configuration @ImportResource與FileSystemResource從Web-Inf讀取屬性
我想使用文件@ImportResource:(的FileSystemResource)將從當這個位於的WebContent或屬性/或資源/屬性/或/屬性/或/資源/屬性/讀取WEB-INF
當我使用文件:我得到一個FileNotFoundException。
我嘗試過移動屬性目錄並將@ImportResource與「file:/properties/properties-config.xml」,「file:/WebContent/properties/properties-config.xml」和/ WEB-INF一起使用/properties/properties-config.xml」。
我映射在我的應用程序-servlet.xml中,並試圖‘文件:/resources/properties/properties-config.xml’
這應該是簡單,並不鮮見但我找不到這樣獲取屬性文件的示例。
@ImportResource("file:/properties/properties-config.xml")
@Configuration
public class AppConfig {
protected final Log logger = LogFactory.getLog(getClass());
private @Value("#{v1Properties['v1.dataUrl']}") String dataUrl;
private @Value("#{v1Properties['v1.metaUrl']}") String metaUrl;
private @Value("#{v1Properties['v1.user']}") String v1User;
private @Value("#{v1Properties['v1.password']}") String v1Password;
@Bean
V1Config v1Config() {
V1Config v1Config = new V1Config();
v1Config.setDataUrl(dataUrl);
v1Config.setMetaUrl(metaUrl);
v1Config.setUserId(v1User);
v1Config.setPassword(v1Password);
return v1Config;
}
@Bean
ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
}