0

我可以使用以下配置在我的 spring 应用程序中读取属性文件(注意类路径中的通配符

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
    <value>classpath*:*/**/test*.properties</value>
</property>

但是,当我使用相同的通配符模式使用org.springframework.web.util.Log4jConfigListenerin指定自定义 Log4j 属性文件时web.xml,它会因讨厌而失败,FileNotFoundException并且 Log4j 未初始化。

有人可以帮我解决这个问题,并指出我在这里缺少什么。

web.xml

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath*:*/**/customLog4j*.properties</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

PS:我知道财产占位符,即。${SOME_PLACE_HOLDER}我们可以用相应的系统/环境属性替换占位符值)在我的情况下无法应用,因为我们无法控制设置此类系统/环境属性并且必须使用通配符来解析自定义 log4j 属性的路径.

4

2 回答 2

1

用于从您指定的路径加载的Log4ConfigListener用途Log4jWebConfigurerResourceUtilsURL

public static URL getURL(String resourceLocation) throws FileNotFoundException {
    ... // trying with prefix 'classpath:' which you don't have
    try {
        // try URL
        return new URL(resourceLocation); // this will throw malformed
    }
    catch (MalformedURLException ex) {
        // no URL -> treat as file path
        try {
            return new File(resourceLocation).toURI().toURL();
        }
        catch (MalformedURLException ex2) {
            throw new FileNotFoundException("Resource location [" + resourceLocation +
                    "] is neither a URL not a well-formed file path");
        }
    }
}

所以你得到一个FileNotFoundException. javadocLog4jWebConfigurer示例解释了它可以采用的路径。我认为它不适用于通配符。

解释为什么PropertyPlaceholderConfigurer可以读取它:XML bean 解析器读取属性中的值classpath*:*/**/test*.propertieslocations生成一些实现,ClassPathResource并将其传递给实际的 bean。通配符行为包含在其中。

于 2013-08-27T17:20:41.920 回答
0

为他人的利益发布解决方案。

PathMatchingResourcePatternResolvergetResources方法解析通配符类路径并将找到的所有匹配项作为Resource数组返回

Resource然后可以相应地使用该对象。

于 2013-08-28T10:16:36.367 回答