@Configuration @ImportResource文件系统资源从 Web-Inf 读取属性



我正在尝试使用我的Spring MVC应用程序的基于Java的配置从WEB-INF读取属性文件。当我将属性目录放在 src 目录中并使用 class: (ClassPathResource) 时,我可以完成这项工作。

我想将@ImportResource与文件一起使用:(文件系统资源),当它位于WebContent或WEB-INF中时,它将从

属性/或资源/属性/或/属性/或/资源/属性/读取

当我使用file时:我得到一个FileNotFoundException。

我尝试移动属性目录,并将@ImportResource与"file:/properties/properties-config.xml","file:/WebContent/properties/properties-config.xml"和/WEB-INF/properties/properties-config.xml"一起使用。

我在我的app-servlet中映射.xml并尝试了"file:/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;
}
}

试试这个,

@ImportResource("classpath:properties-config.xml")

最新更新