javaspringannotationsperf4j

Perf4j does not profile annotated Interface method


I have the following interface and class

public interface ServiceA {

    @Profiled
    String getString();
}

public class ServiceAImpl implements ServiceA{
    String getString(){
      System.out.println("getString");
    }

}

<beans ....>    
    <aop:aspectj-autoproxy />
     <bean id="timingAspect" class="org.perf4j.log4j.aop.TimingAspect"/>"
     <bean id="serviceA" class="com.naresh.profiler.service.ServiceAImpl" />
</beans>

When I do serviceA.getString() the call was not intercepted by the TimingAspect. It does intercepted if I move the annotation from interface to implementation class. The reason I see is that method level annotations are not inherited. How to solve this? By writing some CustomBeanAnnotationProcessor?


Solution

  • Even if @Profiled used @Inherit it wouldn't work because inherited annotations are only ever passed on to sub classed and not implementations of interfaces.

    @Inherited annotations are not inherited when used to annotate anything other than a type. A type that implements one or more interfaces never inherits any annotations from the interfaces it implements.

    Source: http://www.eclipse.org/aspectj/doc/released/adk15notebook/annotations.html#annotation-inheritance

    More details: http://www.jroller.com/melix/entry/the_truth_about_annotations_inheritance

    What would your custom processor do, query a service's interface for @Profiled? I suggest you manually annotate the service implementations because in my experience @Profiled is only helpful if you use it with its tag and message attributes (which differ in each implementing class I suppose).

    http://perf4j.codehaus.org/devguide.html#Adding_the_Profiled_Annotation_to_Method_Declarations -> "@Profiled(tag = "dynamicTag_{$0}")"