Abgar Kebar
Abgar Kebar

Reputation: 63

Spring Boot aspect doesn't work in my API

I am trying to make a Logging aspect for my Spring boot API so that every time a request comes to the server everything is logged to a file. However I can't get my aspect to get invoked. What am I doing wrong?

Here is the Aspect

@Aspect
@Component
public class Logging {

private Logger log;

@Before("execution(* com.package.TeamController.*(..))")
public void before(JoinPoint joinPoint){
    log = Logger.getLogger(joinPoint.getClass());
    log.info("Starting request " + joinPoint.getSourceLocation());
    if(joinPoint.getArgs()!=null){
        log.info(" Arguments are");
        for(Object obj : joinPoint.getArgs()){
            log.info(obj.getClass().getName() );
        }
    }
}
}

The Application entry point is

@SpringBootApplication(scanBasePackages =
           {"com.package", "com.aspect"})
@EnableJpaRepositories
public class Application {

    public static void main(String[] args) {
     SpringApplication.run(Application.class, args);
    }
}

And my application.properties file has this line

spring.aop.auto=true

What am I doing wrong? Why doesn't the aspect log info when the method from TeamController is invoked?

NOTE The team controller method is invoked through a GET request

NOTE2 Intelij Ide has a visual tool for navigating to advised methods, and when I looked it up it showed me the controller methods, however the advise still doesn't work properly

Upvotes: 2

Views: 748

Answers (1)

Abgar Kebar
Abgar Kebar

Reputation: 63

Found the problem. When first creating the file Intelij proposed to make an aspect instead of a java class, which made the file Logging.aj and created

public aspect Logging{}

When I renamed aspect keyword to class keyword the file was still of a .aj type. I renamed it to .java and it works fine now.

Upvotes: 1

Related Questions