I am trying to create a timer aspect for measuring the execution time of methods.
I created an annotation named @Timer :
@Retention(RetentionPolicy.RUNTIME) @Target(value = {ElementType.METHOD, ElementType.TYPE}) public @interface Timer { String value(); }
And then I created the aspect as follows:
@Aspect public class MetricAspect { @Autowired private MetricsFactory metricsFactory; @Pointcut("@annotation(my.package.Timer)") public void timerPointcut() {} @Around("timerPointcut() ") public Object measure(ProceedingJoinPoint joinPoint) throws Throwable { } private Timer getClassAnnotation(MethodSignature methodSignature) { Timer annotation; Class<?> clazz = methodSignature.getDeclaringType(); annotation = clazz.getAnnotation(Timer.class); return annotation; }
I have a configuration class as follows:
@Configuration @EnableAspectJAutoProxy public class MetricsConfiguration { @Bean public MetricAspect notifyAspect() { return new MetricAspect(); } }
Everything until defined here in a packaged jar that I use as dependencies in my spring boot application
In my spring boot application, I import MetricsConfiguration and I debugged the code and saw that the MetricAspect bean was created.
I use it in code as follows:
@Service public class MyService { ... @Timer("mymetric") public void foo() {
But my code does not reach the measure method. Not sure what I am missing.
To complete the image, I have these dependencies in my pom file:
<dependencies> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.4</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.7.4</version> </dependency> </dependencies>
What is the @Configuration class that imports MetricsConfiguration :
@Configuration @EnableAspectJAutoProxy @Import(MetricsConfiguration.class) @PropertySource("classpath:application.properties") public class ApplicationConfiguration { }
It boots with automatic spring boot.