我可以获取Spring <context:property-placeholder>来告诉我它正在查找的类路径吗?</context:property-placeholder>

时间:2011-05-05 11:02:33

标签: java spring classpath

我们在Spring applicationContext.xml中有这一行:

<context:property-placeholder location="classpath*:*.properties" />

但它没有找到并代替我们认为应该是的特定财产价值。我们有什么方法可以让这个特殊的属性 - 占位符告诉我们它正在查看的路径,它正在查找的文件以及它所看到的属性吗?

3 个答案:

答案 0 :(得分:4)

您可以继承PropertyPlaceHolderConfigurer,如下所示:

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;
    }

}

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

<bean class="path.to.LoggingPlaceholderConfigurer">
  <property name="locations" value="classpath*:*.properties" />
</bean>

并设置日志记录配置,以便LoggingPlaceholderConfigurer

激活日志级别调试

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

答案 1 :(得分:0)

我不这么认为,但您可以通过调用System.getProperty("java.class.path")

来获取当前的类路径

答案 2 :(得分:0)

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