Working with Springboot 2.7.0. I had a a working application and I made these changes on top of it
Aspect Configuration
@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class AspectConfig {}
Aspect Interface
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Timed { }
Aspect Class to Measure method execution time
@Around("@annotation(Timed)")
public Object measureExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
Object proceed = joinPoint.proceed();
LOG.info("Time taken for {} is {} ms, joinPoint.getSignature(), System.currentTimeMillis() - start,);
return proceed;
}
Added the new @Timed annotation to an existing method in a bean (omitting non relevant code)
@Component
@ConditionalOnExpression("${oauth.enabled}")
public class JwtAuthFilter extends OncePerRequestFilter {
@Timed
public boolean verifySignatureAndExpiry(String bearerToken){
// method logic
}
}
This causes the Springboot application to fail startup.
I can get it to start if I add @Aspect to the JwtAuthFilter class.
but why would I need to do that? It makes the @Timed annotation limited use if I have to annotate every class that needs to use it with @Aspect. Not to mention, though there are no errors, the functionality won't work because an Aspect cannot work on another Aspect.
@Timed works on my controller method though.
@RestController
@RequestMapping(value = "/api/v1", produces = MediaType.APPLICATION_JSON_VALUE)
public class HealthController {
@GetMapping("/health")
@Timed
public Map<String, String> health(){
return Map.of("status", "up");
}
}