Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将变量传递给方面_Java_Spring_Aop_Statsd - Fatal编程技术网

Java 将变量传递给方面

Java 将变量传递给方面,java,spring,aop,statsd,Java,Spring,Aop,Statsd,我尝试使用AOP和statsd的组合来度量代码中的度量。特别是,我希望开发人员能够通过向方法添加注释并指定其名称和标记来创建计时器,如下所示: @Timed(name="executeHistoryDataSet", tags={dataSetInfo.getLabel()}) @Override public String executeHistoryDataSet(DataSet dataSetInfo) throws DataServiceException { String re

我尝试使用AOP和statsd的组合来度量代码中的度量。特别是,我希望开发人员能够通过向方法添加注释并指定其名称和标记来创建计时器,如下所示:

@Timed(name="executeHistoryDataSet", tags={dataSetInfo.getLabel()})
@Override
public String executeHistoryDataSet(DataSet dataSetInfo) throws DataServiceException {
    String results = executeDataSet(dataSetInfo);
    return results;
}
这将包括有关作为标记的参数的信息。但是,这显示了一个
属性值必须是常量的错误,因为注释的参数不能是可变的,而
dataSetInfo.getLabel()
肯定不是

希望开发人员能够创建一个新的计时器,而无需创建新的方面和建议(
@Timed
将是所有计时器的注释),那么是否有任何方法可以使用此功能,我可以将类似
标记的内容传递给建议,但这不可能是恒定的,不同的方法会有所不同吗


以下是注释本身:

public @interface Timed {
    String name();
    String[] tags();
}
及其方面:

@Aspect
public class MetricsAspects {

    private static final StatsDClient statsd = new NonBlockingStatsDClient(
        "my.prefix",                    //prefix to any stats
        "statsd-host",                  //often "localhost"
        8125,                           //port
        "tag", "another tag"            //tags always applied to stats
    );

    @Around ("execution(* *(..) && @annotation(timed)")
    public Object timeAround(ProceedingJoinPoint point, Timed timed) throws Throwable {

        String metricName = timed.name();
        String[] metricTags = timed.tags();
        long start = System.currentTimeMillis();
        Object result = point.proceed();
        long duration = System.currentTimeMillis() - start;
        statsd.recordExecutionTime(metricName, duration, metricTags);
        return result;
    }
}

注释需要在编译时已知其参数。由于在编译时,无论何时执行此函数,都不知道将哪个
Dataset
对象提供给它,因此编译器会抛出错误。

此错误有点误导性。注释成员值必须是常量表达式、类文字或枚举常量。