【问题标题】:@Configuration @ImportResource with FileSystemResource reading properties from Web-Inf@Configuration @ImportResource 和 FileSystemResource 从 Web-Inf 读取属性
【发布时间】:2012-12-02 16:20:06
【问题描述】:

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

我想将 @ImportResource 与 file: (FileSystemResource) 一起使用,当它位于 WebContent 或 WEB-INF 中时,它将从 properties/ 或 resources/properties/ 或 /properties/ 或 /resources/properties/ 读取

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

我尝试移动属性目录并将@ImportResource 与“file:/properties/properties-config.xml”、“file:/WebContent/properties/properties-config.xml”和/WEB-INF/ 一起使用属性/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;
}

}

【问题讨论】:

    标签: spring configuration


    【解决方案1】:

    试试这个,

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

    【讨论】:

      猜你喜欢
      • 2014-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      • 1970-01-01
      • 2011-01-09
      相关资源
      最近更新 更多