0

I am using Spring boot 2, need to print logs on execution of org.apache.catalina.webresources.CachedResource.validateResource() inside library.

I have created the class and added the dependencies in build.gradle

@Aspect
@Configuration
public class LoggingCollabCustomAspect {

    public static final String TO_STRING_RESULT = "changed";

    @Pointcut("execution(* org.apache.catalina.webresources.CachedResource.validateResource(*))")
    public void loadExternalResource()
    {
    }

    @Around("loadExternalResource()")
    public Object showLogs(ProceedingJoinPoint joinPoint) throws Throwable
    {
        Object ignoredToStringResult = joinPoint.proceed();
        System.out.println("File changed: " + ignoredToStringResult);
        return TO_STRING_RESULT;
    }
}

Main.java

@SpringBootApplication
@ServletComponentScan
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class Main
{

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

Build.gradle

compile group: 'org.springframework', name: 'spring-aop', version: '5.1.9.RELEASE'
compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.4'
compile group: 'org.aspectj', name: 'aspectjrt', version: '1.9.4'

I expect the aspect message to be printed but its not working

Ravi
  • 195
  • 3
  • 15
  • If it is not a Spring bean, it will not have pointcut, because Spring creates a runtime-proxy around aspected beans. – helospark Sep 25 '19 at 10:29
  • I think aspectjweaver could make it happen without bean – Ravi Sep 25 '19 at 10:31
  • In that case, you have to use load-time weaving. Check this: https://stackoverflow.com/a/41384525/8258222 – helospark Sep 25 '19 at 10:38
  • alright, I will try it. Thanks – Ravi Sep 25 '19 at 10:43
  • Does anyone ever read the Spring AOP documentation or use the search function on SO before trying to use it as a new technology? This question comes up almost every day here, e.g. I [answered it yesterday](https://stackoverflow.com/a/58080416/1082681). BTW, what is the purpose of the `@Configuration` annotation on the aspect? An aspect is not a configuration class, you should put your configuration into a separate class. – kriegaex Sep 25 '19 at 11:52

0 Answers0