首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我能让Spring的<context:property-placeholder />告诉我它正在查找什么类路径吗?

我能让Spring的<context:property-placeholder />告诉我它正在查找什么类路径吗?
EN

Stack Overflow用户
提问于 2011-05-05 19:02:33
回答 3查看 2.5K关注 0票数 1

我们的Spring applicationContext.xml中有这一行

代码语言:javascript
复制
<context:property-placeholder location="classpath*:*.properties" />

但它并不是寻找和替换我们认为应该是的特定属性价值。我们有没有办法让这个特殊的属性占位符告诉我们它正在查找的路径,它正在查找的文件以及它正在查看的属性?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-05-05 21:09:15

您可以子类化PropertyPlaceHolderConfigurer,如下所示:

代码语言:javascript
复制
public class LoggingPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    @Override
    public void setLocations(final Resource[] locations) {
        if(logger.isDebugEnabled())
            for (final Resource resource : locations) {
                logger.debug("Using resource: " + resource);
            }
            super.setLocations(locations);
    }

    @Override
    protected Properties mergeProperties() throws IOException {
        final Properties mergedProperties = super.mergeProperties();
        if(logger.isDebugEnabled())
            for (final Entry<String, Object> propertyEntry :
                new TreeMap<String, Object>((Map) mergedProperties).entrySet()) {

                logger.debug(
                    "Key:" + propertyEntry.getKey()
                + ", value:" + propertyEntry.getValue());
            }
        return mergedProperties;
    }

}

现在手动连接您的自定义类(命名空间不起作用):

代码语言:javascript
复制
<bean class="path.to.LoggingPlaceholderConfigurer">
  <property name="locations" value="classpath*:*.properties" />
</bean>

并设置日志记录配置,以便为LoggingPlaceholderConfigurer激活日志级调试

(这是<context:property-placeholder>的临时替代品,仅用于调试目的)

票数 4
EN

Stack Overflow用户

发布于 2011-05-05 19:53:02

我不这么认为,但是您可以通过调用System.getProperty("java.class.path")来获取当前的类路径

票数 0
EN

Stack Overflow用户

发布于 2011-05-05 20:05:31

您可以将spring框架源代码附加到您的项目中,并使用调试,您可以查看找到/读取了哪些属性文件。我认为这是一个项目配置/打包/部署问题。尝试复制您的属性文件,比如my.properties,然后放入其中一个包中,看看它是否正常工作。如果它可以工作,那么你需要重新配置你的类路径。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5896659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档