简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM