java - 通过 .properties 文件进行metrics-spring配置

标签 java spring metrics jersey-2.0

我正在尝试通过配置文件配置metrics-spring

在我的spring.xml中我添加了

<bean id="propertyPlaceholderConfigurer"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>
                classpath:metrics.properties
            </value>
        </list>
    </property>
    <property name="systemPropertiesModeName"
              value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="searchSystemEnvironment" value="true"/>
</bean>

充满了类似的东西

metrics.reporter.type=console

然后我在 spring 配置中设置它,通过 ${metrics.reporter.type} 访问该属性

<metrics:reporter metric-registry="metrics" type="${metrics.reporter.type}" period="1m"/>

在Web应用程序启动期间,由于上面的未解析的变量,Spring抛出了BeanDefinitionParsingException

Configuration problem: No ReporterElementParser found for reporter type '${metrics.reporter.type}'

我正在对 mongo 主机和端口使用此配置方法(通过属性文件),它的工作方式就像一个魅力。

我正在 Tomcat7、Spring 4.0.5.RELEASE、metrics Framework 3.1.0-SNAPSHOT(我需要 jersey 2 支持)和metrics-spring 3.0.1 中运行。我也尝试过使用自编译的metrics-spring 3.1.0-SNAPSHOT,但没有解决我的问题。

[编辑]

找到这个issue这说明 ElementParser 不支持 SpEL。

最佳答案

恐怕无法在 type 属性中使用属性占位符。 Spring 不会解析属性占位符或 SpEL,直到metrics-spring 读取 type 属性并解析reporter 元素之后的阶段(这是允许在所有其他属性中使用占位符和 bean 引用所必需的)。

一个可能的解决方案是配置您可能想要使用的所有报告器,并在启用的属性中使用占位符:

<metrics:reporter metric-registry="metrics" type="console" period="1m"
                  enabled="${metrics.reporter.console.enabled}" />

<metrics:reporter metric-registry="metrics" type="slf4j" period="1m"
                  enabled="${metrics.reporter.slf4j.enabled}" />

以及属性文件:

metrics.reporter.console.enabled=true
metrics.reporter.slf4j.enabled=false

我希望这是有道理的,我度过了漫长的一周!

关于java - 通过 .properties 文件进行metrics-spring配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25135007/

相关文章:

java - 正则表达式测试字符串是否包含数字

java - 自动发送电子邮件

java - 为什么我们在weka评估函数中使用训练数据?

java - Spring Integration - 健全性检查的最佳实践

statistics - Spring Integration channel 统计指标

linux - 从 docker 容器中获取 Linux 主机指标

java - 为什么要使用回调方法进行多态

java - Mapstruct-找不到没有源的属性的映射

Java BigDecimal 将负值变为正值