如何记录Spring加载的属性?

时间:2013-01-13 12:22:09

标签: spring

是否可以使用<context:property-placeholder />简单地记录spring加载的属性文件的所有内容?

由于

2 个答案:

答案 0 :(得分:10)

您可以将org.springframework.core.env.PropertySourcesPropertyResolver的日志级别设置为&#34; debug&#34;。然后,您将能够在解析期间看到属性的值。

答案 1 :(得分:2)

你可以这样做:

<context:property-placeholder properties-ref="myProperties"/>

<bean id="myProperties"
      class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="locations">
    <list>
      .. locations
    </list>
  </property>
</bean>

并添加一个类似于下面的日志bean(此处基于注释并使用slf4j api):

@Component
public class PropertiesLogger {
  private static Logger logger = LoggerFactory.getLogger(PropertiesLogger.class);

  @Resource("myProperties")
  private Properties props;

  @PostConstruct
  public void init() {
    for (Map.Entry<Object, Object> prop : props.entrySet()) {
      logger.debug("{}={}", prop.getKey(), prop.getValue());
    }
  }
}