Search code examples
springannotationsaspect

Element Cannot be Resolved as Variable error in a custom annotation for method interception


New to Spring and AOP programming. Working on a spring AOP tutorial to write aspects that intercept method calls. Would like to enable time logging.

As instructed by the tutorial I created a custom annotation for logging and an aspect to define what should be done when this annotation is called. The code below is the TrackTime annotation:

package com.in28minutes.springboot.tutorial.basics.example.aop;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TrackTime {}

However Eclipse is displaying the errors – “Element Cannot be Resolved as Variable/Retention Cannot be resolved to a variable”

I then created an aspect called MethodExecutionCalculationAspect with the ‘TrackTime’ annotation.

@Around("@annotation(com.in28minutes.springboot.tutorial.
basics.example.aop.TrackTime)")

MethodExecutionCalculationAspect

package com.in28minutes.springboot.tutorial.basics.example.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

@Aspect
@Configuration
public class MethodExecutionCalculationAspect {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

@Around("@annotation
(com.in28minutes.springboot.tutorial.basics.example.aop.TrackTime)")

    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();

    joinPoint.proceed();
    long timeTaken = System.currentTimeMillis() - startTime;
    logger.info("Time Taken by {} is {}", joinPoint, timeTaken);
}

}

@Around uses an around advice. It intercepts the method call and uses joinPoint.proceed() to execute the method. @annotation(com.in28minutes.springboot.tutorial.basics.example.aop.TrackTime) is the pointcut to define interception based on an annotation — @annotation followed by the complete type name of the annotation.

Once I correct the annotation and the advice, I’m hoping to use the annotation on methods for time tracking. as shown below:

@Service
public class Business1 {
    @TrackTime
    public String calculateSomething(){

Any help would be appreciated.

Information about the project is as follows:

SpringBootTutorialBasicsAplication.java: The Spring Boot application class generated with Spring Initializer. This class acts as the launching point for the application.

• pom.xml: Contains all the dependencies needed to build this project using Spring Boot Starter AOP.

• Business1.java, Business2.java, Dao1.java, Dao2.java: Business classes are dependent on DAO classes.

• We would write aspects to intercept calls to these business and DAO classes.

• AfterAopAspect.java: Implements a few After advices.

• UserAccessAspect.java: Implements a Before advice to do an access check.

• BusinessAopSpringBootTest.java: The unit test that invokes the business methods.

• Maven 3.0+ is your build tool • Eclipse. • JDK 1.8+


Solution

  • Your TrackTime is missing imports for RetentionPolicy and Target.

    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;